Is there a way to add files to “Copy Bundle Resources” phase in XCode from unity via scripting API in a Postprocess script?
We’re trying to add Tapjoy’s resources bundle to make it compatible with 2019.3.x, and we couldn’t find a way to copy it from another destination into the Xcode.
Not a solution but it appears the API has methods to add and insert a “Copy Bundle Resources” phase but I can’t see how you would actually add any files to that phase.
There is a way to do it (I did it to add the storyboard launch files to the ‘copy bundle resources’ for a cloud build). Unfortunately the way I did it is really horrible and not really something I would recommend.
If you’re willing to pollute your codebase with this abomination then here’s how to do it:
Add the files you want to include in your xCode project to the ‘StreamingAssets’ folder. In my example I added 2 files:
This has the effect of telling Unity to copy the files into your xCode project and makes them accessible via:
Data/Raw/AA_Gradient.png
Data/Raw/AA_Logo.png
Setup a [PostProcessBuild] script to run when the build is complete
In the post process script retrieve the xCode project file, convert it to a string
Do a search and replace on the string and inject references to your new files in the correct place to add them to the ‘copy bundle resources’ part (make up some random GUIDs for the files to keep xCode happy).
Here’s a code example that shows the xCode project getting retrieved as a string, injecting the references and then writing it back out again:
[PostProcessBuild]
public static void ChangeXcodePlist(BuildTarget buildTarget, string pathToBuiltProject)
{
// xCode workspace modifications
{
// Open the xCode Project
string projectPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
PBXProject project = new PBXProject();
project.ReadFromFile( projectPath );
string targetGuid = project.GetUnityMainTargetGuid();
// Retrieve the xCode project as a string
string projectString = project.WriteToString();
// Add Launch screen storyboard references
{
// Add images and image references
projectString = projectString.Replace( "/* Begin PBXBuildFile section */",
"/* Begin PBXBuildFile section */\n\t\tEA8C3480243F64CC002A3835 /* AA_Gradient.png in Resources */ = {isa = PBXBuildFile; fileRef = EA8C347E243F64CC002A3835 /* AA_Gradient.png */; };" +
"\n\t\tEA8C3481243F64CC002A3835 /* AA_Logo.png in Resources */ = {isa = PBXBuildFile; fileRef = EA8C347F243F64CC002A3835 /* AA_Logo.png */; };" );
projectString = projectString.Replace( "/* Begin PBXFileReference section */",
"/* Begin PBXFileReference section */\n\t\tEA8C347E243F64CC002A3835 /* AA_Gradient.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = AA_Gradient.png; path = Data/Raw/AA_Gradient.png; sourceTree = \"<group>\"; };" +
"\n\t\tEA8C347F243F64CC002A3835 /* AA_Logo.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = AA_Logo.png; path = Data/Raw/AA_Logo.png; sourceTree = \"<group>\"; };" );
// Add images/refs to the 'Copy Bundle Resources' section
projectString = projectString.Replace( "/* CustomTemplate */ = {\n isa = PBXGroup;\n children = (",
"/* CustomTemplate */ = {\n isa = PBXGroup;\n children = (\n EA8C347E243F64CC002A3835 /* AA_Gradient.png */,\n EA8C347F243F64CC002A3835 /* AA_Logo.png */,");
projectString = projectString.Replace( "/* LaunchScreen.storyboard in Resources */,",
"/* LaunchScreen.storyboard in Resources */,\n\t\t\t\tEA8C3480243F64CC002A3835 /* AA_Gradient.png in Resources */," +
"\n\t\t\t\tEA8C3481243F64CC002A3835 /* AA_Logo.png in Resources */," );
}
// Save the xCode project file
File.WriteAllText( projectPath , projectString );
}
}
Unfortunately after trying these solutions, Xcode keeps giving errors with SplashScreen.mm inside of the Unity project, that error is for some reason negating the fact that the Xcode Project regonizes the storyboard, it just fails the build because Apple updated submission does not allow the splash screen setup like the builds before…
I have tried changing Build Player Settings to not include splash screen and only enable the link to the LaunchScreen.storyboard file inside of unity asset folder to no avail,
Is there any way to get unity to not include SplashScreen.mm in the ios build xcode project and just recognize launchscreen.storyboard and bundle resources?
Maybe I should try just removing that file that Xcode doesnt like “SplashScreen.mm” and try again…