2025-07-03 12:59:14 +04:00

102 lines
3.5 KiB
C#

#if UNITY_IOS
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using UnityEngine;
public class PostprocessIOSMainMM
{
[PostProcessBuild(999)]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iOS)
return;
var pbx = new PBXProject();
var pbxPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
pbx.ReadFromFile(pbxPath);
// UNITY MACRO
#if SDK
var projectGuid = pbx.ProjectGuid();
// Enable MACRO for Swift code
// pbx.SetBuildProperty(projectGuid, "SWIFT_ACTIVE_COMPILATION_CONDITIONS", "EXAMPLE_UNITY_BUILD_FLAG");
// Enable MACRO for C++ code
pbx.SetBuildProperty(projectGuid, "GCC_PREPROCESSOR_DEFINITIONS", "SDK");
#endif
// Add WebKit framework to the Xcode project
var mainTarget = pbx.GetUnityFrameworkTargetGuid();
pbx.AddFrameworkToProject(mainTarget, "WebKit.framework", true); // Link it
// Save the modified project file
pbx.WriteToFile(pbxPath);
string projectPath = Path.Combine(pathToBuiltProject, "Classes", "main.mm");
if (!File.Exists(projectPath))
{
Debug.LogError("main.mm not found at: " + projectPath);
return;
}
string basePath = Path.Combine(Application.dataPath, "Editor");
string p1TemplatePath = Path.Combine(basePath, "mainTemplateP1.txt");
string p2TemplatePath = Path.Combine(basePath, "mainTemplateP2.txt");
if (!File.Exists(p1TemplatePath) || !File.Exists(p2TemplatePath))
{
Debug.LogError("One or both template files not found at \n " + p1TemplatePath + "\n" + p2TemplatePath);
return;
}
string[] lines = File.ReadAllLines(projectPath);
string p1Content = File.ReadAllText(p1TemplatePath);
string p2Content = File.ReadAllText(p2TemplatePath);
// 1. Insert P1 content after last #import
int lastImportIndex = -1;
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Trim().StartsWith("void UnityInitTrampoline()"))
lastImportIndex = i;
}
if (lastImportIndex >= 0)
{
var lineList = new System.Collections.Generic.List<string>(lines);
lineList.Insert(lastImportIndex, p1Content);
lines = lineList.ToArray();
}
// 2. Extract original body of runUIApplicationMainWithArgc
string source = string.Join("\n", lines);
var match = Regex.Match(source,
@"- \(void\)runUIApplicationMainWithArgc:\(int\)argc argv:\(char\*\[\]\)argv\s*\{([\s\S]*?)\}",
RegexOptions.Multiline);
if (!match.Success)
{
Debug.LogError("Couldn't find runUIApplicationMainWithArgc method.");
return;
}
string originalBody = match.Groups[1].Value.Trim();
string replacedBody = p2Content.Replace("///PUT ORIGINAL CODE FROM UNITY HERE", originalBody);
string fullOriginalMethod = match.Value;
string fullModifiedMethod = fullOriginalMethod.Replace(match.Groups[1].Value, "\n" + replacedBody + "\n");
// 3. Replace in source and save
string updatedSource = source.Replace(fullOriginalMethod, fullModifiedMethod);
File.WriteAllText(projectPath, updatedSource);
Debug.Log("main.mm successfully postprocessed.");
}
}
#endif