Created
March 8, 2017 04:25
-
-
Save sryze/eee776e85813f45f84a33441f1d10cdf to your computer and use it in GitHub Desktop.
Modify Xcode project after build in Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if UNITY_EDITOR | |
using System; | |
using System.IO; | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.Callbacks; | |
using UnityEditor.iOS.XcodeNew; | |
public class PostBuild { | |
[PostProcessBuildAttribute] | |
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) { | |
#if UNITY_IOS | |
var projectPath = PBXProject.GetPBXProjectPath(pathToBuiltProject); | |
var project = new PBXProject(); | |
project.ReadFromFile(projectPath); | |
var infoPlist = new PlistDocument(); | |
var infoPlistPath = pathToBuiltProject + "/Info.plist"; | |
infoPlist.ReadFromFile(infoPlistPath); | |
AddFrameworks(project); | |
CopyPodfile(pathToBuiltProject); | |
ConfigureFabric(infoPlist, project); | |
infoPlist.WriteToFile(infoPlistPath); | |
project.WriteToFile(projectPath); | |
#endif | |
} | |
private static string GetDefaultTarget(PBXProject project) { | |
return project.TargetGuidByName(PBXProject.GetUnityTargetName()); | |
} | |
private static void AddFrameworks(PBXProject project) { | |
Debug.Log("Adding frameworks"); | |
var defaultTarget = GetDefaultTarget(project); | |
project.AddFrameworkToProject(defaultTarget, "AdSupport.framework", false); | |
} | |
private static void CopyPodfile(string pathToBuiltProject) { | |
var podfilePath = "Assets/BoxLoader/iOS/Podfile"; | |
var destPodfilePath = pathToBuiltProject + "/Podfile"; | |
Debug.Log(String.Format("Copying Podfile from {0} to {1}", podfilePath, destPodfilePath)); | |
if (!File.Exists(destPodfilePath)) { | |
FileUtil.CopyFileOrDirectory(podfilePath, destPodfilePath); | |
} else { | |
Debug.Log("Podfile already exists"); | |
} | |
} | |
private static void ConfigureFabric(PlistDocument infoPlist, PBXProject project) { | |
Debug.Log("Adding Fabric info to Info.plist"); | |
const string FabricApiKey = "<YOUR API KEY>"; | |
const string FabricSecret = "<YOUR SECRET>"; | |
var fabricDict = new PlistElementDict(); | |
fabricDict.SetString("APIKey", FabricApiKey); | |
var fabricKitsArray = fabricDict.CreateArray("Kits"); | |
var crashlyticsKit = fabricKitsArray.AddDict(); | |
crashlyticsKit.SetString("KitName", "Crashlytics"); | |
crashlyticsKit.CreateDict("KitInfo"); | |
infoPlist.root["Fabric"] = fabricDict; | |
Debug.Log("Adding a run script phase for Fabric"); | |
var defaultTarget = GetDefaultTarget(project); | |
var fabricScript = project.ShellScriptByName(defaultTarget, "Fabric"); | |
if (fabricScript == null) { | |
fabricScript = String.Format("\"${{PODS_ROOT}}/Fabric/run\" {0} {1}", FabricApiKey, FabricSecret); | |
project.AppendShellScriptBuildPhase(defaultTarget, "Fabric", "", fabricScript); | |
} | |
} | |
} | |
#endif |
I can found the API here:
https://bitbucket.org/Unity-Technologies/xcodeapi/src/fbd0cb184b437b751ac0e2213779c310aaba0952/Xcode/PBXProject.cs?at=stable&fileviewer=file-view-default
However, it is internal API, how can I make it public? Thanks!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where does
project.ShellScriptByName
andAppendShellScriptBuildPhase
come from? I dont see that in the current docs https://docs.unity3d.com/ScriptReference/iOS.Xcode.PBXProject.html