Skip to content

Commit 80357f9

Browse files
jiangjiayijiangjiayi
authored andcommitted
[增加] Cap 配置
1 parent eaaa045 commit 80357f9

8 files changed

Lines changed: 280 additions & 2 deletions
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#if UNITY_IOS
2+
using System;
3+
using System.Collections;
4+
using UnityEditor.iOS.Xcode;
5+
using UnityEngine;
6+
7+
namespace GameFrameX.Xcode.Editor
8+
{
9+
internal partial class PostProcessBuildHelper
10+
{
11+
/// <summary>
12+
/// 设置Capabilities
13+
/// </summary>
14+
/// <param name="pbxProject">PBX项目</param>
15+
/// <param name="targetGuid">目标GUID</param>
16+
/// <param name="path"></param>
17+
/// <param name="hashtable">配置数据</param>
18+
static void SetCapabilities(PBXProject pbxProject, string targetGuid, string path, Hashtable hashtable)
19+
{
20+
try
21+
{
22+
if (hashtable == null)
23+
{
24+
return;
25+
}
26+
27+
Debug.Log("开始设置Capabilities");
28+
29+
var projectCapabilityManager = new ProjectCapabilityManager(path + "/Unity-iPhone.xcodeproj/project.pbxproj", "gameframex.entitlements", "Unity-iPhone");
30+
31+
// In-App Purchase
32+
if (hashtable.ContainsKey("inAppPurchase") && (bool)hashtable["inAppPurchase"])
33+
{
34+
projectCapabilityManager.AddInAppPurchase();
35+
Debug.Log("已添加 In-App Purchase Capability");
36+
}
37+
38+
// Game Center
39+
if (hashtable.ContainsKey("gameCenter") && (bool)hashtable["gameCenter"])
40+
{
41+
projectCapabilityManager.AddGameCenter();
42+
Debug.Log("已添加 Game Center Capability");
43+
}
44+
45+
// Push Notifications
46+
if (hashtable.ContainsKey("pushNotifications") && (bool)hashtable["pushNotifications"])
47+
{
48+
projectCapabilityManager.AddPushNotifications(false);
49+
Debug.Log("已添加 Push Notifications Capability");
50+
}
51+
52+
// Sign In with Apple
53+
if (hashtable.ContainsKey("signInWithApple") && (bool)hashtable["signInWithApple"])
54+
{
55+
projectCapabilityManager.AddSignInWithApple();
56+
Debug.Log("已添加 Sign In with Apple Capability");
57+
}
58+
59+
// Background Modes
60+
if (hashtable.ContainsKey("backgroundModes"))
61+
{
62+
var backgroundModes = hashtable["backgroundModes"] as ArrayList;
63+
if (backgroundModes != null && backgroundModes.Count > 0)
64+
{
65+
var modes = new BackgroundModesOptions();
66+
foreach (string mode in backgroundModes)
67+
{
68+
switch (mode.ToLower())
69+
{
70+
case "audio":
71+
modes |= BackgroundModesOptions.AudioAirplayPiP;
72+
break;
73+
case "location":
74+
modes |= BackgroundModesOptions.LocationUpdates;
75+
break;
76+
case "voip":
77+
modes |= BackgroundModesOptions.VoiceOverIP;
78+
break;
79+
case "newsstand":
80+
modes |= BackgroundModesOptions.NewsstandDownloads;
81+
break;
82+
case "external":
83+
modes |= BackgroundModesOptions.ExternalAccessoryCommunication;
84+
break;
85+
case "bluetooth":
86+
modes |= BackgroundModesOptions.UsesBluetoothLEAccessory;
87+
break;
88+
case "bluetooth-peripheral":
89+
modes |= BackgroundModesOptions.ActsAsABluetoothLEAccessory;
90+
break;
91+
case "fetch":
92+
modes |= BackgroundModesOptions.BackgroundFetch;
93+
break;
94+
case "remote-notification":
95+
modes |= BackgroundModesOptions.RemoteNotifications;
96+
break;
97+
}
98+
}
99+
100+
projectCapabilityManager.AddBackgroundModes(modes);
101+
Debug.Log($"已添加 Background Modes Capability: {string.Join(", ", backgroundModes.ToArray())}");
102+
}
103+
}
104+
105+
// iCloud
106+
if (hashtable.ContainsKey("iCloud"))
107+
{
108+
var iCloudConfig = hashtable["iCloud"] as Hashtable;
109+
if (iCloudConfig != null)
110+
{
111+
bool enableKeyValueStorage = iCloudConfig.ContainsKey("keyValueStorage") && (bool)iCloudConfig["keyValueStorage"];
112+
bool enableiCloudDocument = iCloudConfig.ContainsKey("iCloudDocument") && (bool)iCloudConfig["iCloudDocument"];
113+
114+
var customContainers = iCloudConfig["customContainers"] as ArrayList;
115+
string[] containers = null;
116+
if (customContainers != null && customContainers.Count > 0)
117+
{
118+
containers = new string[customContainers.Count];
119+
for (int i = 0; i < customContainers.Count; i++)
120+
{
121+
containers[i] = customContainers[i].ToString();
122+
}
123+
}
124+
125+
projectCapabilityManager.AddiCloud(enableKeyValueStorage, enableiCloudDocument, containers);
126+
Debug.Log("已添加 iCloud Capability");
127+
}
128+
}
129+
130+
// App Groups
131+
if (hashtable.ContainsKey("appGroups"))
132+
{
133+
var appGroups = hashtable["appGroups"] as ArrayList;
134+
if (appGroups != null && appGroups.Count > 0)
135+
{
136+
string[] groups = new string[appGroups.Count];
137+
for (int i = 0; i < appGroups.Count; i++)
138+
{
139+
groups[i] = appGroups[i].ToString();
140+
}
141+
142+
projectCapabilityManager.AddAppGroups(groups);
143+
Debug.Log($"已添加 App Groups Capability: {string.Join(", ", groups)}");
144+
}
145+
}
146+
147+
// Associated Domains
148+
if (hashtable.ContainsKey("associatedDomains"))
149+
{
150+
var domains = hashtable["associatedDomains"] as ArrayList;
151+
if (domains != null && domains.Count > 0)
152+
{
153+
string[] domainArray = new string[domains.Count];
154+
for (int i = 0; i < domains.Count; i++)
155+
{
156+
domainArray[i] = domains[i].ToString();
157+
}
158+
159+
projectCapabilityManager.AddAssociatedDomains(domainArray);
160+
Debug.Log($"已添加 Associated Domains Capability: {string.Join(", ", domainArray)}");
161+
}
162+
}
163+
164+
projectCapabilityManager.WriteToFile();
165+
}
166+
catch (Exception e)
167+
{
168+
Debug.LogError(e);
169+
}
170+
}
171+
}
172+
}
173+
#endif

