gpt4 book ai didi

ios - 统一 3d |后处理构建 |在为 iOS 构建创建 xcode 项目时,如何从 unity 脚本将新的运行脚本添加到 PBXProject?

转载 作者:行者123 更新时间:2023-11-28 23:31:00 26 4
gpt4 key购买 nike

我正在使用适用于 iOS 平台的 Unity 3d 创建一个游戏应用程序。当 Unity 3d 为 iOS 构建创建 xCode 项目时,我希望 xcode 项目具有一些预定义的构建阶段和运行脚本。那么我应该在 PostProcessBuild 中编写什么代码来实现这一点?

最佳答案

关于你的问题,你可以引用下面这个链接:

https://bitbucket.org/Unity-Technologies/xcodeapi/src/stable/

之前因为工作原因用过

我下载了它并做了一些代码转换。我在Unity中打包Xcode工程测试后,可以实现如下需求:

1.修改Build Phases,如添加Run Script。

2.添加嵌入式框架。

3.添加系统框架

4.修改build设置。

5.修改Info.plist.

6.修改UnityAppController.mm。

其他引用代码可以自行测试。

我把代码 here ,下载的XcodeAPI代码需要和Unity自带的Xcode API区分开来。我添加了一个命名空间:Users.Custom。

using System;
using UnityEngine;
#if UNITY_EDITOR && UNITY_IOS
using UnityEditor;
using UnityEditor.Callbacks;
//using UnityEditor.iOS.Xcode;
using Users.Custom; //
#endif
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;



namespace ThirdSDKPostBuilds
{
public class UserOnPostBuild: MonoBehaviour
{
#if UNITY_EDITOR && UNITY_IOS
[PostProcessBuild( 900 )]
static void OnPostProcessBuild (BuildTarget target, string pathToBuiltProject)
{
if (target.ToString()=="iPhone"||target.ToString()=="iOS") {
Debug.Log("Start Xcode project related configuration of SDK......");
Debug.Log(pathToBuiltProject);
EditProj(pathToBuiltProject);
Debug.Log("Complete the Xcode project configuration of the SDK!");
}
}
#endif

static void EditProj(string pathToBuiltProject)
{
string projPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
Users.Custom.PBXProject pbxProj = new Users.Custom.PBXProject();
pbxProj.ReadFromFile(projPath);
string targetGuid = pbxProj.TargetGuidByName("Unity-iPhone");

pbxProj.SetBuildProperty(targetGuid, "IPHONEOS_DEPLOYMENT_TARGET", "9.0");
//pbxProj.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");
pbxProj.SetBuildProperty(targetGuid, "CLANG_ENABLE_MODULES", "YES");
pbxProj.AddFrameworkToProject(targetGuid, "CoreBluetooth.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "GLKit.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "AudioToolbox.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "CoreFoundation.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "ImageIO.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "AdSupport.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "AVFoundation.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "CoreMedia.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "Foundation.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "Security.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "UIKit.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "CoreVideo.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "CFNetwork.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "MobileCoreServices.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "CoreData.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "CoreMotion.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "EventKitUI.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "EventKit.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "MessageUI.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "Social.framework", false);
pbxProj.AddFrameworkToProject(targetGuid, "Twitter.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "CoreGraphics.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "CoreLocation.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "CoreTelephony.framework", false);
pbxProj.AddFrameworkToProject(targetGuid, "MediaPlayer.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "QuartzCore.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "StoreKit.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "SystemConfiguration.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "AdSupport.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "Mapkit.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "Passkit.framework", false);
pbxProj.AddFrameworkToProject(targetGuid, "Webkit.framework", true);
pbxProj.AddFrameworkToProject(targetGuid, "StoreKit.framework", false);
pbxProj.AddFrameworkToProject(targetGuid, "AVKit.framework", false);
pbxProj.AddFrameworkToProject(targetGuid, "JavaScriptCore.framework", false);
pbxProj.AddFrameworkToProject(targetGuid, "WatchConnectivity.framework", false);

//add embedded framework
pbxProj.SetBuildProperty (targetGuid,"LD_RUNPATH_SEARCH_PATHS", "$(inherited) @executable_path/Frameworks");
string defaultLocationInProj = "Frameworks/Plugins/iOS/EmbeddedFramework/";
string relativeCoreFrameworkPath = "";
string[] commonFrameworkNames=new string[]{"EmbeddedFramework1","EmbeddedFramework2"};
foreach (var frameworkNameTemp in commonFrameworkNames) {
string frameworkName = frameworkNameTemp+".framework";
relativeCoreFrameworkPath = Path.Combine(defaultLocationInProj, frameworkName);
AddDynamicFrameworks (ref pbxProj,targetGuid,relativeCoreFrameworkPath);
}

//add run script
pbxProj.AppendShellScriptBuildPhase(targetGuid,"Run Script copy_test.sh","/bin/sh","./../Assets/Editor/copy_test.sh");
Debug.Log("copy_test completed!");
//add .dylib
pbxProj.AddFileToBuild(targetGuid, pbxProj.AddFile("usr/lib/libresolv.dylib", "Frameworks/libresolv.dylib", Users.Custom.PBXSourceTree.Sdk));
pbxProj.AddFileToBuild(targetGuid, pbxProj.AddFile("usr/lib/libsqlite3.0.dylib", "Frameworks/libsqlite3.0.dylib", Users.Custom.PBXSourceTree.Sdk));
pbxProj.AddFileToBuild(targetGuid, pbxProj.AddFile("usr/lib/libz.dylib", "Frameworks/libz.dylib", Users.Custom.PBXSourceTree.Sdk));
pbxProj.AddFileToBuild(targetGuid, pbxProj.AddFile("usr/lib/libxml2.dylib", "Frameworks/libxml2.dylib", Users.Custom.PBXSourceTree.Sdk));
pbxProj.AddFileToBuild(targetGuid, pbxProj.AddFile("usr/lib/libz.dylib", "Frameworks/libz.dylib", Users.Custom.PBXSourceTree.Sdk));
pbxProj.AddFileToBuild(targetGuid, pbxProj.AddFile("usr/lib/libsqlite3.dylib", "Frameworks/libsqlite3.dylib", Users.Custom.PBXSourceTree.Sdk));
pbxProj.AddFileToBuild(targetGuid, pbxProj.AddFile("usr/lib/libc++.dylib", "Frameworks/libc++.dylib", Users.Custom.PBXSourceTree.Sdk));
pbxProj.AddFileToBuild(targetGuid, pbxProj.AddFile("usr/lib/libsqlite3.dylib", "Frameworks/libsqlite3.dylib", Users.Custom.PBXSourceTree.Sdk));
pbxProj.AddFileToBuild(targetGuid, pbxProj.AddFile("usr/lib/libxml2.2.dylib", "Frameworks/libxml2.2.dylib", Users.Custom.PBXSourceTree.Sdk));
//build settings
pbxProj.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-ObjC");
pbxProj.WriteToFile(projPath);
Debug.Log(" OnPostProcessBuild success!");
}

static void EditInfoPlist(string filePath)
{
string path = filePath + "/Info.plist";
Users.Custom.PlistDocument plistDocument = new Users.Custom.PlistDocument();
plistDocument.ReadFromFile(path);
Users.Custom.PlistElementDict dict = plistDocument.root.AsDict();
Users.Custom.PlistElementDict securityDic = dict.CreateDict("NSAppTransportSecurity");
securityDic.SetBoolean ("NSAllowsArbitraryLoads",true);
dict.SetString ("NSLocationAlwaysUsageDescription", "NSLocationAlwaysUsageDescription");
dict.SetString ("NSPhotoLibraryUsageDescription", "NSPhotoLibraryUsageDescription");
dict.SetString ("NSCameraUsageDescription", "NSCameraUsageDescription");
dict.SetString ("NSCalendarsUsageDescription", "NSCalendarsUsageDescription");
plistDocument.WriteToFile(path);
}

public static void CopyDirectory(string srcPath, string dstPath, string[] excludeExtensions, bool overwrite = true)
{
if (!Directory.Exists(dstPath))
Directory.CreateDirectory(dstPath);

foreach (var file in Directory.GetFiles(srcPath, "*.*", SearchOption.TopDirectoryOnly).Where(path => excludeExtensions == null || !excludeExtensions.Contains(Path.GetExtension(path))))
{
File.Copy(file, Path.Combine(dstPath, Path.GetFileName(file)), overwrite);
}

foreach (var dir in Directory.GetDirectories(srcPath))
CopyDirectory(dir, Path.Combine(dstPath, Path.GetFileName(dir)), excludeExtensions, overwrite);
}

static void EditUnityAppController(string pathToBuiltProject)
{
//Edit UnityAppController.mm
XClass UnityAppController = new XClass(pathToBuiltProject + "/Classes/UnityAppController.mm");
//Refer to the header file of the third-party SDK
UnityAppController.WriteBelow("#include \"PluginBase/AppDelegateListener.h\"","#import \"ThirdDK.h\"");
//add code: [[ThirdSDK sharedInstance] showSplash:@"appkey" withWindow:self.window blockid:@"blockid"]; return YES;
string resultStr = "";
string newCodeStr = " [[ThirdSDK sharedInstance] showSplash:@\"{0}\" withWindow:self.window blockid:@\"{1}\"];\n\n return YES;";
resultStr = string.Format (newCodeStr,"appkey","blockid");
UnityAppController.Replace ("return YES;",resultStr,"didFinishLaunchingWithOptions");
}

static void AddDynamicFrameworks(ref Users.Custom.PBXProject project, string target,string embeddedFrameworkRelativePath)
{
string relativeCoreFrameworkPath = embeddedFrameworkRelativePath;
project.AddDynamicFrameworkToProject(target, relativeCoreFrameworkPath);
Debug.Log("Dynamic Frameworks added to Embedded binaries.");
}


public partial class XClass : System.IDisposable
{

private string filePath;

public XClass(string fPath)
{
filePath = fPath;
if( !System.IO.File.Exists( filePath ) ) {
Debug.LogError( filePath +"The file does not exist under the path!" );
return;
}
}
public void Replace(string oldStr,string newStr,string method="")
{
if (!File.Exists (filePath))
{

return;
}
bool getMethod = false;
string[] codes = File.ReadAllLines (filePath);
for (int i=0; i<codes.Length; i++)
{
string str=codes[i].ToString();
if(string.IsNullOrEmpty(method))
{
if(str.Contains(oldStr))codes.SetValue(newStr,i);
}
else
{
if(!getMethod)
{
getMethod=str.Contains(method);
}
if(!getMethod)continue;
if(str.Contains(oldStr))
{
codes.SetValue(newStr,i);
break;
}
}
}
File.WriteAllLines (filePath, codes);
}


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){

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 Dispose()
{

}
}

}

}

重要代码:

pbxProj.AppendShellScriptBuildPhase(targetGuid,"Run Script copy_test.sh","/bin/sh","./../Assets/Editor/copy_test.sh");

build Phases配置下最终打包的Xcode工程如下:

/image/rxA2y.png

关于ios - 统一 3d |后处理构建 |在为 iOS 构建创建 xcode 项目时,如何从 unity 脚本将新的运行脚本添加到 PBXProject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56479894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com