博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity3D研究院之IOS全自动编辑framework、plist、oc代码
阅读量:2305 次
发布时间:2019-05-09

本文共 7116 字,大约阅读时间需要 23 分钟。

转自:http://blog.csdn.net/cbbbc/article/details/50437131

其他参考链接:https://github.com/onevcat/XUPorter

https://onevcat.com/2012/12/xuporter/

Unity打IOS时会先生成一个Xcode工程,如果你需要增加一些第三方的framework那么需要手动一条一条的添加,这太烦了。。而且可能你还需要修改Plist文件,甚至还可能要修改unity自动生成的oc代码,每次打包都要修改的话,那太累了。。这篇文章就是全自动打包的第一步。。建议使用,我在它的基础上拓展了两个类,一个用来修改plist,一个用来修改unity生成出来的OC代码。文章的最后我会给出代码。。

那么我用一个比较变态的SDK举个例子ShareSDK,它就需要自动添加framework,修改plist,还有要修改oc的代码。第一步打开XUPorter/Mods/share.projmods 文件。

[cpp]   
  1. {  
  2.     "group""share",  
  3.     "libs": ["libicucore.dylib","libz.1.2.5.dylib"],  
  4.     "frameworks": [  
  5.                     "SystemConfiguration.framework",  
  6.      "QuartzCore.framework",  
  7.      "CoreTelephony.framework",  
  8.      "Security.framework",  
  9.                     "AdSupport.framework:optional",  
  10.                     "MessageUI.framework",  
  11.                      "StoreKit.framework",  
  12.                      "AudioToolbox.framework",  
  13.                      "QuartzCore.framework"  
  14.         ],  
  15.     "headerpaths": [],  
  16.     "files":   [  
  17.                 "ShareSDK/Connection/SinaWeiboConnection.framework",  
  18.                 "ShareSDK/Connection/WeChatConnection.framework",  
  19.                 "ShareSDK/Core/AGCommon.framework",  
  20.                 "ShareSDK/Core/ShareSDKCoreService.framework",  
  21.                 "ShareSDK/ShareSDK.framework"  
  22.                 ],  
  23.     "folders": ["ShareSDK/"],      
  24.     "excludes": ["^.*.meta$""^.*.mdown$""^.*.pdf$"],  
  25.     "linker_flags": []  
  26. }  

frameworks分成两种,一种是系统自带的framework还有一种是第三方的framework。 “frameworks”节点里面放的是系统自带的frameworks。”files”节点里面放的是第三方做出来的framework。 尤其是第三方的framework如果位置放的不对,就不会被xcode所引用的!切记切记!!

folders:可以把某个文件夹下的所有文件拷贝在xcode工程里面,一般sdk都会附带一些oc的代码文件,最好通过folders把oc的代码拷贝在工程里面。或者你也可以把oc的代码放在plugins下面,同样打包的时候也会拷贝进xcode工程。

unity打完IOS或者包以后会自动回调一个静态方法。

[cpp]   
  1. [PostProcessBuild (100)]  
  2.  public static void OnPostProcessBuild (BuildTarget target, string pathToBuiltProject)  

自动添加framework的原理其实就是等包打完以后,在这个方法里面进行文件的操作,把需要的framework plist oc 代码拷贝进去,或者修改它们。。

 

