Unity 5.x 打包Xcode工程自動添加framework炎疆、plist、lib壹士、OC代碼

功能:
使用Unity 5.x自帶API打包Xcode工程自動添加framework磷雇、plist偿警、lib躏救、和插入OC代碼

參考文章:
http://www.xuanyusong.com/archives/4026
http://www.xuanyusong.com/archives/2720
http://www.reibang.com/p/f347e00abb9c
http://www.reibang.com/p/dbd7c4b205b0

官方API:
https://docs.unity3d.com/ScriptReference/iOS.Xcode.PBXProject.html

開發(fā)環(huán)境

OS:macOS Sierra 10.12.2
Unity:5.3.6(p8)
Xcode:8.2

特別注意

以下的代碼文件必須放在Unity工程下的Editor文件夾內(nèi)才能生效否則會報錯!

PBXProjectDemo.cs

using System.IO;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.iOS.Xcode;
using UnityEditor.Callbacks;
using UnityEditor.XCodeEditor;
#endif
using System.Collections;

public class PBXProjectDemo
{
    //該屬性是在build完成后,被調(diào)用的callback
    [PostProcessBuildAttribute(0)]
    public static void OnPostprocessBuild(BuildTarget buildTarget, string pathToBuiltProject)
    {
        // BuildTarget需為iOS
        if (buildTarget != BuildTarget.iOS)
            return;

        // 初始化
        var projectPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
        PBXProject pbxProject = new PBXProject();
        pbxProject.ReadFromFile(projectPath);
        string targetGuid = pbxProject.TargetGuidByName("Unity-iPhone");

        // 添加flag
        pbxProject.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-ObjC");
        // 關(guān)閉Bitcode
        pbxProject.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");

         // 添加framwrok
        pbxProject.AddFrameworkToProject(targetGuid, "Security.framework", false);
        pbxProject.AddFrameworkToProject(targetGuid, "CoreTelephony.framework", false);
        pbxProject.AddFrameworkToProject(targetGuid, "SystemConfiguration.framework", false);
        pbxProject.AddFrameworkToProject(targetGuid, "CoreGraphics.framework", false);
        pbxProject.AddFrameworkToProject(targetGuid, "ImageIO.framework", false);
        pbxProject.AddFrameworkToProject(targetGuid, "CoreData.framework", false);
        
        //添加lib
        AddLibToProject(pbxProject, targetGuid, "libsqlite3.tbd");
        AddLibToProject(pbxProject, targetGuid, "libc++.tbd");
        AddLibToProject(pbxProject, targetGuid, "libz.tbd");

        // 應(yīng)用修改
        File.WriteAllText(projectPath, pbxProject.WriteToString());

        // 修改Info.plist文件
        var plistPath = Path.Combine(pathToBuiltProject, "Info.plist");
        var plist = new PlistDocument();
        plist.ReadFromFile(plistPath);

        // 插入URL Scheme到Info.plsit(理清結(jié)構(gòu))
        var array = plist.root.CreateArray("CFBundleURLTypes");
        //插入dict
        var urlDict = array.AddDict();
        urlDict.SetString("CFBundleTypeRole", "Editor");
        //插入array
        var urlInnerArray = urlDict.CreateArray("CFBundleURLSchemes");
        urlInnerArray.AddString("blablabla");
        // 應(yīng)用修改
        plist.WriteToFile(plistPath);

        //插入代碼
        //讀取UnityAppController.mm文件
        string unityAppControllerPath = pathToBuiltProject + "/Classes/UnityAppController.mm";
        XClass UnityAppController = new XClass(unityAppControllerPath);

        //在指定代碼后面增加一行代碼
        UnityAppController.WriteBelow("#include \"PluginBase/AppDelegateListener.h\"", "#import <UMSocialCore/UMSocialCore.h>");

        string newCode = "\n" +
                   "    [[UMSocialManager defaultManager] openLog:YES];\n" +
                   "    [UMSocialGlobal shareInstance].type = @\"u3d\";\n" +
                   "    [[UMSocialManager defaultManager] setUmSocialAppkey:@\"" +"\"];\n" +
                   "    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@\""+"\" appSecret:@\""+ "\" redirectURL:@\"http://mobile.umeng.com/social\"];\n" +
                   "    \n"
                   ;
        //在指定代碼后面增加一大行代碼
        UnityAppController.WriteBelow("http:// if you wont use keyboard you may comment it out at save some memory", newCode);
        
    }

        //添加lib方法
    static void AddLibToProject(PBXProject inst, string targetGuid, string lib)
    {
        string fileGuid = inst.AddFile("usr/lib/" + lib, "Frameworks/" + lib, PBXSourceTree.Sdk);
        inst.AddFileToBuild(targetGuid, fileGuid);
    }
}

XClass.cs

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()
        {

        }
    }
}

XCPlist.cs

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()
        {

        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末盒使,一起剝皮案震驚了整個濱河市崩掘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌少办,老刑警劉巖苞慢,帶你破解...
    沈念sama閱讀 211,376評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異英妓,居然都是意外死亡挽放,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評論 2 385
  • 文/潘曉璐 我一進店門蔓纠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來辑畦,“玉大人,你說我怎么就攤上這事腿倚〈砍觯” “怎么了?”我有些...
    開封第一講書人閱讀 156,966評論 0 347
  • 文/不壞的土叔 我叫張陵敷燎,是天一觀的道長暂筝。 經(jīng)常有香客問我,道長硬贯,這世上最難降的妖魔是什么焕襟? 我笑而不...
    開封第一講書人閱讀 56,432評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮饭豹,結(jié)果婚禮上胧洒,老公的妹妹穿的比我還像新娘。我一直安慰自己墨状,他們只是感情好卫漫,可當我...
    茶點故事閱讀 65,519評論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著肾砂,像睡著了一般列赎。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上镐确,一...
    開封第一講書人閱讀 49,792評論 1 290
  • 那天包吝,我揣著相機與錄音,去河邊找鬼源葫。 笑死诗越,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的息堂。 我是一名探鬼主播嚷狞,決...
    沈念sama閱讀 38,933評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼块促,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了床未?” 一聲冷哼從身側(cè)響起竭翠,我...
    開封第一講書人閱讀 37,701評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎薇搁,沒想到半個月后斋扰,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,143評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡啃洋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,488評論 2 327
  • 正文 我和宋清朗相戀三年传货,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片宏娄。...
    茶點故事閱讀 38,626評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡损离,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出绝编,到底是詐尸還是另有隱情僻澎,我是刑警寧澤,帶...
    沈念sama閱讀 34,292評論 4 329
  • 正文 年R本政府宣布十饥,位于F島的核電站窟勃,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏逗堵。R本人自食惡果不足惜秉氧,卻給世界環(huán)境...
    茶點故事閱讀 39,896評論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蜒秤。 院中可真熱鬧汁咏,春花似錦、人聲如沸作媚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽纸泡。三九已至漂问,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間女揭,已是汗流浹背蚤假。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評論 1 265
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留吧兔,地道東北人磷仰。 一個月前我還...
    沈念sama閱讀 46,324評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像境蔼,于是被迫代替她去往敵國和親灶平。 傳聞我的和親對象是個殘疾皇子伺通,可洞房花燭夜當晚...
    茶點故事閱讀 43,494評論 2 348

推薦閱讀更多精彩內(nèi)容