killapp/Assets/Editor/FirebaseIosPodfilePostProcessor.cs

82 lines
2.6 KiB
C#
Raw Normal View History

2026-06-18 09:20:04 +08:00
#if UNITY_IOS
using UnityEditor;
using UnityEditor.Callbacks;
using System.IO;
using System.Diagnostics;
public class FirebaseIosPodfilePostProcessor
{
[PostProcessBuild(100)] // 优先级设为 100在其他后处理之后执行
public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iOS) return;
string podfilePath = Path.Combine(pathToBuiltProject, "Podfile");
// 生成 Podfile
string podfileContent = @"source 'https://cdn.cocoapods.org/'
platform :ios, '15.0'
target 'UnityFramework' do
pod 'Firebase/Core', '12.4.0'
pod 'Firebase/Auth', '12.4.0'
pod 'Firebase/Database', '12.4.0'
pod 'Firebase/Messaging', '12.4.0'
end
target 'Unity-iPhone' do
end
use_frameworks! :linkage => :static
";
File.WriteAllText(podfilePath, podfileContent);
UnityEngine.Debug.Log("[FirebaseIosPostProcessor] Podfile 已生成: " + podfilePath);
// 尝试自动运行 pod install
TryRunPodInstall(pathToBuiltProject);
}
private static void TryRunPodInstall(string projectPath)
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "pod",
Arguments = "install",
WorkingDirectory = projectPath,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (Process process = Process.Start(startInfo))
{
string stdout = process.StandardOutput.ReadToEnd();
string stderr = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode == 0)
{
UnityEngine.Debug.Log("[FirebaseIosPostProcessor] pod install 成功");
}
else
{
UnityEngine.Debug.LogWarning("[FirebaseIosPostProcessor] pod install 失败:\n" + stderr);
UnityEngine.Debug.LogWarning("[FirebaseIosPostProcessor] 请手动在 " + projectPath + " 目录下执行 pod install");
}
}
}
catch (System.Exception e)
{
UnityEngine.Debug.LogWarning("[FirebaseIosPostProcessor] 未找到 CocoaPods请手动执行:\n" +
" 1. 安装 CocoaPods: sudo gem install cocoapods\n" +
" 2. cd " + projectPath + "\n" +
" 3. pod install\n" +
" 错误: " + e.Message);
}
}
}
#endif