Skip to content

Instantly share code, notes, and snippets.

@anzfactory
Last active August 29, 2015 14:20
Show Gist options
  • Save anzfactory/0f6d6c961d52e7651ff3 to your computer and use it in GitHub Desktop.
Save anzfactory/0f6d6c961d52e7651ff3 to your computer and use it in GitHub Desktop.
Unityでフォルダ階層にしたがってnamespaceをシンボル置換してくれるスクリプト
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class SymbolReplacer : UnityEditor.AssetModificationProcessor {
public static void OnWillCreateAsset (string path) {
// メタファイルからファイルパスを
path = path.Replace(".meta", "");
// 拡張子チェック
int index = path.LastIndexOf(".");
// フォルダなどの場合は拡張子なし
if (index < 0) {
return;
}
string ext = path.Substring(index);
if (ext != ".cs" && ext != ".js" && ext != ".boo") {
return;
}
// 対象ファイルの読み込み
index = Application.dataPath.LastIndexOf("Assets");
string fullPath = Application.dataPath.Substring(0, index) + path;
string file = System.IO.File.ReadAllText(fullPath);
// カスタム置換シンボルを走査して変換
file = file.Replace("#CREATED#", System.DateTime.Now.ToString("yyy-MM-dd"));
file = file.Replace("#PROJECT_NAME#", PlayerSettings.productName);
List<string> array = path.Split('/').ToList();
index = array.LastIndexOf("Scripts");
if (index >= 0) { // 見つかった場合
array[index] = "App"; // namespace用にScriptsをAppに(これは好み)
array.RemoveRange(0, index); // App(Scripsts)上位を削除
array.Remove(array.Last()); // ファイル名部分を削除
string pathBelowScripts = string.Join(".", array.ToArray()); // ドットでつなげる
file = file.Replace( "#NAMESPACE#", pathBelowScripts);
}
// ファイル上書きで作成完了
System.IO.File.WriteAllText(fullPath, file);
AssetDatabase.Refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment