Unity3D研究院之IOS全自动编辑framework、plist、oc代码
Unity打IOS时会先生成一个Xcode工程,如果你需要增加一些第三方的framework那么需要手动一条一条的添加,这太烦了。。而且可能你还需要修改Plist文件,甚至还可能要修改unity自动生成的oc代码,每次打包都要修改的话,那太累了。。这篇文章就是全自动打包的第一步。。建议使用XUPorter,我在它的基础上拓展了两个类,一个用来修改plist,一个用来修改unity生成出来的OC代码。文章的最后我会给出代码。。
那么我用一个比较变态的SDK举个例子ShareSDK,它就需要自动添加framework,修改plist,还有要修改oc的代码。第一步打开XUPorter/Mods/share.projmods 文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
{
"group": "share",
"libs": ["libicucore.dylib","libz.1.2.5.dylib"],
"frameworks": [
"SystemConfiguration.framework",
"QuartzCore.framework",
"CoreTelephony.framework",
"Security.framework",
"AdSupport.framework:optional",
"MessageUI.framework",
"StoreKit.framework",
"AudioToolbox.framework",
"QuartzCore.framework"
],
"headerpaths": [],
"files": [
"ShareSDK/Connection/SinaWeiboConnection.framework",
"ShareSDK/Connection/WeChatConnection.framework",
"ShareSDK/Core/AGCommon.framework",
"ShareSDK/Core/ShareSDKCoreService.framework",
"ShareSDK/ShareSDK.framework"
],
"folders": ["ShareSDK/"],
"excludes": ["^.*.meta$", "^.*.mdown$", "^.*.pdf$"],
"linker_flags": []
}
|
frameworks分成两种,一种是系统自带的framework还有一种是第三方的framework。 “frameworks”节点里面放的是系统自带的frameworks。”files”节点里面放的是第三方做出来的framework。 尤其是第三方的framework如果位置放的不对,就不会被xcode所引用的!切记切记!!
folders:可以把某个文件夹下的所有文件拷贝在xcode工程里面,一般sdk都会附带一些oc的代码文件,最好通过folders把oc的代码拷贝在工程里面。或者你也可以把oc的代码放在plugins下面,同样打包的时候也会拷贝进xcode工程。
unity打完IOS或者Android包以后会自动回调一个静态方法。
1
2
|
[PostProcessBuild (100)]
public static void OnPostProcessBuild (BuildTarget target, string pathToBuiltProject)
|
自动添加framework的原理其实就是等包打完以后,在这个方法里面进行文件的操作,把需要的framework plist oc 代码拷贝进去,或者修改它们。。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.XCodeEditor;
using System.Xml;
#endif
using System.IO;
public static class XCodePostProcess
{
#if UNITY_EDITOR
[PostProcessBuild (100)]
public static void OnPostProcessBuild (BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iPhone) {
Debug.LogWarning ("Target is not iPhone. XCodePostProcess will not run");
return;
}
//得到xcode工程的路径
string path = Path.GetFullPath (pathToBuiltProject);
// Create a new project object from build target
XCProject project = new XCProject (pathToBuiltProject);
// Find and run through all projmods files to patch the project.
// Please pay attention that ALL projmods files in your project folder will be excuted!
//在这里面把frameworks添加在你的xcode工程里面
string[] files = Directory.GetFiles (Application.dataPath, "*.projmods", SearchOption.AllDirectories);
foreach (string file in files) {
project.ApplyMod (file);
}
//增加一个编译标记。。没有的话sharesdk会报错。。
project.AddOtherLinkerFlags("-licucore");
//设置签名的证书, 第二个参数 你可以设置成你的证书
project.overwriteBuildSetting ("CODE_SIGN_IDENTITY", "xxxxxx", "Release");
project.overwriteBuildSetting ("CODE_SIGN_IDENTITY", "xxxxxx", "Debug");
// 编辑plist 文件
EditorPlist(path);
//编辑代码文件
EditorCode(path);
// Finally save the xcode project
project.Save ();
}
private static void EditorPlist(string filePath)
{
XCPlist list =new XCPlist(filePath);
string bundle = "com.yusong.momo";
string PlistAdd = @"
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLIconFile</key>
<string>Icon@2x</string>
<key>CFBundleURLName</key>
<string>"+bundle+@"</string>
<key>CFBundleURLSchemes</key>
<array>
<string>ww123456</string>
</array>
</dict>
</array>";
//在plist里面增加一行
list.AddKey(PlistAdd);
//在plist里面替换一行
list.ReplaceKey("<string>com.yusong.${PRODUCT_NAME}</string>","<string>"+bundle+"</string>");
//保存
list.Save();
}
private static void EditorCode(string filePath)
{
//读取UnityAppController.mm文件
XClass UnityAppController = new XClass(filePath + "/Classes/UnityAppController.mm");
//在指定代码后面增加一行代码
UnityAppController.WriteBelow("#include \"PluginBase/AppDelegateListener.h\"","#import <ShareSDK/ShareSDK.h>");
//在指定代码中替换一行
UnityAppController.Replace("return YES;","return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:nil];");
//在指定代码后面增加一行
UnityAppController.WriteBelow("UnityCleanup();\n}","- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url\r{\r return [ShareSDK handleOpenURL:url wxDelegate:nil];\r}");
}
#endif
}
|
在回到ShareSDK上,在接微信平台的时候,它们需要在pList 里面增加URL types选项,这里我通过XCPlist增加一行 或者替换一行。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace UnityEditor.XCodeEditor
{
public partial class XCPlist : System.IDisposable
{
private string filePath;
List<string> contents = new List<string>();
public XCPlist(string fPath)
{
filePath = Path.Combine( fPath, "info.plist" );
if( !System.IO.File.Exists( filePath ) ) {
Debug.LogError( filePath +"路径下文件不存在" );
return;
}
FileInfo projectFileInfo = new FileInfo( filePath );
StreamReader sr = projectFileInfo.OpenText();
while (sr.Peek() >= 0)
{
contents.Add(sr.ReadLine());
}
sr.Close();
}
public void AddKey(string key)
{
if(contents.Count < 2)
return;
contents.Insert(contents.Count - 2,key);
}
public void ReplaceKey(string key,string replace){
for(int i = 0;i < contents.Count;i++){
if(contents[i].IndexOf(key) != -1){
contents[i] = contents[i].Replace(key,replace);
}
}
}
public void Save()
{
StreamWriter saveFile = File.CreateText(filePath);
foreach(string line in contents)
saveFile.WriteLine(line);
saveFile.Close();
}
public void Dispose()
{
}
}
}
|
ShareSDK在接入微信平台的时候 必须修改Unity生成的UnityAppController.mm 文件,这里我通过 XClass 自动修改UnityAppController.mm生成的代码。 主要是增加代码和替换代码 两部分。。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace UnityEditor.XCodeEditor
{
public partial class XClass : System.IDisposable
{
private string filePath;
public XClass(string fPath)
{
filePath = fPath;
if( !System.IO.File.Exists( filePath ) ) {
Debug.LogError( filePath +"路径下文件不存在" );
return;
}
}
public void WriteBelow(string below, string text)
{
StreamReader streamReader = new StreamReader(filePath);
string text_all = streamReader.ReadToEnd();
streamReader.Close();
int beginIndex = text_all.IndexOf(below);
if(beginIndex == -1){
Debug.LogError(filePath +"中没有找到标致"+below);
return;
}
int endIndex = text_all.LastIndexOf("\n", beginIndex + below.Length);
text_all = text_all.Substring(0, endIndex) + "\n"+text+"\n" + text_all.Substring(endIndex);
StreamWriter streamWriter = new StreamWriter(filePath);
streamWriter.Write(text_all);
streamWriter.Close();
}
public void Replace(string below, string newText)
{
StreamReader streamReader = new StreamReader(filePath);
string text_all = streamReader.ReadToEnd();
streamReader.Close();
int beginIndex = text_all.IndexOf(below);
if(beginIndex == -1){
Debug.LogError(filePath +"中没有找到标致"+below);
return;
}
text_all = text_all.Replace(below,newText);
StreamWriter streamWriter = new StreamWriter(filePath);
streamWriter.Write(text_all);
streamWriter.Close();
}
public void Dispose()
{
}
}
}
|
像这样,我就可以把unity生成的代码修改了。。
最后是工程地址:http://pan.baidu.com/s/1i3gLDTN
建议大家把工程下载下来看看一基本上就明白它的工作原理了。如果你掌握者本篇文章的知识 那么恭喜你 自动化打包就已经完成了一半。。 现在我们已经可以自动化生成 xcode工程了,等有时间的话我会把下一半shell 自动化打包.ipa的方法在整理出来。。
Unity3D研究院之IOS全自动编辑framework、plist、oc代码的更多相关文章
- Unity3D研究院之IOS全自动打包生成ipa
接着上一篇文章, 自动生成framework,这篇文章我把shell自动化打包ipa整理了一下,希望大家喜欢,嘿嘿.. 建议大家先看一下上一篇文章.http://www.xuanyusong.com/ ...
- Unity3D研究院之IOS本地消息通知LocalNotification的使用(六十七)
http://www.xuanyusong.com/archives/2632 现在的游戏里一般都会有本地消息,比如每天定时12点或者下午6点告诉玩家进入游戏领取体力.这种东西没必要服务器去推送 ...
- Unity3D研究院之IOS本地消息通知LocalNotification的使用
原地址:http://www.xuanyusong.com/archives/2632 现在的游戏里一般都会有本地消息,比如每天定时12点或者下午6点告诉玩家进入游戏领取体力.这种东西没必要服务器 ...
- 【转载】Unity3D研究院之IOS自定义游戏摇杆与飞机平滑的移动
移动开发游戏中使用到的触摸游戏摇杆在iPhone上是非常普遍的,毕竟是全触摸屏手机,今天MOMO 通过一个小例子和大家讨论Unity3D 中如何自定义一个漂亮的全触摸游戏摇杆. 值得高兴 ...
- 【转载】Unity3D研究院之IOS触摸屏手势控制镜头旋转与缩放
前几篇文章介绍了很多Unity3D引擎自身的一些问题, 今天我们在回到IOS设备上讨论一些触摸屏幕手势,本章的目标是通过触摸iPhone屏幕手势 实现模型左右的旋转,与模型的缩放. 大家想一想模型的旋 ...
- 【转载】Unity3D研究院之IOS&Andoird使用Sharesdk遇到的坑
这两天研究了一下ShareSDK,说实话挺好用的,但是还是有点坑的地方.那么雨松MOMO写下博文记录一下来我遇到的坑,嘿嘿. 大部分内容它的文档上已经说的很清楚了. http://wiki.share ...
- (转)Unity3D研究院之IOS&Android收集Log文件
转自:http://www.xuanyusong.com/archives/2477 有段时间没有写过文章了,不知道大伙儿还记得雨松MOMO吗? 嘿嘿. 开发项目的时候尤其在处理与服务器交互这块,如果 ...
- Unity3D研究院之IOS Android支持中文与本地文件的读取写
前几天有个朋友问我为什么在IOS平台中可以正常的读写文件可是在Android平台中就无法正常的读写.当时因为在上班所以我没时间来帮他解决,晚上回家后我就拿起安卓手机真机调试很快就定位问题所在,原 ...
- Unity3D研究院之IOS&Android收集Log文件(六十二)
开发项目的时候尤其在处理与服务器交互这块,如果服务端程序看不到客户端请求的Log信息,那么无法修改BUG.在Windows上Unity会自动讲Log文件写入本地,但是在IOS和Android上确没有这 ...
随机推荐
- SilverLight命名空间详解-新手入门
1.核心命名空间 1.xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"是silverlight的核 ...
- 用Windows API函数(CreateFile/ReadFile/WriteFile/CloseHandle)完成文件拷贝程序(初级版)
文件拷贝程序 程序类型:Console 参数:源文件名 目的文件名 要求:1.只能使用Windows API函数(CreateFile/ReadFile/WriteFile/CloseHandle ...
- iOS 非ARC基本内存管理系列总结6 -设计微博模型
设计简单的微博模型:用User类和Status类来模拟实现 在非ARC机制下有两种方式,两者没有太大的区别之所以写了两种只是为了方便学习和对比两种写法! 第一种:没有使用atuorelease和自动释 ...
- request.getSession();为什么不用response儿用request!
首先回答为什么分别是response和request这两个内置对象.你得先明白你通过获取对象是做什么用的,是往哪用的.第一个PrintWriter out=response.getWriter()是想 ...
- Javascript this 解析
Javascript中,this是一个非常有用的关键字, this是在运行时基于函数的运行环境绑定的,但是,如果使用的时候不注意,很容易就出错了. ECMAScript Standard对this的定 ...
- VMware Workstation中linux(centos)与windows7共享文件夹
引用网站有: http://www.jb51.net/LINUXjishu/161994.html http://www.cnblogs.com/xiehy/archive/2011/12/19/22 ...
- C# 网卡IP(网上资料整理)
//设置对外访问所使用网卡的IP string sendingIp = "192.168.0.1"; //设置对外访问所使用的端口 ; Uri uri = new Uri(&quo ...
- RHEL安装docker-compose
Note that Compose 1.5.2 requires Docker 1.7.1 or later. pip install docker-compose==1.5.2 Note that ...
- QA如何增强网站建设公司竞争力
在上一篇文章<QA在网站建设公司中的作用>中我们已经详细说了QA的作用,不过有一点没有明确说明,也就是只有在超高速发展的网站建设公司中才会充分体现QA的价值.这并不是说在发展稳定的公司或低 ...
- 动态创建MySQL数据库
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...