Skip to content

Instantly share code, notes, and snippets.

@zilppuri
Last active January 7, 2020 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zilppuri/25514d9eb30283e372b86000482042f4 to your computer and use it in GitHub Desktop.
Save zilppuri/25514d9eb30283e372b86000482042f4 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
#if UNITY_IPHONE
using UnityEditor.iOS.Xcode;
#endif
using System.IO;
public static class XCodePostprocessSplashFix
{
#if UNITY_IPHONE
const string XCODE_PROJECT_NAME = "Unity-iPhone";
[PostProcessBuild]
static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
{
if (target == BuildTarget.iOS)
{
//Launch screen
//Set team ID manually
string projPath = pathToBuiltProject + "/" + XCODE_PROJECT_NAME + ".xcodeproj/project.pbxproj";
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
AddFileToProject(ref proj, pathToBuiltProject, "Editor/customlaunchscreen.png", "customlaunchscreen.png"); //If you have png used in your Storyboard
AddFileToProject(ref proj, pathToBuiltProject, "Editor/CustomLaunchScreen.storyboard", "ActualLaunchScreen.storyboard"); //This one is used by iOS
AddFileToProject(ref proj, pathToBuiltProject, "Editor/LaunchScreen.storyboard", "LaunchScreen.storyboard"); //Used by Unity for 1 sec for "reasons"
//This uses the CustomSplashViewController class to disable Unity orientation hacks
AddFileToProject(ref proj, pathToBuiltProject, "Editor/CustomSplashViewController.h", "CustomSplashViewController.h");
AddFileToProject(ref proj, pathToBuiltProject, "Editor/CustomSplashViewController.m", "CustomSplashViewController.m");
// Get plist
string plistPath = pathToBuiltProject + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
//Set the Storyboard without custom class
rootDict.SetString("UILaunchStoryboardName", "ActualLaunchScreen");
//Optionally delete the deprecated garbage values added by Unity
//rootDictValues.Remove("UILaunchStoryboardName~ipad");
//rootDictValues.Remove("UILaunchStoryboardName~iphone");
//rootDictValues.Remove("UILaunchStoryboardName~ipod");
// Write to file
File.WriteAllText(plistPath, plist.WriteToString());
}
}
static void AddFileToProject(ref PBXProject proj, string pathToBuiltProject, string assetPath, string projectPath) {
string sourcepath = Path.Combine(Application.dataPath, assetPath);
string targetpath = Path.Combine(pathToBuiltProject, projectPath);
string targetguid = proj.TargetGuidByName(XCODE_PROJECT_NAME);
Debug.Log("Add File To Xcode project: " + sourcepath + " -> " + targetpath);
File.Copy(sourcepath, targetpath, true);
string guid = proj.AddFile(targetpath, projectPath, PBXSourceTree.Source);
proj.AddFileToBuild(targetguid, guid);
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment