Skip to content

Instantly share code, notes, and snippets.

@suakig
Last active August 23, 2021 15:20
Show Gist options
  • Save suakig/ffb64f48c800e9dea545 to your computer and use it in GitHub Desktop.
Save suakig/ffb64f48c800e9dea545 to your computer and use it in GitHub Desktop.
PostBuildProcess.cs
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.IO;
using System.Collections.Generic;
public class PostBuildProcess : MonoBehaviour
{
internal static void CopyAndReplaceDirectory(string srcPath, string dstPath)
{
if (Directory.Exists(dstPath))
Directory.Delete(dstPath);
if (File.Exists(dstPath))
File.Delete(dstPath);
Directory.CreateDirectory(dstPath);
foreach (var file in Directory.GetFiles(srcPath))
File.Copy(file, Path.Combine(dstPath, Path.GetFileName(file)));
foreach (var dir in Directory.GetDirectories(srcPath))
CopyAndReplaceDirectory(dir, Path.Combine(dstPath, Path.GetFileName(dir)));
}
[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
{
if (buildTarget == BuildTarget.iPhone) {
BuildForiOS(path);
}
}
private static void BuildForiOS(string path)
{
string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
Debug.Log("Build iOS. path: " + projPath);
PBXProject proj = new PBXProject();
var file = File.ReadAllText(projPath);
proj.ReadFromString(file);
string target = proj.TargetGuidByName("Unity-iPhone");
List<string> frameworks = new List<string>();
frameworks.Add("GoogleOpenSource.framework");
frameworks.Add("GooglePlus.bundle");
frameworks.Add("GooglePlus.framework");
foreach (string framework in frameworks) {
CopyAndReplaceDirectory("Frameworks/google-plus-ios-sdk-1.7.1/" + framework, Path.Combine(path, "Frameworks/" + framework));
string name = proj.AddFile("Frameworks/" + framework, "Frameworks/" + framework, PBXSourceTree.Source);
Debug.Log("Add File: " + framework);
proj.AddFileToBuild(target, name);
}
frameworks.Clear();
frameworks.Add("gpg.bundle");
frameworks.Add("gpg.framework");
foreach (string framework in frameworks) {
CopyAndReplaceDirectory("Frameworks/gpg-cpp-sdk/ios/" + framework, Path.Combine(path, "Frameworks/" + framework));
string name = proj.AddFile("Frameworks/" + framework, "Frameworks/" + framework, PBXSourceTree.Source);
Debug.Log("Add File: " + framework);
proj.AddFileToBuild(target, name);
}
proj.AddFrameworkToProject(target, "AddressBook.framework", false);
proj.AddFrameworkToProject(target, "AssetsLibrary.framework", false);
proj.AddFrameworkToProject(target, "CoreData.framework", false);
proj.AddFrameworkToProject(target, "CoreTelephony.framework", false);
proj.AddFrameworkToProject(target, "CoreText.framework", false);
proj.AddFrameworkToProject(target, "Security.framework", false);
proj.AddFrameworkToProject(target, "WebKit.framework", false);
proj.AddFrameworkToProject(target, "ImageIO.framework", false);
proj.AddFrameworkToProject(target, "EventKit.framework", false);
proj.AddFrameworkToProject(target, "EventKitUI.framework", false);
proj.AddFrameworkToProject (target, "AdSupport.framework", false);
proj.AddFrameworkToProject (target, "AudioToolbox.framework", false);
proj.AddFrameworkToProject (target, "AVFoundation.framework", false);
proj.AddFrameworkToProject (target, "CoreGraphics.framework", false);
proj.AddFrameworkToProject (target, "EventKit.framework", false);
proj.AddFrameworkToProject (target, "EventKitUI.framework", false);
proj.AddFrameworkToProject (target, "MessageUI.framework", false);
proj.AddFrameworkToProject (target, "StoreKit.framework", false);
proj.AddFrameworkToProject (target, "SystemConfiguration.framework", false);
CopyAndReplaceDirectory("Frameworks/GoogleMobileAdsSdkiOS-7.1.0/GoogleMobileAds.framework", Path.Combine(path, "Frameworks/GoogleMobileAds.framework"));
proj.AddFileToBuild(target, proj.AddFile("Frameworks/GoogleMobileAds.framework", "Frameworks/GoogleMobileAds.framework", PBXSourceTree.Source));
proj.SetBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(inherited)");
proj.AddBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(PROJECT_DIR)/Libraries");
AddUsrLib(proj, target, "libc++.dylib");
AddUsrLib(proj, target, "libz.dylib");
proj.SetBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(SRCROOT)/Frameworks");
proj.AddBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(inherited)");
proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-ObjC");
File.WriteAllText(projPath, proj.WriteToString());
}
private static void AddUsrLib(PBXProject proj, string targetGuid, string framework)
{
string fileGuid = proj.AddFile("usr/lib/"+framework, "Frameworks/"+framework, PBXSourceTree.Sdk);
proj.AddFileToBuild(targetGuid, fileGuid);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment