首页 > 其他分享 >Unity通过PBXProject生成XCode工程

Unity通过PBXProject生成XCode工程

时间:2023-04-24 19:01:24浏览次数:56  
标签:PBXProject string filePath mainTarget framework Unity pbxProject frameworkTarget

Unity版本:2020.3.47f1

首先通过PostProcessBuildAttribute监听XCode工程导出完成事件,GetUnityMainTargetGuid是获取XCode工程中"Unity-iPhone"对应的target,GetUnityFrameworkTargetGuid则对应"UnityFramework",在unity中大部分操作会是针对UnityFramework。PBXProject的很多操作都是通过guid的,下面会出现各种各样的guid。

下面是基础代码:

static PBXProject pbxProject;
static string mainTarget;
static string frameworkTarget;
[PostProcessBuildAttribute]
public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
{
    if (buildTarget == BuildTarget.iOS)
    {
        pbxProject = new PBXProject();
        pbxProject.ReadFromString(File.ReadAllText(path));
        mainTarget = pbxProject.GetUnityMainTargetGuid();
        frameworkTarget = pbxProject.GetUnityFrameworkTargetGuid();

    }
}

 

一、BuildSettings

使用SetBuildProperty设置BuildSettings中的设置,参数分别为target、设置名、设置值,其中设置名可以参考这里,比如:

pbxProject.SetBuildProperty(mainTarget, "ENABLE_BITCODE", "NO");
pbxProject.SetBuildProperty(frameworkTarget, "ENABLE_BITCODE", "NO");

如果需要对Debug、Release分别设置,使用BuildConfigByName和SetBuildPropertyForConfig即可,比如:

string debugConfig = pbxProject.BuildConfigByName(mainTarget, "Debug");//Debug、Release、ReleaseForProfiling、ReleaseForRunning
pbxProject.SetBuildPropertyForConfig(debugConfig , "ENABLE_BITCODE", "NO");

 BuildSettings中的所有设置都可以通过上面两个方法设置,包括签名等设置,签名后面单独写。

二、BuildPhases

1、加入至CopyBundleResources

使用AddFile和AddFileToBuild,如下代码,GoogleService-Info.plist与Unity-iPhone.xcodeproj在同一级目录下,下面代码只加到了mainTarget中,如有需要也可加入frameworkTarget

string filePath = "GoogleService-Info.plist";
string fileGuid = pbxProject.AddFile(filePath, filePath, PBXSourceTree.Source);
pbxProject.AddFileToBuild(mainTarget, fileGuid);

2、加入至CompileSources

代码都需要加入到这里,注:AppleLogin.m在Apple目录中,Apple与Unity-iPhone.xcodeproj在同一级目录,不需要对Apple文件夹做任何操作,不要使用"Apple/AppleLogin.m",否则会找不着路径,必须使用Path.Combine。

string filePath = Path.Combine("Apple", "AppleLogin.m");
string fileGuid = pbxProject.AddFile(filePath, filePath, PBXSourceTree.Source);
var sourcesBuildPhase = pbxProject.GetSourcesBuildPhaseByTarget(mainTarget);
pbxProject.AddFileToBuildSection(target, sourcesBuildPhase, fileGuid);

3、LinkBinaryWithLibraries

这块会比较复杂,要分开为4种情况

·系统Framework

使用AddFrameworkToProject,函数很简单,三个参数分别是target、framework名、是否为弱引用,framework名必须以".framework"结尾,弱引用为false时对应xcode中的Required

pbxProject.AddFrameworkToProject(frameworkTarget, "AdSupport.framework", false);

·SwiftPackageManager中的Framework

当工程中用到了SwiftPakcageManager则需要调用以下方法,如下面用到的是Firebase包中的三个framework,Remote相关接口是在Unity2020.3.42新加的,具体可查看官方文档。其中AddRemotePackageFrameworkToProject既是添加到LinkBinaryWithLibraries中,framework的名字不带任何后缀

string firebaseGuid = pbxProject.AddRemotePackageReferenceAtVersion("https://github.com/firebase/firebase-ios-sdk", "10.8.1");
pbxProject.AddRemotePackageFrameworkToProject(frameworkTarget, "FirebaseMessaging", firebaseGuid, false);
pbxProject.AddRemotePackageFrameworkToProject(frameworkTarget, "FirebaseAnalytics", firebaseGuid, false);
pbxProject.AddRemotePackageFrameworkToProject(frameworkTarget, "FirebaseCrashlytics", firebaseGuid, false);

·target作为Framework

由于前面的remoteFramework全部是添加至frameworkTarget中,mainTarget中的代码可能会报错,如果mainTarget中也添加一次,会报两个target中有重复代码的提示,但依然可以编译通过,运行时会有比较诡异的情况发生,比如FirebaseCloudMessaging中的注册token会每次启动都更新,解决方法是把UnityFramework添加至Unity-iPhone的LinkBinaryWithLibraries中,代码如下:

