Skip to content

Add Unity post-processing build scripts for iOS and Android #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions unity/Edtior/PostProcessBuild/PostProcessingBuildAndroid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
using System.IO;
using System.Xml;

public class PostProcessingBuildAndroid
{
[PostProcessBuild(1)]
public static void OnPostBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.Android) return;

var manifestPath = Path.Combine(pathToBuiltProject, "src", "main", "AndroidManifest.xml");
if (!File.Exists(manifestPath)) return;

XmlDocument manifest = new XmlDocument();
manifest.Load(manifestPath);

XmlNode appNode = manifest.SelectSingleNode("/manifest/application");

if (appNode != null)
{
foreach (XmlNode activityNode in appNode.SelectNodes(".//activity"))
{
var intentFilters = activityNode.SelectNodes("intent-filter");
for (int i = intentFilters.Count - 1; i >= 0; i--)
{
activityNode.RemoveChild(intentFilters[i]);
}
}
}

manifest.Save(manifestPath);
Debug.Log("[Post Build] Android : Removed intent-filters from AndroidManifest.xml Success!.");
}
}
53 changes: 53 additions & 0 deletions unity/Edtior/PostProcessBuild/PostProcessingBuildIOS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using UnityEngine;

public class PostBuildIOS
{
[PostProcessBuild]
public static void OnPostBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iOS) return;

string projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
PBXProject proj = new PBXProject();
proj.ReadFromFile(projPath);

#if UNITY_2019_3_OR_NEWER
string frameworkTarget = proj.GetUnityFrameworkTargetGuid();
string mainTarget = proj.GetUnityMainTargetGuid();
#else
string frameworkTarget = proj.TargetGuidByName("UnityFramework");
string mainTarget = proj.TargetGuidByName("Unity-iPhone");
#endif

// Change Data folder to UnityFramework
string dataFolderPath = "Data";
string dataFolderGuid = proj.FindFileGuidByProjectPath(dataFolderPath);
if (!string.IsNullOrEmpty(dataFolderGuid))
{
proj.RemoveFileFromBuild(mainTarget, dataFolderGuid);
proj.AddFileToBuild(frameworkTarget, dataFolderGuid);
Debug.Log("[Post Build] iOS : 'Data' folder reassigned to UnityFramework.");
}

// Change NativeCallProxy.h to Public
string headerPath = "Libraries/Plugins/iOS/NativeCallProxy.h";
string headerGuid = proj.FindFileGuidByProjectPath(headerPath);

if (!string.IsNullOrEmpty(headerGuid))
{
proj.RemoveFileFromBuild(mainTarget, headerGuid);
proj.AddPublicHeaderToBuild(frameworkTarget, headerGuid);

Debug.Log("[Post Build] iOS : NativeCallProxy.h set as Public in UnityFramework.");
}
else
{
Debug.LogWarning("[Post Build] iOS : NativeCallProxy.h not found. Check Unity export path.");
}

proj.WriteToFile(projPath);
}
}