"Komatsu teaches you to develop" "Unity Practical Skills" Unity iOS Quick Pack

Source: Internet
Author: User

iOS packaging is more troublesome, with a process need to take a note after a variety of plug-ins to reconfigure each time the package, as a programmer certainly can not accept such a stupid thing, write a script to let the code to achieve these.

First, introduce a tag

[PostProcessBuild]

Write this tag before the function, and unity will call this function after the package is finished.

So we'll also use this tag to make unity change the configuration in Xcode after the package is played into the Xcode project.

and unity has a default function for us to use

static void OnPostprocessBuild(BuildTarget target,string pathToBuildProject)  

Target is the goal of packaging, whether it's Android or iOS or something, Pathtobuildproject is the path after the package is finished.
So, the main function is this.

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

The next step is to add the framework and some system plugins and settings buildsetting

    public static void Modifyproj (String pathtobuildproject) {string _projpath = Pbxproject.getpbxprojectpath (path        Tobuildproject);        Pbxproject _pbxproj = new Pbxproject ();        _pbxproj.readfromstring (File.readalltext (_projpath));        String _targetguid = _pbxproj.targetguidbyname ("Unity-iphone"); Add framework*******************************//_pbxproj.addframeworktoproject (_        Targetguid, "Security.framework", true);        _pbxproj.addframeworktoproject (_targetguid, "Javascriptcore.framework", true); Add tbd*******************************//_pbxproj.addfiletobuild (_targetguid, _        Pbxproj.addfile ("Usr/lib/libz.1.dylib", "Frameworks/libz.1.dylib", PBXSOURCETREE.SDK)); Set buildsetting*******************************////_pbxproj.setbuildproperty (_          Targetguid, "code_sign_identity", code_sign_identity); File.writealltext (_PROJPAth, _pbxproj.writetostring ()); }

The path of the main function can be passed in.
The middle note that line is set buildsetting, the project is not useless on the block, specifically how to pass parameters may also need Baidu a bit

After that, we modify plist.

    static void Setplist (String pathtobuildproject) {string _plistpath = Pathtobuildproject + "/info.plist";        Plistdocument _plist = new Plistdocument (); _plist.        Readfromstring (File.readalltext (_plistpath));        Plistelementdict _rootdic = _plist.root; Set the Plist property *******************************//_rootdic.setstring ("Privacy-microph        One 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 requires your consent to access the Media database");        _rootdic.setstring ("Privacy-bluetooth peripheral Usage Description", "app requires your consent to access Bluetooth");        _rootdic.setstring ("Privacy-calendars Usage Description", "app requires your consent to access the calendar");        _rootdic.setstring ("Privacy-camera Usage Description", "app requires your consent to access the camera"); _rootdic.setstring ("Privacy-contacts UsAge Description "," Do you want to allow this app to access your contacts?        ");        _rootdic.setstring ("Privacy-health Share Usage Description", "app requires your consent to access health sharing");        _rootdic.setstring ("Privacy-health update Usage Description", "app requires your consent to access Health updates");        _rootdic.setstring ("Privacy-location always Usage Description", "app requires your consent in order to access the location");        _rootdic.setstring ("Privacy-location Usage Description", "app requires your consent to access the location");        _rootdic.setstring ("privacy-location when on use Usage Description", "Do you want to allow this app to access your geographic location?");        _rootdic.setstring ("Privacy-motion Usage Description", "app requires your consent to access sports and fitness");        _rootdic.setstring ("Privacy-photo Library Usage Description", "app requires your consent to access the album");        _rootdic.setstring ("Privacy-reminders Usage Description", "app requires your consent to access reminders"); File.writealltext (_plistpath, _plist.    Writetostring ()); }

Basically, it's done.

However, iOS packaging generally in order to hurry up, generally will take out the asset folder and then after the package to put back (or like our project, the resource is planning to upload to SVN, so the resource folder is not in the project, in this case the code copied past)

[MenuItem ("Test/test")] static void Copyassettoxcode (String pathtobuildproject) {string assetfoldername = Pathto        BuildProject + "/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) latestd            IR = D; }} if (Latestdir = = null) {Debug.logerror ("Error!! Unable to replicate asset resource!!!!!            ");        Return        } directory.createdirectory (Assetfoldername + "assets");        CopyDirectory (Latestdir + "/resources_1first/assets", Assetfoldername + "assets");        File.Copy (Latestdir + "/version exported after file storage/version.ini", Assetfoldername + "Version.ini"); Debug.logerror ("Copy Asset to Xcode.Complete        ");    Debug.logerror ("Asset Original Resource path:" + 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); }    }

So the last main function is this.

    [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);    }

"Komatsu teaches you to develop" "Unity Practical Skills" Unity iOS Quick Pack

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.