string file = "UnityFramework.framework";
string fileGuid = pbxProject.AddFile(file, file, PBXSourceTree.Build);
if (fileGuid != null)
{
    var sourcesBuildPhase = pbxProject.GetFrameworksBuildPhaseByTarget(mainTarget);
    pbxProject.AddFileToBuildSection(mainTarget, sourcesBuildPhase, fileGuid);
}

·本地Framework

本地Framework也有点特殊,调用的是AddFileToBuild,注:FBAudienceNetwork.framework在sdks目录中,sdks与Unity-iPhone.xcodeproj同一级,不需要单独添加sdks这个目录,否则会找不着所添加的framework

string filePath = Path.Combine("sdks", "FBAudienceNetwork.framework");
string fileGuid = pbxProject.AddFile(filePath, filePath, PBXSourceTree.Source);
pbxProject.AddFileToBuild(frameworkTarget, fileGuid);

三、Info.plist

待续

四、Capabilities

待续

五、签名

待续

标签:PBXProject,string,filePath,mainTarget,framework,Unity,pbxProject,frameworkTarget
From: https://www.cnblogs.com/muhoor/p/17350392.html

相关文章

  • 在Unity 网络通讯
    usingSystem.Collections;usingUnityEngine;usingUnityEngine.Networking;publicclassHttpTest:MonoBehaviour{voidStart(){StartCoroutine(UnityWebRequestDemo());}IEnumeratorUnityWebRequestDemo(){using(Uni......
  • Unity___QFramework笔记
    引入Event引入事件监听。使用方法先定义一个事件类//定义数据变更事件publicstructCountChangeEvent//++{}//执行事件this.SendEvent<CountChangeEvent>();//++//注册事件this.RegisterEvent<CountChangeEvent>(e......
  • unity 打开电脑本地文件夹
    1.调用方法如下这是选择路径 2.代码如下usingSystem;usingSystem.IO;usingSystem.Runtime.InteropServices;usingUnityEngine;///<summary>///调用系统代码,打开本地文件夹///</summary>[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]pub......
  • Unity框架:JKFrame2.0学习笔记(十)——自动生成资源引用代码(2)
    前言上一篇记录了自动生成资源引用代码的内部实现,主要是针对addressable的资源系统的,为了在加载时不会因为名字写错,加载错,也更加方便的使用addressable加载,这一篇记录下如何使用。如何使用之前看过,在编辑器中添加了工具按钮我们可以在addressable的groups面板上添加几个测试资源我......
  • 【Unity】旋转木马
    对三角函数进行实际操作,需要对木马移动进行平滑插值木马起伏采用的Cos函数的周期实现usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassMerryGoRound:MonoBehaviour{publicTransformnode;publicfloatrudis;......
  • unity实现简单AR识别
    首先前往unity官网:https://unity.cn/releases,下拉找到下载UnityHub绿色按钮下载完安装进入UnityHub中,在侧边栏找到Installs,点击打开下载如图所示的unity版本(注:unity同ue,只能高版本向低版本兼容)下载过程中记得取消勾选Documentation,选择简体中文(注:很多内容需要访问外网......
  • Unity Resources.Load
    图片路径必须是Assets\Resources目录下面的,并且不能带扩展名//E:\Assets\Resources\img\abc.jpgstringfilePath="img/abc";vartexture=Resources.Load<Texture2D>(filePath);GameObjectobj=newGameObject("newname",typeof(SpriteRenderer));Spr......
  • M1 Mac Xcode模拟器无法运行
    from:https://www.jianshu.com/p/87a5cca2a490 新版本的M1芯片运行模拟器报CocoaPods库(如:WechatOpenSDK报arm64错误)错误,是因为M1兼容问题,解决办法有两种如下:强烈推荐方法2方法1:强制打开xcode对x86的支持,有缺点:因为还是采用x86,编译速度很慢。关闭xcode--应用程序--xocde--......
  • Unity- 数据与网络(占坑)
    一、常用数据格式1.轻量数据格式JSON1.1对象1.2数组1.3SON的创建和解析2.可扩展标记语言XML1.使用XML语法进行解析2.使用XPath语法进行解析CSV与ExcelSQLite1.创建表2.删除表3.增加数据4.查找数据5.删除数据6.修改数据二、多线程三、网络请求的使用网络请求搭......
  • 在MacOS下使用Unity3D开发游戏
    第一次发博客,先发一下我的游戏开发环境吧。去年2月份买了一台MacBookPro2021M1pro(以下简称mbp),这一年来一直在用mbp开发游戏。我大致分享一下我的开发工具以及使用体验。1、Unity官网链接:https://unity.cn/releases我一般使用的Applesilicon版本的,支持M1芯片,无需转译。Un......