How To Unity Build Inside App
How To Unity Build Inside App
MIT License
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#if UNITY_IOS
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
/// <summary>
/// Adding this post build script to Unity project enables Unity iOS build output
to be embedded
/// into existing Xcode Swift project.
///
/// However, since this script touches Unity iOS build output, you will not be able
to use Unity
/// iOS build directly in Xcode. As a result, it is recommended to put Unity iOS
build output into
/// a temporary directory that you generally do not touch, such as '/tmp'.
///
/// In order for this to work, necessary changes to the target Xcode Swift project
are needed.
/// Especially the 'AppDelegate.swift' should be modified to properly initialize
Unity.
/// See https://github.com/jiulongw/swift-unity for details.
/// </summary>
public static class XcodePostBuild
{
/// <summary>
/// Path to the root directory of Xcode project.
/// This should point to the directory of '${XcodeProjectName}.xcodeproj'.
/// It is recommended to use relative path here.
/// Current directory is the root directory of this Unity project, i.e. the
directory of 'Assets' folder.
/// Sample value: "../xcode"
/// </summary>
private const string XcodeProjectRoot = "";
/// <summary>
/// Name of the Xcode project.
/// This script looks for '${XcodeProjectName} + ".xcodeproj"' under '$
{XcodeProjectRoot}'.
/// Sample value: "DemoApp"
/// </summary>
private const string XcodeProjectName = "";
/// <summary>
/// Directories, relative to the root directory of the Xcode project, to put
generated Unity iOS build output.
/// </summary>
private const string ClassesProjectPath = XcodeProjectName + "/Unity/Classes";
private const string LibrariesProjectPath = XcodeProjectName +
"/Unity/Libraries";
/// <summary>
/// Path, relative to the root directory of the Xcode project, to put
information about generated Unity output.
/// </summary>
private const string ExportsConfigProjectPath = XcodeProjectName +
"/Unity/Exports.xcconfig";
/// <summary>
/// The identifier added to touched file to avoid double edits when building to
existing directory without
/// replace existing content.
/// </summary>
private const string TouchedMarker = "https://github.com/jiulongw/swift-
unity#v1";
[PostProcessBuild]
public static void OnPostBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iOS)
{
return;
}
PatchUnityNativeCode(pathToBuiltProject);
UpdateUnityIOSExports(pathToBuiltProject);
UpdateUnityProjectFiles(pathToBuiltProject);
File.WriteAllText(configPath, config.ToString());
}
/// <summary>
/// Enumerates Unity output files and add necessary files into Xcode project
file.
/// It only add a reference entry into project.pbx file, without actually copy
it.
/// Xcode pre-build script will copy files into correct location.
/// </summary>
private static void UpdateUnityProjectFiles(string pathToBuiltProject)
{
var pbx = new PBXProject();
var pbxPath = Path.Combine(XcodeProjectRoot, PbxFilePath);
pbx.ReadFromFile(pbxPath);
ProcessUnityDirectory(
pbx,
Path.Combine(pathToBuiltProject, "Classes"),
Path.Combine(XcodeProjectRoot, ClassesProjectPath),
ClassesProjectPath);
ProcessUnityDirectory(
pbx,
Path.Combine(pathToBuiltProject, "Libraries"),
Path.Combine(XcodeProjectRoot, LibrariesProjectPath),
LibrariesProjectPath);
pbx.WriteToFile(pbxPath);
}
/// <summary>
/// Update pbx project file by adding src files and removing extra files that
/// exists in dest but not in src any more.
///
/// This method only updates the pbx project file. It does not copy or delete
/// files in Swift Xcode project. The Swift Xcode project will do copy and
delete
/// during build, and it should copy files if contents are different,
regardless
/// of the file time.
/// </summary>
/// <param name="pbx">The pbx project.</param>
/// <param name="src">The directory where Unity project is built.</param>
/// <param name="dest">The directory of the Swift Xcode project where the
/// Unity project is embedded into.</param>
/// <param name="projectPathPrefix">The prefix of project path in Swift Xcode
/// project for Unity code files. E.g. "DempApp/Unity/Classes" for all files
/// under Classes folder from Unity iOS build output.</param>
private static void ProcessUnityDirectory(PBXProject pbx, string src, string
dest, string projectPathPrefix)
{
var targetGuid = pbx.TargetGuidByName(XcodeProjectName);
if (string.IsNullOrEmpty(targetGuid)) {
throw new Exception(string.Format("TargetGuid could not be found for
'{0}'", XcodeProjectName));
}
extraFilesSet.ExceptWith(srcFiles);
extraFiles = extraFilesSet.ToArray();
}
if (Directory.Exists(directory))
{
foreach (var path in Directory.GetFiles(directory, "*",
SearchOption.AllDirectories))
{
var relative = path.Substring(directory.Length).TrimStart('/');
results.Add(relative);
}
}
return results.ToArray();
}
return false;
}
/// <summary>
/// Make necessary changes to Unity build output that enables it to be embedded
into existing Xcode project.
/// </summary>
private static void PatchUnityNativeCode(string pathToBuiltProject)
{
EditMainMM(Path.Combine(pathToBuiltProject, "Classes/main.mm"));
EditUnityAppControllerH(Path.Combine(pathToBuiltProject,
"Classes/UnityAppController.h"));
EditUnityAppControllerMM(Path.Combine(pathToBuiltProject,
"Classes/UnityAppController.mm"));
if (Application.unityVersion == "2017.1.1f1")
{
EditMetalHelperMM(Path.Combine(pathToBuiltProject,
"Classes/Unity/MetalHelper.mm"));
}
/// <summary>
/// Edit 'main.mm': removes 'main' entry that would conflict with the Xcode
project it embeds into.
/// </summary>
private static void EditMainMM(string path)
{
EditCodeFile(path, line =>
{
if (line.TrimStart().StartsWith("int main", StringComparison.Ordinal))
{
return line.Replace("int main", "int old_main");
}
return line;
});
}
/// <summary>
/// Edit 'UnityAppController.h': returns 'UnityAppController' from
'AppDelegate' class.
/// </summary>
private static void EditUnityAppControllerH(string path)
{
var inScope = false;
var markerDetected = false;
var markerAdded = false;
if (!markerAdded)
{
markerAdded = true;
return new string[]
{
"// Modified by " + TouchedMarker,
"// " + line,
};
}
/// <summary>
/// Edit 'UnityAppController.mm': triggers 'UnityReady' notification after
Unity is actually started.
/// </summary>
private static void EditUnityAppControllerMM(string path)
{
var inScope = false;
var markerDetected = false;
if (markerDetected)
{
return new string[] { line };
}
else
{
return new string[]
{
" // Modified by " + TouchedMarker,
" // Post a notification so that Swift can load unity
view once started.",
@" [[NSNotificationCenter defaultCenter]
postNotificationName: @""UnityReady"" object:self];",
"}",
};
}
}
return new string[] { line };
});
}
/// <summary>
/// Edit 'MetalHelper.mm': fixes a bug (only in 2017.1.1f1) that causes crash.
/// </summary>
private static void EditMetalHelperMM(string path)
{
var markerDetected = false;
/// <summary>
/// Edit 'SplashScreen.mm': Unity introduces its own 'LaunchScreen.storyboard'
since 2017.3.0f3.
/// Disable it here and use Swift project's launch screen instead.
/// </summary>
private static void EditSplashScreenMM(string path) {
var markerDetected = false;
var markerAdded = false;
var inScope = false;
var level = 0;
if (!markerAdded)
{
markerAdded = true;
return new string[]
{
"// Modified by " + TouchedMarker,
line,
};
}
}
File.Move(path, bakPath);
#endif