private void OnFocus() { Debug.Log("OnFocus"); if (mConfig != null) { return; } string path = ConfigPath(); if (File.Exists(path)) { string content = File.ReadAllText(path); mConfig = JsonUtility.FromJson <iOSConfig>(content); } else { mConfig = new iOSConfig(); } }
/// <summary> /// 在生成xcode工程后,自动将一些ios配置写入xcode工程 /// </summary> /// <param name="buildTarget"></param> /// <param name="path"></param> public static void OnPostprocessBuild_ios(BuildTarget buildTarget, string path) { //#if UNITY_IOS //Debug.Log ("path: " + path); if (buildTarget != BuildTarget.iOS) { return; } // 读取配置,一定要保证配置的正确性 string configPath = ConfigPath(); string configContent = File.ReadAllText(configPath); iOSConfig config = JsonUtility.FromJson <iOSConfig>(configContent); #region 修改工程 string projPath = PBXProject.GetPBXProjectPath(path); Debug.Log("projPath: " + projPath); PBXProject proj = new PBXProject(); string fileText = File.ReadAllText(projPath); proj.ReadFromString(fileText); //Debug.Log ("fileText: " + fileText); string targetName = PBXProject.GetUnityTargetName();//Unity-iPhone string targetGuid = proj.TargetGuidByName(targetName); //Debug.Log ("targetName: " + targetName); //Debug.Log ("targetGuid: " + targetGuid); // BuildPropertys proj.SetBuildProperty(targetGuid, "CFBundleDevelopmentRegion", config.CFBundleDevelopmentRegion); proj.SetBuildProperty(targetGuid, "ENABLE_BITCODE", config.ENABLE_BITCODE); proj.SetBuildProperty(targetGuid, "GCC_ENABLE_OBJC_EXCEPTIONS", config.GCC_ENABLE_OBJC_EXCEPTIONS); proj.SetBuildProperty(targetGuid, "DEBUG_INFORMATION_FORMAT", config.DEBUG_INFORMATION_FORMAT); proj.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", config.OTHER_LDFLAGS); for (int i = 0; i < config.LIBRARY_SEARCH_PATHS.Count; i++) { proj.AddBuildProperty(targetGuid, "LIBRARY_SEARCH_PATHS", config.LIBRARY_SEARCH_PATHS[i]); } for (int i = 0; i < config.FRAMEWORK_SEARCH_PATHS.Count; i++) { proj.AddBuildProperty(targetGuid, "FRAMEWORK_SEARCH_PATHS", config.FRAMEWORK_SEARCH_PATHS[i]); } // Framework //string fileguid = proj.FindFileGuidByProjectPath("libiconv.2.dylib"); //proj.RemoveFile(fileguid); foreach (iOSFrameWork framework in config.frameWorkList) { proj.AddFrameworkToProject(targetGuid, framework.name, framework.flag); } // save changed File.WriteAllText(projPath, proj.WriteToString()); #endregion #region 修改plist string plistPath = Path.Combine(path, "Info.plist"); Debug.Log("plistPath: " + plistPath); PlistDocument plist = new PlistDocument(); string plistFileText = File.ReadAllText(plistPath); plist.ReadFromString(plistFileText); PlistElementDict rootDict = plist.root; rootDict.SetString("NSCameraUsageDescription", config.NSCameraUsageDescription); rootDict.SetString("NSLocationWhenInUseUsageDescription", config.NSLocationWhenInUseUsageDescription); rootDict.SetString("NSMicrophoneUsageDescription", config.NSMicrophoneUsageDescription); rootDict.SetString("wx_app_id", config.wxAppId); rootDict.SetString("wx_app_secret", config.wxAppSecret); rootDict.SetString("xianliao_app_id", config.xianLiaoAppId); rootDict.SetString("yunwa_app_id", config.yunvaAppId); rootDict.SetString("baidu_app_key", config.baiduAppKey); rootDict.SetString("bugly_app_id", config.buglyAppId); rootDict.SetString("url_schemes_name", config.url_schemes_name); //CFBundleURLTypes PlistElementArray array = rootDict.CreateArray("CFBundleURLTypes"); PlistElementDict dict = array.AddDict(); dict.SetString("CFBundleTypeRole", config.CFBundleTypeRole); dict.SetString("CFBundleURLName", config.CFBundleURLName); PlistElementArray CFBundleURLSchemesArr = dict.CreateArray("CFBundleURLSchemes"); int CFBundleURLSchemesCount = config.CFBundleURLSchemes.Count; for (int i = 0; i < CFBundleURLSchemesCount; i++) { CFBundleURLSchemesArr.AddString(config.CFBundleURLSchemes[i]); } // LSApplicationQueriesSchemes PlistElementArray LSApplicationQueriesSchemesArr = rootDict.CreateArray("LSApplicationQueriesSchemes"); int LSApplicationQueriesSchemesCount = config.LSApplicationQueriesSchemes.Count; for (int i = 0; i < LSApplicationQueriesSchemesCount; i++) { LSApplicationQueriesSchemesArr.AddString(config.LSApplicationQueriesSchemes[i]); } // 保存修改 File.WriteAllText(plistPath, plist.WriteToString()); #endregion #region 修改文件 string applicationPath = Path.Combine(path, "Classes/UnityAppController.mm"); string applicationContent = File.ReadAllText(applicationPath); string oriStr = "_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];"; string destStr = "NSString* deviceVersion = [self getDeviceVersion]; if ([deviceVersion isEqualToString: @\"iPhone10,3\"] || [deviceVersion isEqualToString: @\"iPhone10,6\"]){CGRect bounds = CGRectMake(34, 0, 744, 360);_window = [[UIWindow alloc] initWithFrame: bounds];}else{_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];}"; applicationContent = applicationContent.Replace(oriStr, destStr); oriStr = "#include <mach/mach_time.h>"; destStr = "#include <mach/mach_time.h> \n #import <sys/utsname.h>"; applicationContent = applicationContent.Replace(oriStr, destStr); oriStr = "- (void)applicationDidEnterBackground:(UIApplication*)application"; destStr = " - (NSString*)getDeviceVersion{struct utsname systemInfo; uname(&systemInfo); NSString* deviceVersion = [NSString stringWithCString: systemInfo.machine encoding: NSUTF8StringEncoding];return deviceVersion;} \n - (void)applicationDidEnterBackground:(UIApplication*)application"; applicationContent = applicationContent.Replace(oriStr, destStr); if (File.Exists(applicationPath)) { File.Delete(applicationPath); File.WriteAllText(applicationPath, applicationContent); } #endregion #region 移动文件 string oriDir = Application.dataPath + "/Plugins/ios/bdSDK"; string destDir = path + "/Libraries/Plugins/ios/bdSDK"; Debug.Log("oriDir = " + oriDir + ",destDir =" + destDir); CopyFolder(oriDir, destDir); #endregion #region 修改iCOn string iconName = Path.GetFileName(config.iConPath); string oriIconPath = Application.dataPath + config.iConPath; string destIconPath = path + "/Unity-iPhone/Images.xcassets/AppIcon.appiconset/" + iconName; File.Copy(oriIconPath, destIconPath); string iConConfigPath = path + "/Unity-iPhone/Images.xcassets/AppIcon.appiconset/Contents.json"; string content = File.ReadAllText(iConConfigPath); content = content.Replace("\"images\" : [", "\"images\" : [{\"size\" : \"1024x1024\",\"idiom\" : \"ios-marketing\",\"scale\" : \"1x\",\"filename\" : \"" + iconName + "\"},"); File.WriteAllText(iConConfigPath, content); #endregion }