You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PlumberUltimateAds/Assets/Scripts/GoogleSignInIOSBuildPatch.cs

77 lines
2.7 KiB
C#

#if UNITY_IOS && UNITY_EDITOR
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using UnityEngine;
using System.IO;
public class GoogleSignInIOSBuildPatch
{
[PostProcessBuild(100)]
public static void OnPostProcessBuild(BuildTarget buildTarget, string pathToBuiltProject)
{
if (buildTarget != BuildTarget.iOS)
return;
string plistPath = Path.Combine(pathToBuiltProject, "Info.plist");
// === Modify Info.plist ===
PlistDocument plist = new PlistDocument();
plist.ReadFromFile(plistPath);
PlistElementDict rootDict = plist.root;
rootDict.SetString("GIDClientID", GameConstants.iOS_ClientID);
PlistElementArray urlTypes = rootDict.CreateArray("CFBundleURLTypes");
PlistElementDict urlDict = urlTypes.AddDict();
PlistElementArray schemes = urlDict.CreateArray("CFBundleURLSchemes");
schemes.AddString(GameConstants.iOS_URL_Scheme);
plist.WriteToFile(plistPath);
Debug.Log("✅ Patched Info.plist with GIDClientID and URL scheme.");
// === Modify UnityAppController.mm ===
string appControllerPath = Path.Combine(pathToBuiltProject, "Classes", "UnityAppController.mm");
if (!File.Exists(appControllerPath))
{
Debug.LogWarning("⚠️ UnityAppController.mm not found.");
return;
}
string content = File.ReadAllText(appControllerPath);
// 1. Inject #import if missing
string importLine = "#import <GoogleSignIn/GoogleSignIn.h>";
if (!content.Contains(importLine))
{
int importInsertIndex = content.IndexOf("#import");
if (importInsertIndex >= 0)
{
int endOfLine = content.IndexOf("\n", importInsertIndex);
content = content.Insert(endOfLine + 1, importLine + "\n");
}
}
// 2. Inject GIDSignIn client ID line
string injection = $"[GIDSignIn sharedInstance].clientID = @\"{GameConstants.iOS_ClientID}\";";
if (!content.Contains(injection))
{
string marker = "didFinishLaunchingWithOptions:(NSDictionary*)launchOptions";
int markerIndex = content.IndexOf(marker);
if (markerIndex >= 0)
{
int braceIndex = content.IndexOf("{", markerIndex);
if (braceIndex >= 0)
{
content = content.Insert(braceIndex + 1, "\n " + injection);
}
}
}
File.WriteAllText(appControllerPath, content);
Debug.Log("✅ Patched UnityAppController.mm with GIDSignIn setup.");
}
}
#endif