[cpp]   
  1. using UnityEngine;  
  2.    
  3. #if UNITY_EDITOR  
  4. using UnityEditor;  
  5. using UnityEditor.Callbacks;  
  6. using UnityEditor.XCodeEditor;  
  7. using System.Xml;  
  8. #endif  
  9. using System.IO;  
  10.    
  11. public static class XCodePostProcess  
  12. {  
  13.     #if UNITY_EDITOR  
  14.     [PostProcessBuild (100)]  
  15.     public static void OnPostProcessBuild (BuildTarget target, string pathToBuiltProject)  
  16.     {  
  17.         if (target != BuildTarget.iPhone) {  
  18.             Debug.LogWarning ("Target is not iPhone. XCodePostProcess will not run");  
  19.             return;  
  20.         }  
  21.    
  22.         //得到xcode工程的路径  
  23.         string path = Path.GetFullPath (pathToBuiltProject);  
  24.    
  25.         // Create a new project object from build target  
  26.         XCProject project = new XCProject (pathToBuiltProject);  
  27.    
  28.         // Find and run through all projmods files to patch the project.  
  29.         // Please pay attention that ALL projmods files in your project folder will be excuted!  
  30.         //在这里面把frameworks添加在你的xcode工程里面  
  31.         string[] files = Directory.GetFiles (Application.dataPath, "*.projmods", SearchOption.AllDirectories);  
  32.         foreach (string file in files) {  
  33.             project.ApplyMod (file);  
  34.         }  
  35.    
  36.         //增加一个编译标记。。没有的话sharesdk会报错。。  
  37.         project.AddOtherLinkerFlags("-licucore");  
  38.    
  39.         //设置签名的证书, 第二个参数 你可以设置成你的证书  
  40. project.overwriteBuildSetting ("CODE_SIGN_IDENTITY""xxxxxx""Release");  
  41.         project.overwriteBuildSetting ("CODE_SIGN_IDENTITY""xxxxxx""Debug");  
  42.    
  43.         // 编辑plist 文件  
  44.         EditorPlist(path);  
  45.    
  46.         //编辑代码文件  
  47.         EditorCode(path);  
  48.    
  49.         // Finally save the xcode project  
  50.         project.Save ();  
  51.    
  52.     }  
  53.    
  54.     private static void EditorPlist(string filePath)  
  55.     {  
  56.    
  57.         XCPlist list =new XCPlist(filePath);  
  58.         string bundle = "com.yusong.momo";  
  59.    
  60.         string PlistAdd = @"    
  61.             <key>CFBundleURLTypes</key>  
  62.             <array>  
  63.             <dict>  
  64.             <key>CFBundleTypeRole</key>  
  65.             <string>Editor</string>  
  66.             <key>CFBundleURLIconFile</key>  
  67.             <string>Icon@2x</string>  
  68.             <key>CFBundleURLName</key>  
  69.             <string>"+bundle+@"</string>  
  70.             <key>CFBundleURLSchemes</key>  
  71.             <array>  
  72.             <string>ww123456</string>  
  73.             </array>  
  74.             </dict>  
  75.             </array>";  
  76.    
  77.         //在plist里面增加一行  
  78.         list.AddKey(PlistAdd);  
  79.         //在plist里面替换一行  
  80.         list.ReplaceKey("<string>com.yusong.${PRODUCT_NAME}</string>","<string>"+bundle+"</string>");  
  81.         //保存  
  82.         list.Save();  
  83.    
  84.     }  
  85.    
  86.     private static void EditorCode(string filePath)  
  87.     {  
  88. //读取UnityAppController.mm文件  
  89.         XClass UnityAppController = new XClass(filePath + "/Classes/UnityAppController.mm");  
  90.    
  91.         //在指定代码后面增加一行代码  
  92.         UnityAppController.WriteBelow("#include \"PluginBase/AppDelegateListener.h\"","#import <ShareSDK/ShareSDK.h>");  
  93.    
  94.         //在指定代码中替换一行  
  95.         UnityAppController.Replace("return YES;","return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:nil];");  
  96.    
  97.         //在指定代码后面增加一行  
  98.         UnityAppController.WriteBelow("UnityCleanup();\n}","- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url\r{\r    return [ShareSDK handleOpenURL:url wxDelegate:nil];\r}");  
  99.    
  100.     }  
  101.    
  102.     #endif  
  103. }  

在回到ShareSDK上,在接微信平台的时候,它们需要在pList 里面增加URL types选项,这里我通过XCPlist增加一行 或者替换一行。

 

[cpp]   
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.IO;  
  5.    
  6. namespace UnityEditor.XCodeEditor  
  7. {  
  8.     public partial class XCPlist : System.IDisposable  
  9.     {  
  10.    
  11. private string filePath;  
  12. List<string> contents = new List<string>();  
  13. public XCPlist(string fPath)  
  14. {  
  15.             filePath = Path.Combine( fPath, "info.plist" );  
  16.             if( !System.IO.File.Exists( filePath ) ) {  
  17.                 Debug.LogError( filePath +"路径下文件不存在" );  
  18.     return;  
  19. }  
  20.    
  21.             FileInfo projectFileInfo = new FileInfo( filePath );  
  22. StreamReader sr = projectFileInfo.OpenText();  
  23. while (sr.Peek() >= 0)  
  24. {  
  25. contents.Add(sr.ReadLine());  
  26. }  
  27. sr.Close();  
  28.    
  29. }  
  30. public void AddKey(string key)  
  31. {  
  32. if(contents.Count < 2)  
  33. return;  
  34. contents.Insert(contents.Count - 2,key);  
  35.    
  36. }  
  37.    
  38. public void ReplaceKey(string key,string replace){  
  39. for(int i = 0;i < contents.Count;i++){  
  40. if(contents[i].IndexOf(key) != -1){  
  41. contents[i] = contents[i].Replace(key,replace);  
  42. }  
  43. }  
  44. }  
  45.    
  46. public void Save()  
  47. {  
  48.             StreamWriter saveFile = File.CreateText(filePath);  
  49. foreach(string line in contents)  
  50. saveFile.WriteLine(line);  
  51. saveFile.Close();  
  52.      }  
  53.    
  54. public void Dispose()  
  55. {  
  56.    
  57. }  
  58.     }  
  59. }  

ShareSDK在接入微信平台的时候 必须修改Unity生成的UnityAppController.mm 文件,这里我通过 XClass 自动修改UnityAppController.mm生成的代码。 主要是增加代码和替换代码 两部分。。

[cpp]   
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.IO;  
  5.    
  6. namespace UnityEditor.XCodeEditor  
  7. {  
  8. public partial class XClass : System.IDisposable  
  9. {  
  10.    
  11.         private string filePath;  
  12.    
  13. public XClass(string fPath)  
  14. {  
  15.             filePath = fPath;  
  16. if( !System.IO.File.Exists( filePath ) ) {  
  17. Debug.LogError( filePath +"路径下文件不存在" );  
  18. return;  
  19. }  
  20. }  
  21.    
  22.         public void WriteBelow(string below, string text)  
  23.         {  
  24.             StreamReader streamReader = new StreamReader(filePath);  
  25.             string text_all = streamReader.ReadToEnd();  
  26.             streamReader.Close();  
  27.    
  28.             int beginIndex = text_all.IndexOf(below);  
  29.             if(beginIndex == -1){  
  30.                 Debug.LogError(filePath +"中没有找到标致"+below);  
  31.                 return;  
  32.             }  
  33.    
  34.             int endIndex = text_all.LastIndexOf("\n", beginIndex + below.Length);  
  35.    
  36.             text_all = text_all.Substring(0, endIndex) + "\n"+text+"\n" + text_all.Substring(endIndex);  
  37.    
  38.             StreamWriter streamWriter = new StreamWriter(filePath);  
  39.             streamWriter.Write(text_all);  
  40.             streamWriter.Close();  
  41.         }  
  42.    
  43.         public void Replace(string below, string newText)  
  44.         {  
  45.             StreamReader streamReader = new StreamReader(filePath);  
  46.             string text_all = streamReader.ReadToEnd();  
  47.             streamReader.Close();  
  48.    
  49.             int beginIndex = text_all.IndexOf(below);  
  50.             if(beginIndex == -1){  
  51.                 Debug.LogError(filePath +"中没有找到标致"+below);  
  52.                 return;  
  53.             }  
  54.    
  55.             text_all =  text_all.Replace(below,newText);  
  56.             StreamWriter streamWriter = new StreamWriter(filePath);  
  57.             streamWriter.Write(text_all);  
  58.             streamWriter.Close();  
  59.    
  60.         }  
  61.    
  62.         public void Dispose()  
  63.         {  
  64.    
  65.         }  
  66. }  
  67. }  


像这样,我就可以把unity生成的代码修改了。。

最后是工程地址:

建议大家把工程下载下来看看一基本上就明白它的工作原理了。如果你掌握者本篇文章的知识 那么恭喜你 自动化打包就已经完成了一半。。 现在我们已经可以自动化生成 xcode工程了,等有时间的话我会把下一半shell 自动化打包.ipa的方法在整理出来。。

原文地址:http://www.xuanyusong.com/archives/2720/

你可能感兴趣的文章
文件操作
查看>>
NSData
查看>>
日期操作
查看>>
iOS的三种弹框
查看>>
UIApplication和程序启动过程
查看>>
cocoaPods安装2017 以及遇到的坑
查看>>
Android中自定义可以选择中文的NumberPicker屏蔽弹出软键盘
查看>>
Scrapy教程——搭建环境、创建项目、爬取内容、保存文件(txt)
查看>>
SQL SERVER 2012 附加数据AdventureWorks2012失败解决方案
查看>>
C++内联函数(inline)的工作原理与例子
查看>>
Eclipse中使用svn主要命令的详细介绍
查看>>
MS第二题解题思路
查看>>
第一个mpi程序in linux
查看>>
epoll 详解
查看>>
Hadoop 面试题
查看>>
【Day22】mysql数据库的优化(一版)
查看>>
【Day23】几道值得研究注意的php相关问题(一)
查看>>
【Day24】几道值得研究注意的php相关问题(二)
查看>>
php源码之路第三章第六节( 变量的生命周期之变量的赋值和销毁)
查看>>
【Day35】浅谈PHP拦截器之__set()与__get()的理解与使用方法
查看>>