| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- #if UNITY_IOS || UNITY_TVOS
- #define UNITY_XCODE_EXTENSIONS_AVAILABLE
- #endif
- using UnityEditor;
- using System.IO;
- using UnityEngine;
- #if UNITY_IOS && UNITY_EDITOR
- using UnityEditor.Callbacks;
- using UnityEditor.iOS.Xcode;
- #endif
- using AppleAuth.Editor;
- #if UNITY_XCODE_EXTENSIONS_AVAILABLE
- //using UnityEditor.iOS.Xcode;
- #endif
- public static class XCodePostProcessBuild
- {
- #if UNITY_IOS && UNITY_EDITOR
- private static readonly string[] csAddFrameworks = new string[]{
- "Security.framework","WebKit.framework", "CoreGraphics.framework"
- };
- [PostProcessBuild(1)]
- public static void OnPostprocessBuild(BuildTarget target, string path)
- {
- if (BuildTarget.iOS != target)
- {
- return;
- }
- string projectPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
- SetFrameworksAndBuildSettings(projectPath);
- //SetInfoList(path, "com.xmjssvr.BowArrow", "wxfe29f3f64e3c5d16");
- // 根据当前包名 ApplicationIdentifier 决定调用哪个参数
- string applicationIdentifier = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS);
- if (applicationIdentifier.Contains("PadBowArrow"))
- {
- // iPad版
- SetInfoList(path, "com.xmjssvr.PadBowArrow", "wx64d1835dbd218aec"); // 这里填写 iPad 版的 wx id
- }
- else
- {
- // 默认普通版
- SetInfoList(path, "com.xmjssvr.BowArrow", "wxfe29f3f64e3c5d16");
- }
- SetAssociatedDomains(projectPath, "xmjssvr.cn");
- setSignInWithApple(projectPath);
- // 这里新增你的两个方法调用添加国际化
- AddLocalizedPermissionDescriptions(path);
- AddLocalizedResourcesToXcode(path);
- }
- private static void SetFrameworksAndBuildSettings(string path)
- {
- PBXProject proj = new PBXProject();
- proj.ReadFromString(File.ReadAllText(path));
- string target = proj.GetUnityMainTargetGuid();
- string frameworkGuid = proj.GetUnityFrameworkTargetGuid();
- Debug.Log("Target Name is " + target);
- // 设置 BuildSettings
- //proj.AddBuildProperty(target, "Other Linker Flags", "-Objc -all_load");
- // 添加 Other Linker Flags
- proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-ObjC");
- proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-all_load");
- proj.AddBuildProperty(frameworkGuid, "OTHER_LDFLAGS", "-ObjC");
- proj.AddBuildProperty(frameworkGuid, "OTHER_LDFLAGS", "-all_load");
-
- proj.SetBuildProperty(target, "ENABLE_BITCODE", "NO");
- proj.SetBuildProperty(frameworkGuid, "ENABLE_BITCODE", "NO");
- //根据微信SDK文档的要求,加入相关的Frameworks
- for (int i = 0; i < csAddFrameworks.Length; ++i)
- {
- if (!proj.ContainsFramework(target, csAddFrameworks[i]))
- proj.AddFrameworkToProject(target, csAddFrameworks[i], false);
- }
- File.WriteAllText(path, proj.WriteToString());
- }
- public static void SetInfoList(string buildPath, string wxUrlName, string wxScheme)
- {
- string listPath = buildPath + "/Info.plist";
- PlistDocument plist = new PlistDocument();
- plist.ReadFromString(File.ReadAllText(listPath));
- // 在“info”标签栏的“URL type“添加“URL scheme”,值为你在微信后台注册的应用程序的 AppID
- PlistElementArray urlArray = plist.root.CreateArray("CFBundleURLTypes");
- PlistElementDict dict = urlArray.AddDict();
- dict.SetString("CFBundleTypeRole", "Editor");
- dict.SetString("CFBundleURLName", wxUrlName);
- PlistElementArray urlSchemes = dict.CreateArray("CFBundleURLSchemes");
- urlSchemes.AddString(wxScheme);
- // 在 “info”标签栏的“LSApplicationQueriesSchemes“添加weixin wechat和weixinULAPI
- PlistElementArray wxArray = plist.root.CreateArray("LSApplicationQueriesSchemes");
- wxArray.AddString("weixin");
- //wxArray.AddString("wechat");
- wxArray.AddString("weixinULAPI");
- wxArray.AddString("weixinURLParamsAPI");
- //PlistElementDict ed = plist.root;
- //ed.SetString("NSBluetoothAlwaysUsageDescription", "您是否允此App使用蓝牙,来搜索和连接设备?");
- //ed.SetString("NSBluetoothPeripheralUsageDescription", "您是否允此App使用蓝牙,来搜索和连接设备?");
- //ed.SetString("NSCameraUsageDescription", "App需要您的同意,才能访问照相机");
- //ed.SetString("NSPhotoLibraryUsageDescription", "App需要您的同意,才能访问相册");
- //ed.SetString("NSLocationWhenInUseUsageDescription", "我们需要通过您的地理位置信息获取您周边的相关数据");
- //ed.SetString("NSLocationAlwaysUsageDescription", "我们需要通过您的地理位置信息获取您周边的相关数据");
- File.WriteAllText(listPath, plist.WriteToString());
- }
- // 设置Associated Domains
- public static void SetAssociatedDomains(string pbxProjectPath, string domainUrl)
- {
- //默认 Target Name, 你自己的可能不一样
- string targetName = "Unity-iPhone";
- //Set the entitlements file name to what you want but make sure it has this extension
- string entitlementsFileName = "my_app.entitlements";
- var entitlements = new ProjectCapabilityManager(pbxProjectPath, entitlementsFileName, targetName);
- entitlements.AddAssociatedDomains(new string[] { "applinks:" + domainUrl });
- entitlements.WriteToFile();
- }
- // 设置Associated Domains
- public static void setSignInWithApple(string pbxProjectPath)
- {
- //默认 Target Name, 你自己的可能不一样
- string targetName = "Unity-iPhone";
- //Set the entitlements file name to what you want but make sure it has this extension
- string entitlementsFileName = "my_app.entitlements";
- var project = new PBXProject();
- project.ReadFromString(System.IO.File.ReadAllText(pbxProjectPath));
- var entitlements = new ProjectCapabilityManager(pbxProjectPath, entitlementsFileName, targetName, project.GetUnityMainTargetGuid());
- //entitlements.AddSignInWithAppleWithCompatibility(project.GetUnityFrameworkTargetGuid());
- entitlements.AddSignInWithAppleWithCompatibility();
- entitlements.WriteToFile();
- }
- public static void AddLocalizedPermissionDescriptions(string path)
- {
- string localizedFolderPath = Path.Combine(path, "LocalizedInfoPlist");
- if (!Directory.Exists(localizedFolderPath))
- Directory.CreateDirectory(localizedFolderPath);
- // 中文 (简体) zh-Hans.lproj
- string zhHansPath = Path.Combine(localizedFolderPath, "zh-Hans.lproj");
- if (!Directory.Exists(zhHansPath))
- Directory.CreateDirectory(zhHansPath);
- File.WriteAllText(Path.Combine(zhHansPath, "InfoPlist.strings"),
- "\"NSCameraUsageDescription\" = \"应用需要您的授权,以访问相机功能。\";\n" +
- "\"NSPhotoLibraryUsageDescription\" = \"应用需要您的授权,以访问您的照片库。\";\n" +
- "\"NSBluetoothAlwaysUsageDescription\" = \"请允许应用使用蓝牙,以搜索并连接附近的设备。\";\n" +
- "\"NSBluetoothPeripheralUsageDescription\" = \"请允许应用使用蓝牙,以搜索并连接附近的设备。\";\n" +
- "\"NSLocationWhenInUseUsageDescription\" = \"应用需要在使用期间访问您的位置信息,用于确定排行榜归属区域,并在蓝牙连接时搜索附近设备。例如,我们会根据您的当前位置将您的成绩归入对应地区的排行榜,并帮助您发现和连接周边蓝牙设备。\";\n" +
- "\"NSLocationAlwaysUsageDescription\" = \"应用需要持续访问您的位置信息,用于确定排行榜归属区域,并在后台运行时搜索附近设备。例如,我们会根据您的当前位置将您的成绩归入对应地区的排行榜,并帮助您发现和连接周边蓝牙设备。\";\n"
- );
- // 英文 en.lproj
- string enPath = Path.Combine(localizedFolderPath, "en.lproj");
- if (!Directory.Exists(enPath))
- Directory.CreateDirectory(enPath);
- File.WriteAllText(Path.Combine(enPath, "InfoPlist.strings"),
- "\"NSCameraUsageDescription\" = \"The app requires your permission to access the camera.\";\n" +
- "\"NSPhotoLibraryUsageDescription\" = \"The app requires your permission to access your photo library.\";\n" +
- "\"NSBluetoothAlwaysUsageDescription\" = \"Allow this app to use Bluetooth to search for and connect to devices.\";\n" +
- "\"NSBluetoothPeripheralUsageDescription\" = \"Allow this app to use Bluetooth to search for and connect to devices.\";\n" +
- "\"NSLocationWhenInUseUsageDescription\" = \"The app requires your location to determine your region for leaderboard assignment and to discover nearby devices during Bluetooth connections. For example, your achievements will be placed on the leaderboard of your current region, and the app will help you find and connect to nearby Bluetooth devices.\";\n" +
- "\"NSLocationAlwaysUsageDescription\" = \"The app requires continuous access to your location to determine your region for leaderboard assignment and to discover nearby devices even when running in the background. For example, your achievements will be placed on the leaderboard of your current region, and the app will help you find and connect to nearby Bluetooth devices.\";\n"
- );
- }
- public static void AddLocalizedResourcesToXcode(string path)
- {
- string projPath = PBXProject.GetPBXProjectPath(path);
- PBXProject proj = new PBXProject();
- proj.ReadFromString(File.ReadAllText(projPath));
- string targetGuid = proj.GetUnityMainTargetGuid();
- string localizedFolderPath = Path.Combine(path, "LocalizedInfoPlist");
- if (Directory.Exists(localizedFolderPath))
- {
- foreach (var dir in Directory.GetDirectories(localizedFolderPath))
- {
- string relativePath = "LocalizedInfoPlist/" + Path.GetFileName(dir);
- proj.AddFolderReference(relativePath, relativePath);
- string stringsFile = Path.Combine(relativePath, "InfoPlist.strings");
- if (File.Exists(Path.Combine(path, stringsFile)))
- {
- proj.AddFileToBuild(targetGuid, proj.AddFile(stringsFile, stringsFile));
- }
- }
- }
- File.WriteAllText(projPath, proj.WriteToString());
- }
- #endif
- }
|