【小松教你手遊開發】【unity實用技能】unity ios快捷打包

來源:互聯網
上載者:User

標籤:無   實用技能   

ios打包是比較麻煩的,配通一次流程後需要做個筆記把各種外掛程式各種配置在每次打包的時候重新設定,作為一個程式員當然不能接受這麼笨的事情,寫個指令碼讓代碼去實現這些。

首先,介紹一個標記

[PostProcessBuild]

在函數前寫上這個標記,unity在打完包後便會調用這個函數。

所以我們也將用這個標記讓unity在包打成xcode項目以後,去改動xcode裡面的配置。

而unity也有一個預設的函數給我們使用

static void OnPostprocessBuild(BuildTarget target,string pathToBuildProject)  

target就是打包的目標,是android還是ios還是其他,pathToBuildProject是打完包後的路徑。
所以,主函數就是這樣

[PostProcessBuild]  static void OnPostprocessBuild(BuildTarget target,string pathToBuildProject)  {  }  

接下來是添加framework和一些系統外掛程式和設定buildSetting

    public static void ModifyProj(string pathToBuildProject){        string _projPath = PBXProject.GetPBXProjectPath (pathToBuildProject);        PBXProject _pbxProj = new PBXProject ();        _pbxProj.ReadFromString (File.ReadAllText (_projPath));        string _targetGuid = _pbxProj.TargetGuidByName ("Unity-iPhone");        //*******************************添加framework*******************************//        _pbxProj.AddFrameworkToProject (_targetGuid, "Security.framework", true);        _pbxProj.AddFrameworkToProject (_targetGuid, "JavaScriptCore.framework", true);        //*******************************添加tbd*******************************//        _pbxProj.AddFileToBuild(_targetGuid, _pbxProj.AddFile("usr/lib/libz.1.dylib", "Frameworks/libz.1.dylib", PBXSourceTree.Sdk));        //*******************************設定buildsetting*******************************//        //_pbxProj.SetBuildProperty (_targetGuid, "CODE_SIGN_IDENTITY", code_sign_identity);          File.WriteAllText(_projPath, _pbxProj.WriteToString());    }

把主函數的路徑傳進去就可以了。
中間注釋的那行就是設定buildSetting,項目暫時還沒用就屏蔽了,具體要怎麼傳參數可能還需要百度一下

之後就是修改plist

    static void SetPlist(string pathToBuildProject)    {        string _plistPath = pathToBuildProject + "/Info.plist";        PlistDocument _plist = new PlistDocument ();        _plist.ReadFromString (File.ReadAllText (_plistPath));        PlistElementDict _rootDic = _plist.root;        //*******************************設定plist屬性*******************************//        _rootDic.SetString ("Privacy - Microphone Usage Description", "microphoneDesciption");        _rootDic.SetString ("Bundle version", "6");        _rootDic.SetString ("Bundle versions string, short", "0.0.2");        _rootDic.SetString ("Privacy - Media Library Usage Description", "App需要您的同意,才能訪問媒體資料庫");        _rootDic.SetString ("Privacy - Bluetooth Peripheral Usage Description", "App需要您的同意,才能訪問藍芽");        _rootDic.SetString ("Privacy - Calendars Usage Description", "App需要您的同意,才能訪問日曆");        _rootDic.SetString ("Privacy - Camera Usage Description", "App需要您的同意,才能訪問相機");        _rootDic.SetString ("Privacy - Contacts Usage Description", "是否允許此App訪問您的通訊錄?");        _rootDic.SetString ("Privacy - Health Share Usage Description", "App需要您的同意,才能訪問健康分享");        _rootDic.SetString ("Privacy - Health Update Usage Description", "App需要您的同意,才能訪問健康更新 ");        _rootDic.SetString ("Privacy - Location Always Usage Description", "App需要您的同意,才能始終訪問位置");        _rootDic.SetString ("Privacy - Location Usage Description", "App需要您的同意,才能訪問位置");        _rootDic.SetString ("Privacy - Location When In Use Usage Description", "是否允許此App訪問您的地理位置?");        _rootDic.SetString ("Privacy - Motion Usage Description", "App需要您的同意,才能訪問運動與健身");        _rootDic.SetString ("Privacy - Photo Library Usage Description", "App需要您的同意,才能訪問相簿");        _rootDic.SetString ("Privacy - Reminders Usage Description", "App需要您的同意,才能訪問提醒事項");        File.WriteAllText(_plistPath, _plist.WriteToString());    }

基本上這樣就算搞定了。

不過ios打包一般為了快一點的話,一般會把asset檔案夾拿出來再打完包後放回去(或者像我們項目,資源是策劃準備好上傳到svn,所以資源檔夾不在項目裡面的,這種情況下就代碼複製過去)

[MenuItem("測試/測試")]    static void CopyAssetToXCode(string pathToBuildProject)    {        string assetFolderName = pathToBuildProject + "/Data/Raw/";        DirectoryInfo dir = new DirectoryInfo (IOS_RESOURCE_FOLDER_PATH);        DirectoryInfo[] allDirs = dir.GetDirectories ();        DirectoryInfo latestDir = null;        foreach (DirectoryInfo d in allDirs)         {            if (latestDir == null)                latestDir = d;            else            {                if (latestDir.LastWriteTime.Ticks < d.LastWriteTime.Ticks)                    latestDir = d;            }        }        if (latestDir == null)         {            Debug.LogError ("錯誤!!無法複製Asset資源!!!!!");            return;        }        Directory.CreateDirectory (assetFolderName + "assets");        CopyDirectory (latestDir + "/resources_1first/assets", assetFolderName + "assets");        File.Copy(latestDir + "/版本匯出後檔案存放/version.ini",assetFolderName + "version.ini");        Debug.LogError ("複製Asset到xcode...完成!");        Debug.LogError ("Asset原資源路徑:" + latestDir);    }    static void CopyDirectory(string sourceDirectory, string targetDirectory)     {        if (!Directory.Exists(sourceDirectory) || !Directory.Exists(targetDirectory))         {            return;        }        DirectoryInfo sourceInfo = new DirectoryInfo(sourceDirectory);        FileInfo[] fileInfo = sourceInfo.GetFiles();        foreach (FileInfo fiTemp in fileInfo)         {            //if(!fiTemp.Name.StartsWith("."))                File.Copy(sourceDirectory + "/" + fiTemp.Name, targetDirectory + "/" + fiTemp.Name, true);        }        DirectoryInfo[] diInfo = sourceInfo.GetDirectories();        foreach (DirectoryInfo diTemp in diInfo)         {            string sourcePath = diTemp.FullName;            string targetPath = diTemp.FullName.Replace(sourceDirectory,targetDirectory);            Directory.CreateDirectory(targetPath);            CopyDirectory(sourcePath,targetPath);        }    }

所以最後主函數就是這樣

    [PostProcessBuild]    static void OnPostprocessBuild(BuildTarget target, string pathToBuildProject)    {        Debug.LogError ("修改xcode外掛程式及其他配置...開始!");        ModifyProj (pathToBuildProject);        Debug.LogError ("修改xcode外掛程式及其他配置...完成!");        Debug.LogError ("修改xcode,plist...開始!");        SetPlist (pathToBuildProject);        Debug.LogError ("修改xcode,plist...完成!");        Debug.LogError ("複製Asset到xcode...開始!");        CopyAssetToXCode (pathToBuildProject);    }

【小松教你手遊開發】【unity實用技能】unity ios快捷打包

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.