浏览代码

Merge pull request #76 from unity/collaboration-support-xcode12

Support multipeer connectivity in iOS 14
/4.1
GitHub Enterprise 4 年前
当前提交
d61ce035
共有 5 个文件被更改,包括 126 次插入3 次删除
  1. 6
      Assets/Scenes/ARCollaborationData/CollaborativeSession.cs
  2. 8
      Assets/Scenes/ARCollaborationData/Editor.meta
  3. 11
      Assets/Scenes/ARCollaborationData/Editor/CollaborationBuildPostprocessor.cs.meta
  4. 104
      Assets/Scenes/ARCollaborationData/Editor/CollaborationBuildPostprocessor.cs

6
Assets/Scenes/ARCollaborationData/CollaborativeSession.cs


/// </summary>
public string serviceType
{
get { return m_ServiceType; }
set { m_ServiceType = value; }
get => m_ServiceType;
set => m_ServiceType = value;
}
ARSession m_ARSession;

}
#endif
}
}
}

8
Assets/Scenes/ARCollaborationData/Editor.meta


fileFormatVersion: 2
guid: 3598feda6320f4bc6b3930000e47b93b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

11
Assets/Scenes/ARCollaborationData/Editor/CollaborationBuildPostprocessor.cs.meta


fileFormatVersion: 2
guid: 4c9c547b7f3964e75b3b6d84570de72b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

104
Assets/Scenes/ARCollaborationData/Editor/CollaborationBuildPostprocessor.cs


#if UNITY_IOS
using System.Collections.Generic;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.iOS.Xcode;
using System.IO;
using System.Linq;
using UnityEditor.Callbacks;
using UnityEngine;
using UnityEngine.XR.ARFoundation.Samples;
namespace UnityEditor.XR.ARFoundation.Samples
{
/// <summary>
/// This build processor finds all <see cref="CollaborativeSession"/> components in the build and adds the
/// necessary plist entries according to https://developer.apple.com/documentation/multipeerconnectivity
/// </summary>
class CollaborationBuildProcessor : IPostprocessBuildWithReport, IPreprocessBuildWithReport
{
public int callbackOrder => 2;
void IPreprocessBuildWithReport.OnPreprocessBuild(BuildReport report)
{
s_CollaborativeSessions = new List<CollaborativeSession>();
}
static PlistElementArray GetOrCreatePlistElementArray(PlistElementDict dict, string key)
{
var element = dict[key];
return element == null ? dict.CreateArray(key) : element.AsArray();
}
/// <summary>
/// Adds necessary plist entries required by iOS 14+.
/// See https://developer.apple.com/documentation/multipeerconnectivity for details.
/// </summary>
/// <param name="report">The BuildReport provided by the post build process.</param>
void IPostprocessBuildWithReport.OnPostprocessBuild(BuildReport report)
{
if (report.summary.platform != BuildTarget.iOS)
return;
if (s_CollaborativeSessions.Count == 0)
return;
var plistPath = Path.Combine(report.summary.outputPath, "Info.plist");
var plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
var root = plist.root;
// The app requires permission from the user to use the local network
if (root["NSLocalNetworkUsageDescription"] == null)
{
// If no entry exists, then we will add one with the prompt "Collaborative Session"
root["NSLocalNetworkUsageDescription"] = new PlistElementString("Collaborative Session");
}
// Collect all the service names we need to add
var bonjourServices = GetOrCreatePlistElementArray(root, "NSBonjourServices");
var existingValues = new HashSet<string>(bonjourServices.values
.Where(value => value is PlistElementString)
.Select(value => value.AsString()));
var valuesToAdd = new HashSet<string>();
foreach (var serviceType in s_CollaborativeSessions
.Select(collaborativeSession => collaborativeSession.serviceType)
.Where(serviceType => !string.IsNullOrEmpty(serviceType)))
{
// Each "serviceType" must be registered as a Bonjour Service or the app will crash
// See https://developer.apple.com/documentation/bundleresources/information_property_list/nsbonjourservices
AddIfNecessary(existingValues, valuesToAdd, $"_{serviceType}._tcp");
AddIfNecessary(existingValues, valuesToAdd, $"_{serviceType}._udp");
}
foreach (var value in valuesToAdd)
{
bonjourServices.AddString(value);
}
File.WriteAllText(plistPath, plist.WriteToString());
}
static void AddIfNecessary(HashSet<string> existingValues, HashSet<string> valuesToAdd, string value)
{
if (!existingValues.Contains(value))
valuesToAdd.Add(value);
}
/// <summary>
/// Collects all CollaborativeSession components from each scene in the final build
/// </summary>
[PostProcessScene]
static void OnPostProcessScene()
{
foreach (var collaborativeSession in Object.FindObjectsOfType<CollaborativeSession>())
{
s_CollaborativeSessions.Add(collaborativeSession);
}
}
static List<CollaborativeSession> s_CollaborativeSessions = new List<CollaborativeSession>();
}
}
#endif
正在加载...
取消
保存