Editor/PostProcessBuildHelper.Capabilities.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#if UNITY_IOS
2+
using System.Collections;
3+
using UnityEditor.iOS.Xcode;
4+
5+
namespace GameFrameX.Xcode.Editor
6+
{
7+
internal partial class PostProcessBuildHelper
8+
{
9+
/// <summary>
10+
/// 添加运行搜索路径
11+
/// </summary>
12+
/// <param name="proj"></param>
13+
/// <param name="targetGuid"></param>
14+
/// <param name="table"></param>
15+
private static void AddRunPathSearchPaths(PBXProject proj, string targetGuid, Hashtable table)
16+
{
17+
if (table == null)
18+
{
19+
return;
20+
}
21+
22+
foreach (DictionaryEntry kv in table)
23+
{
24+
proj.AddBuildProperty(targetGuid, kv.Key.ToString().Trim(), kv.Value.ToString().Trim());
25+
}
26+
}
27+
}
28+
}
29+
#endif

Editor/PostProcessBuildHelper.RunPathSearchPaths.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/PostProcessBuildHelper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public static void OnPostProcessBuild(BuildTarget target, string path)
5555
RunArgument(path, table.Get("launcherArgs") as ArrayList);
5656
// PodFile
5757
RunPodfile(path, table.Get("podSource") as ArrayList);
58+
59+
// 设置Capabilities (只在主项目上设置)
60+
SetCapabilities(project, project.GetUnityMainTargetGuid(), path, table.Get<Hashtable>("capabilities"));
5861
}
5962
catch (Exception e)
6063
{
@@ -77,6 +80,8 @@ static void Run(PBXProject pbxProject, string targetGuid, Hashtable hashtable, s
7780
SetFilesCompileFlag(pbxProject, targetGuid, hashtable.Get<Hashtable>("filesCompileFlag"));
7881
// Linker Flag
7982
AddOtherLinkFlag(pbxProject, targetGuid, hashtable.Get<Hashtable>("otherLinkerFlag"));
83+
// Run Path Search Paths
84+
AddRunPathSearchPaths(pbxProject, targetGuid, hashtable.Get<Hashtable>("runPathSearchPaths"));
8085
}
8186
}
8287
}

Editor/XCodeConfigDemo.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"plist": {
33
"CFBundleURLTypes": [
44
{
@@ -61,6 +61,20 @@
6161
"podSource": [
6262
"https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git"
6363
],
64+
"capabilities": {
65+
"inAppPurchase": true,
66+
"gameCenter": false,
67+
"pushNotifications": false,
68+
"signInWithApple": false,
69+
"backgroundModes": [],
70+
"iCloud": {
71+
"keyValueStorage": false,
72+
"iCloudDocument": false,
73+
"customContainers": []
74+
},
75+
"appGroups": [],
76+
"associatedDomains": []
77+
},
6478
"unityFramework": {
6579
"libs": {
6680
"+": [
@@ -147,6 +161,9 @@
147161
"otherLinkerFlag": {
148162
"OTHER_LDFLAGS": "-ObjC"
149163
},
164+
"runPathSearchPaths": {
165+
"OTHER_LDFLAGS": "-ObjC"
166+
},
150167
"files": {
151168
"ios_libsM.txt": "Classes/ios_libsM.txt"
152169
},

Editor/XCodeConfigTemplate.json

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,28 @@
3131
{
3232
"OTHER_LDFLAGS": "-ObjC"
3333
}
34-
]
34+
],
35+
"tip4": "capabilities配置,用于设置iOS应用的各种能力",
36+
"capabilities": {
37+
"tip0": "In-App Purchase功能,布尔值",
38+
"inAppPurchase": true,
39+
"tip1": "Game Center功能,布尔值",
40+
"gameCenter": false,
41+
"tip2": "推送通知功能,布尔值",
42+
"pushNotifications": false,
43+
"tip3": "Sign In with Apple功能,布尔值",
44+
"signInWithApple": false,
45+
"tip4": "后台模式,字符串数组,可选值: audio, location, voip, newsstand, external, bluetooth, bluetooth-peripheral, fetch, remote-notification",
46+
"backgroundModes": [],
47+
"tip5": "iCloud配置,包含keyValueStorage和iCloudDocument布尔值,以及customContainers字符串数组",
48+
"iCloud": {
49+
"keyValueStorage": false,
50+
"iCloudDocument": false,
51+
"customContainers": []
52+
},
53+
"tip6": "App Groups,字符串数组",
54+
"appGroups": [],
55+
"tip7": "Associated Domains,字符串数组",
56+
"associatedDomains": []
57+
}
3558
}

Editor/XcodeConfig.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public sealed class XcodeConfig : ScriptableObject
1212

1313
[Header("Plist列表")] [SerializeField] public List<XcodeConfigPlist> plist = new List<XcodeConfigPlist>();
1414

15+
[Header("Capabilities配置")] [SerializeField] public XcodeConfigCapabilities capabilities = new XcodeConfigCapabilities();
16+
1517
[Header("主项目")] [SerializeField] public XcodeConfigData unityMain = new XcodeConfigData();
1618

1719
[Header("UnityFrameWork")] [SerializeField]
@@ -62,4 +64,19 @@ public sealed class XcodeConfigChange
6264
[Header("增加")] [SerializeField] public string[] add;
6365
[Header("删除")] [SerializeField] public string[] remove;
6466
}
67+
68+
[Serializable]
69+
public sealed class XcodeConfigCapabilities
70+
{
71+
[Header("In-App Purchase")] [SerializeField] public bool inAppPurchase = false;
72+
[Header("Game Center")] [SerializeField] public bool gameCenter = false;
73+
[Header("Push Notifications")] [SerializeField] public bool pushNotifications = false;
74+
[Header("Sign In with Apple")] [SerializeField] public bool signInWithApple = false;
75+
[Header("Background Modes")] [SerializeField] public string[] backgroundModes = new string[0];
76+
[Header("iCloud Key-Value Storage")] [SerializeField] public bool iCloudKeyValueStorage = false;
77+
[Header("iCloud Documents")] [SerializeField] public bool iCloudDocuments = false;
78+
[Header("iCloud Custom Containers")] [SerializeField] public string[] iCloudCustomContainers = new string[0];
79+
[Header("App Groups")] [SerializeField] public string[] appGroups = new string[0];
80+
[Header("Associated Domains")] [SerializeField] public string[] associatedDomains = new string[0];
81+
}
6582
}

0 commit comments

Comments
 (0)