unity导出xcode工程之后,自动配置

接上一节: iOS与unity交互混编, 闭包方式实现互相调用

插件添加到工程

如果插件只包含 .h/.m/.mm/.c/.cpp/.a 文件,直接放入 Unity Assets/Plugins/iOS 目录即可。在 Unity 导出 Xcode 工程时,这些文件会放置在Unity-iPhone/Libraries/Plugins/iOS目录。

如果插件包含 .framework/.bundle 文件,需要在 Unity 导出 XCode 工程之后,手动或利用unity的脚本功能,加入 Xcode 工程中。勾选Copy items if needed,选择Create groups,勾选Add to targets: Unity-iPhone。

导出iOS工程之后的设置

如果每次导出 Xcode 之后手动修改 PBXProject 和 Plist,将会非常麻烦。UnityEditor 提供一个[PostProcessBuild]标签,可以帮助我们在导出 Xcode 之后执行一些自动化操作。

标记 PostProcessBuild 方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;



#if UNITY_IOS || UNITY_EDITOR
public class XcodeBuildPostprocessor
{
    // 版本号
    [PostProcessBuild(1)]
    public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
    {
        if (buildTarget == BuildTarget.iOS)
        {
            UnityEngine.Debug.Log("XCodePostProcess: Starting to perform post build tasks for iOS platform.");

            /*===projPath===*/
            string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";

            PBXProject proj = new PBXProject();
            proj.ReadFromFile(projPath);

            string target = proj.TargetGuidByName("Unity-iPhone");

            // ENABLE_BITCODE=False
            proj.SetBuildProperty(target, "ENABLE_BITCODE", "false");

            // add extra framework(s) 
            proj.AddFrameworkToProject(target, "Accelerate.framework", true);
            proj.AddFrameworkToProject(target, "AddressBook.framework", true);
            proj.AddFrameworkToProject(target, "AdSupport.framework", true);
            proj.AddFrameworkToProject(target, "CoreTelephony.framework", true);
            proj.AddFrameworkToProject(target, "ImageIO.framework", true);
            proj.AddFrameworkToProject(target, "MessageUI.framework", true);
            proj.AddFrameworkToProject(target, "MobileCoreServices.framework", true);
            proj.AddFrameworkToProject(target, "libc++.tbd", true);
            proj.AddFrameworkToProject(target, "libsqlite3.tbd", true);
            proj.AddFrameworkToProject(target, "libxml2.tbd", true);

            // rewrite to file
            File.WriteAllText(projPath, proj.WriteToString());


            /*====plist=====*/
            string plistPath = path + "/Info.plist";
            PlistDocument plist = new PlistDocument();
            plist.ReadFromString(File.ReadAllText(plistPath));
            // Get root
            PlistElementDict rootDict = plist.root;

            // 权限
            rootDict.SetString("NSCameraUsageDescription", "请允许打开摄像头");
            rootDict.SetString("NSMicrophoneUsageDescription", "请允许打开麦克风");

            // 允许http
            var atsKey = "NSAppTransportSecurity";
            PlistElementDict dictTmp = rootDict.CreateDict(atsKey);
            dictTmp.SetBoolean("NSAllowsArbitraryLoads", true);

            // Write to file
            File.WriteAllText(plistPath, plist.WriteToString());
        }
    }
}
#endif

你可能感兴趣的:(unity导出xcode工程之后,自动配置)