Skip to content

Instantly share code, notes, and snippets.

@litefeel
Forked from keijiro/PbxModifier.cs
Last active September 7, 2016 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save litefeel/7873a91b05910a16d9154894238cd214 to your computer and use it in GitHub Desktop.
Save litefeel/7873a91b05910a16d9154894238cd214 to your computer and use it in GitHub Desktop.
(Unity Xcode Manipulation API) An example which modifies compiler flags for a given source file.
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.IO;
public class PbxModifier
{
[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
{
if (buildTarget == BuildTarget.iOS) {
string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
string target = proj.TargetGuidByName("Unity-iPhone");
string file = proj.FindFileGuidByProjectPath("Classes/UnityAppController.mm");
var flags = proj.GetCompileFlagsForFile(target, file);
flags.Add("-fno-objc-arc");
proj.SetCompileFlagsForFile(target, file, flags);
File.WriteAllText(projPath, proj.WriteToString());
}
}
}
// link form http://qiita.com/tkyaji/items/19dfff4afe228c7f34a1
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.Collections.Generic;
using System.Text;
using System.IO;
public class PostBuildProcess {
[PostProcessBuild]
public static void OnPostProcessBuild (BuildTarget buildTarget, string path) {
if (buildTarget == BuildTarget.iOS) {
processForiOS (path);
}
}
private static void processForiOS (string path) {
string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
PBXProject proj = new PBXProject ();
proj.ReadFromString (File.ReadAllText (projPath));
string target = proj.TargetGuidByName ("Unity-iPhone");
// tbd追加
string tbdName = "libsqlite3.tbd";
proj.AddFileToBuild(target, proj.AddFile("usr/lib/" + tbdName, "Frameworks/" + tbdName, PBXSourceTree.Sdk));
// IOS9支持.tbd扩展名的库, Unity的PBXProject不支持将.tbd增加到 Link Binary With Libraaries
// 需要手动支持Link Binary With Libraaries
string projText = addTbdLibrary (target, proj.WriteToString (), tbdName);
File.WriteAllText(projPath, projText);
}
// .pbxproj 修正 .tbd 库为 Link Binary With Libraries
private static string addTbdLibrary(string target, string projText, string tbdName) {
string[] lines = projText.Split ('\n');
List<string> newLines = new List<string> ();
string refId = null;
bool editFinish = false;
for (int i = 0; i < lines.Length; i++) {
string line = lines [i];
if (editFinish) {
newLines.Add (line);
} else if (line.IndexOf (tbdName) > -1) {
if (refId == null && line.IndexOf ("PBXBuildFile") > -1) {
refId = line.Substring (0, line.IndexOf ("/*")).Trim ();
} else if (line.IndexOf ("lastKnownFileType") > -1) {
line = line.Replace ("lastKnownFileType = file;", "lastKnownFileType = \"sourcecode.text-based-dylib-definition\";");
}
newLines.Add (line);
} else if (line.IndexOf ("isa = PBXFrameworksBuildPhase;") > -1) {
do {
newLines.Add (line);
line = lines [++i];
} while (line.IndexOf("files = (") == -1);
while (true) {
if (line.IndexOf (")") > -1) {
newLines.Add (refId + ",");
newLines.Add (line);
break;
} else if (line.IndexOf (refId) > -1) {
newLines.Add (line);
break;
} else {
newLines.Add (line);
line = lines [++i];
}
}
editFinish = true;
} else {
newLines.Add (line);
}
}
return string.Join ("\n", newLines.ToArray ());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment