Created
May 24, 2015 14:42
-
-
Save edom18/57b59c45e85028611805 to your computer and use it in GitHub Desktop.
[Unity] PostProcessでビルド後に処理を差し込む ref: http://qiita.com/edo_m18/items/346439f7678218e85e69
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C# example: | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.Callbacks; | |
public class MyBuildPostprocessor { | |
[PostProcessBuildAttribute(1)] | |
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) { | |
Debug.Log( pathToBuiltProject ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.Callbacks; | |
using System.Collections; | |
using System.IO; | |
public class PostProcessTest { | |
[PostProcessBuild (100)] | |
public static void OnPostProcessBuild(BuildTarget target, string path) { | |
#if UNITY_5_0 | |
if (target == BuildTarget.iOS) { | |
#else | |
if (target == BuildTarget.iPhone) { | |
#endif | |
RewriteObjCFiles(path); | |
} | |
} | |
/// <summary> | |
/// Rewrites the Objective-C files. | |
/// </summary> | |
/// <param name="path">File path.</param> | |
private static void RewriteObjCFiles(string path) { | |
string filePath = System.IO.Path.Combine(path, "Classes/UnityAppController.mm"); | |
if (File.Exists(filePath)) { | |
ReplaceStringInFile(filePath, "OpenGLES", "OpenGLESHoge"); | |
} | |
} | |
/// <summary> | |
/// Replaces the string in the file. | |
/// </summary> | |
/// <param name="file">File path</param> | |
/// <param name="target">Target string</param> | |
/// <param name="replaceText">Replace text</param> | |
private static void ReplaceStringInFile(string file, string target, string replaceText) { | |
if (!File.Exists(file)) { | |
return; | |
} | |
// 置換後のテキスト | |
string processedContents = ""; | |
using (StreamReader stream = new StreamReader(file)) { | |
while (stream.Peek() >= 0) { | |
string line = stream.ReadLine(); | |
processedContents += line.Replace(target, replaceText) + "\n"; | |
} | |
} | |
// 既存ファイルを削除し、置換後のテキストで新規作成 | |
File.Delete(file); | |
using (StreamWriter stream = File.CreateText(file)) { | |
stream.Write(processedContents); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment