xzhuan原创稿件,转载请注明出处!
一、使用PostProcessBuild设置 Unity生成的Xcode Project工程
unity提供了一套api去修改xcode项目工程配置以及修改plist文件内容(当unity build结束后, 会自动回调OnPostProcessBuild).
以下是一些用到的配置处理:
[UnityEditor.Callbacks.PostProcessBuild(999)]
public static void OnPostProcessBuild(BuildTarget BuildTarget, string path)
{
if(BuildTarget==BuildTarget.iOS)
{
OnIOS(path);
}
}
private static string xcodePath;
private static string sourceSDKPath;
public static void OnIOS(string path)
{
string projPath = PBXProject.GetPBXProjectPath(path);
UnityEditor.iOS.Xcode.PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
//获取当前项目名字
string target = proj.TargetGuidByName(PBXProject.GetUnityTargetName());
sourceSDKPath = Application.dataPath;
sourceSDKPath = sourceSDKPath.Remove(sourceSDKPath.IndexOf("Assets"));
sourceSDKPath += "SDK/";
Debug.Log(sourceSDKPath);
以下只做示列:
//设置编译配置选项
proj.SetBuildProperty(target, "ENABLE_BITCODE", "NO");
//设置 Search FrameWork
proj.SetBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(inherited)");
proj.AddBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(PROJECT_DIR)/Frameworks/Plugins/iOS");
//设置 Search Head
proj.SetBuildProperty(target, "HEADER_SEARCH_PATHS", "$(SRCROOT)");
proj.AddBuildProperty(target, "HEADER_SEARCH_PATHS", "$(SRCROOT)/Classes");
proj.AddBuildProperty(target, "HEADER_SEARCH_PATHS", "$(SRCROOT)/Classes/Native");
proj.AddBuildProperty(target, "HEADER_SEARCH_PATHS", "$(SRCROOT)/Libraries/bdwgc/include");
proj.AddBuildProperty(target, "HEADER_SEARCH_PATHS", "$(SRCROOT)/Libraries/libil2cpp/include");
//设置 Search lib
proj.SetBuildProperty(target, "LIBRARY_SEARCH_PATHS", "$(inherited)");
proj.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS", "$(SRCROOT)");
proj.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS", "$(SRCROOT)/Libraries");
proj.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS", "$(SRCROOT)/Libraries/Plugins/iOS");
proj.AddBuildProperty(target, "LIBRARY_SEARCH_PATHS", "$(PROJECT_DIR)/Libraries");
//添加依赖库
proj.AddFrameworkToProject(target, "libiconv.tbd", false);
proj.AddFrameworkToProject(target, "libz.tbd", false);
proj.AddFrameworkToProject(target, "libc++.tbd", false);
proj.AddFrameworkToProject(target, "libsqlite.3.0.tbd", false);
proj.AddFrameworkToProject(target, "Security.framework", false);
proj.AddFrameworkToProject(target, "CoreTelephony.framework", false);
proj.AddFrameworkToProject(target, "SystemConfiguration.framework", false);
//设置 Comple Flags
string file = proj.FindFileGuidByProjectPath("Libraries/Plugins/iOS/KeychainItemWrapper.m");
var flags = proj.GetCompileFlagsForFile(target, file);
flags.Add("-fno-objc-arc");
proj.SetCompileFlagsForFile(target, file, flags);
//保存工程
proj.WriteToFile(projPath);
//修改plist
string plistPath = path + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
PlistElementDict rootDict = plist.root;
//iOS10必须
rootDict.SetString("NSCameraUsageDescription", "同意使用相机才能拍照");
rootDict.SetString("NSPhotoLibraryUsageDescription", "同意使用相册才能使用图片"
PlistElementArray schemes = rootDict.CreateArray("LSApplicationQueriesSchemes");
schemes.AddString("wechat");
schemes.AddString("weixin");
//设置 Security Exception
PlistElement SecurityElement = rootDict["NSAppTransportSecurity"];
PlistElementDict appTransportSecurity = null;
if (SecurityElement == null)
appTransportSecurity = rootDict.CreateDict("NSAppTransportSecurity");
else
appTransportSecurity = SecurityElement.AsDict();
appTransportSecurity.SetBoolean("NSAllowsArbitraryLoads", true);
var exceptionDomain = appTransportSecurity.CreateDict("NSExceptionDomains");
var qqExceptions = exceptionDomain.CreateDict("qq.com");
qqExceptions.SetBoolean("NSExceptionAllowsInsecureHTTPLoads", true);
qqExceptions.SetBoolean("NSExceptionRequiresForwardSecrecy", false);
qqExceptions.SetBoolean("NSIncludesSubdomains", true);
var backgroundModes = rootDict.CreateArray("UIBackgroundModes");
backgroundModes.AddString("remote-notification");
plist.WriteToFile(plistPath);
}
这样就可以自动设置了, 哇卡卡~~~