- Home /
automated post process frameworks
Trying to automate using post process from unity to xcode. Looked through and tried all relavent parts of PBXProject library including: PBXProject.AddFrameworkToProject PBXProject.AddFileToBuild and others but still the the frameworks are not add like when dragged manually PBXProject.AddFileToEmbedFrameworks - did manage to the frameworks to the embedded binaries but not in to linked frameworks. The build is successful when done manually.
Also having problems automatically adding a second run script in build phases. Thanks for the help
Answer by peter_pimley_unity · Oct 29, 2017 at 09:38 PM
Hi,
Is there a specific bit of code you're having trouble with, that you can post here?
Some things to keep in mind when using the PBXProject API are:
Use the [PostProcessBuild] attribute, and use the correct function signature.
See https://docs.unity3d.com/ScriptReference/Callbacks.PostProcessBuildAttribute.html
Unity will run a [PostProcessBuild]
function after doing the build. Your function must return void and take two parameters, a BuildTarget target and a string pathToBuiltProject. The BuildTarget tells you which platform the build was for, and the string tells you where the build was saved. So for example if you were only interested in processing an iOS build, you could return from the function if the BuildTarget was anything other than BuildTarget.iOS:
[PostProcessBuild]
private static void PostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iOS)
return;
The string is the location of the build. For iOS, this is the location of the folder that contains the Unity-iPhone.xcodeproj
file. So in order to access the project.pbxproj
file you need to do something like this:
string pbxFilename = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj"
Load the file into your PBXProject instance
After instantiating a new PBXProject
, you need to load the data from the file on disk into your instance. Use PBXProject.ReadFromFile
, ReadFromString
or ReadFromStream
.
PBXProject proj = new PBXProject();
proj.ReadFromFile (pbxFilename);
Add your framework
This is the easy part, just call PBXProject.AddFrameworkToProject
. For the first parameter, pass in the result of PBXProject.TargetGuidByName
, passing in the string returned by PBXProject.GetUnityTargetName
. The second parameter is the name of the framework, and the third specifies whether it is optional (i.e. false
means required).
For example, to add ARKit.framework to your project, do this:
string targetName = PBXProject.GetUnityTargetName ();
string guid = proj.TargetGuidByName (targetName);
proj.AddFrameworkToProject(guid, "ARKit.framework", false);
Write the project back to disk
This is an easy step to forget. Use one of PBXProject.WriteToFile
, WriteToStream
or WriteToString
to serialize the PBXProject instance back out to disk:
proj.WriteToFile (pbxFilename);
Putting it all together
Putting all the previous steps together, here is a minimal C# script that does the following:
Early-exits of the build is not for iOS
Creates a PBXProject instance, and loads the data in from the project.pbxproj file.
Adds ARKit.framework.
Saves back out to the project.pbxproj file.
public class MyBuildScript { [PostProcessBuild] private static void PostprocessBuild(BuildTarget target, string pathToBuiltProject) { Debug.LogFormat ("Postprocessing build at \"{0}\" for target {1}", pathToBuiltProject, target); if (target != BuildTarget.iOS) return; PBXProject proj = new PBXProject(); string pbxFilename = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj"; proj.ReadFromFile (pbxFilename); string targetName = PBXProject.GetUnityTargetName (); string guid = proj.TargetGuidByName (targetName); proj.AddFrameworkToProject(guid, "ARKit.framework", false); proj.WriteToFile (pbxFilename); } }
I think this answer should be marked as correct. Thank you, @peter_pimley_unity for this. It is very helpful.
I'm going to add a couple of other notes as it took me quite a bit of time and searching to piece together all of the information needed to accomplish automating the adding of a framework to Xcode (this answer was the most helpful resource I found, btw)...
First: in order to use this, you need to have your script in a folder called "Editor" or its subfolder. Second: you need to include the correct namespaces. Some of these are apparent using Unity's API documentation, but others aren't. Here's the complete list...
using UnityEngine; // only needed for the Debug logging. using UnityEditor; // needed to gain access to the BuildTarget enum. using UnityEditor.Callbacks; // needed to gain access to the PostProcessBuild class. using UnityEditor.iOS.Xcode; // needed to gain access to the PBXProject class.
This is awesome! I did this though and although i see arkit in my xcode file when I build i receive linker errors for _UnityARKit any ideas?
Your answer
![](https://koobas.hobune.stream/wayback/20220612140617im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
URP Post-Processing single button 0 Answers
BuildFailedException: Incremental Player build failed! 2 Answers
Force package import with script errors 0 Answers
Automating transition animations through script 0 Answers
In FastBloom(Bloom Optimized) shader, what's the difference between Standard and Sgx blur type? 0 Answers