| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- #if UNITY_IOS
- using System.IO;
- using UnityEditor;
- using UnityEditor.Build;
- using UnityEditor.Callbacks;
- using UnityEditor.iOS.Xcode;
- namespace TowerDevil.Editor
- {
- internal static class GameCenterBuild
- {
- const string Template = "Assets/TowerDevil/Editor/GameCenterLeaderboards.plist";
- const string Night = "BestNightRankID", Score = "TotalScoreRankID";
- [PostProcessBuild(1001)]
- public static void Configure(BuildTarget target, string path)
- {
- if (target != BuildTarget.iOS) return;
- var source = new PlistDocument();
- source.ReadFromFile(Template);
- string night = ReadId(source.root, Night), score = ReadId(source.root, Score);
- if (night == score) throw new BuildFailedException("Game Center requires two distinct leaderboard IDs in " + Template);
- string plistPath = Path.Combine(path, "Info.plist");
- var info = new PlistDocument();
- info.ReadFromFile(plistPath);
- info.root.SetString(Night, night);
- info.root.SetString(Score, score);
- info.WriteToFile(plistPath);
- string pbxPath = PBXProject.GetPBXProjectPath(path);
- var project = new PBXProject();
- project.ReadFromFile(pbxPath);
- string app = project.GetUnityMainTargetGuid();
- string entitlements = project.GetEntitlementFilePathForTarget(app);
- if (string.IsNullOrEmpty(entitlements)) entitlements = "Unity-iPhone.entitlements";
- project.AddFrameworkToProject(project.GetUnityFrameworkTargetGuid(), "GameKit.framework", false);
- project.WriteToFile(pbxPath);
- var capabilities = new ProjectCapabilityManager(pbxPath, entitlements, null, app);
- capabilities.AddGameCenter();
- capabilities.Entitlements.root.SetBoolean("com.apple.developer.game-center", true);
- capabilities.WriteToFile();
- }
- static string ReadId(PlistElementDict root, string key)
- {
- if (root.values.TryGetValue(key, out var value) && value is PlistElementString text &&
- !string.IsNullOrWhiteSpace(text.value)) return text.value.Trim();
- throw new BuildFailedException("Missing Game Center leaderboard ID " + key + " in " + Template);
- }
- }
- }
- #endif
|