Unity产生Xcode工程的参数自动化设置

简单来说就是unity提供一套api去修改xcode项目工程配置以及修改plist文件内容(当unity build结束后, 会自动回调OnPostProcessBuild).

以下是一些用到的配置处理:
ENABLE_BITCODE
AddFramework
https
NSPhotoLibraryUsageDescription

 

[PostProcessBuild]
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, "Security.framework", false);
        proj.AddFrameworkToProject(target, "CoreTelephony.framework", true);
        proj.AddFrameworkToProject(target, "libz.tbd", true);

        // rewrite to file
        File.WriteAllText(projPath, proj.WriteToString());
        
        string plistPath = path + "/Info.plist";
        PlistDocument plist = new PlistDocument();
        plist.ReadFromString(File.ReadAllText(plistPath));
                
        // Get root
        PlistElementDict rootDict = plist.root;

        /* ipad 关闭分屏 */
        rootDict.SetBoolean("UIRequiresFullScreen", true);

        var now = System.DateTime.Now;
        string time = string.Format("{0}_{1}_{2} {3}:{4}:{5}", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
        /* 设置Build值 */
        rootDict.SetString("CFBundleVersion", string.Format("{0}({1})", GlobalVars.VERSION, time));

        /* iOS9所有的app对外http协议默认要求改成https */
        // Add value of NSAppTransportSecurity in Xcode plist
        var atsKey = "NSAppTransportSecurity";
                
        PlistElementDict dictTmp = rootDict.CreateDict( atsKey );
        dictTmp.SetBoolean( "NSAllowsArbitraryLoads", true);

        // location native development region 
        rootDict.SetString("CFBundleDevelopmentRegion", "zh_CN");

        // for share sdk 截屏
        rootDict.SetString("NSPhotoLibraryUsageDescription", "We need use photo library usage");

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

    }
}

 

在Info.plist里添加URL Scheme:

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

// URL schemes 追加
var urlTypeArray = plist.root.CreateArray("CFBundleURLTypes");
var urlTypeDict = urlTypeArray.AddDict();
urlTypeDict.SetString("CFBundleTypeRole", "Editor");
urlTypeDict.SetString("CFBundleURLName", "OpenApp");
var urlScheme = urlTypeDict.CreateArray("CFBundleURLSchemes");
urlScheme.AddString("sampleApp");
                
urlTypeDict = urlTypeArray.AddDict();
urlTypeDict.SetString("CFBundleURLName", "com.abc.sampleApp");
urlScheme = urlTypeDict.CreateArray("CFBundleURLSchemes");
urlScheme.AddString("sampleApp2");

 

有些第三方的sdk需要在appController里加一些“料”,比如Adjust SDK的deeplink功能,需要在openUrl里加[Adjust appWillOpenUrl:url];,也需要在continueUserActivity函数里作相应处理。这里的处理参考了雨松大大的博客。

首先是XClass这个类:

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 + "not found in path.");
                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 + " not found sign in " + 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 + " not found sign in " + below);
                return;
            }

            text_all = text_all.Replace(below, newText);
            StreamWriter streamWriter = new StreamWriter(filePath);
            streamWriter.Write(text_all);
            streamWriter.Close();

        }

        public void Dispose()
        {

        }
    }
}

 

然后还是在OnPostprocessBuild里进行hack:

string xcodePath = Path.GetFullPath (path);
UnityEditor.XCodeEditor.XClass UnityAppController = new UnityEditor.XCodeEditor.XClass(xcodePath + "/Classes/UnityAppController.mm");
UnityAppController.WriteBelow("#include \"PluginBase/AppDelegateListener.h\"", "#include \"Adjust.h\"");
UnityAppController.WriteBelow("AppController_SendNotificationWithArg(kUnityOnOpenURL, notifData);","[Adjust appWillOpenUrl:url];");
UnityAppController.WriteBelow("SensorsCleanup();\n}", "- (BOOL)application:(UIApplication*)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * _Nullable))restorationHandler\r{\rif ([[userActivity activityType] isEqualToString:NSUserActivityTypeBrowsingWeb])\r{\rNSURL *url = [userActivity webpageURL];\rNSURL *oldStyleDeepLink = [Adjust convertUniversalLink:url scheme:@\"sampleApp\"];\r[Adjust appWillOpenUrl:url];\r}\rreturn YES;\r}");

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值