Last active
November 9, 2018 14:38
-
-
Save Taremin/a0e995809312d1b13d23773b5942f456 to your computer and use it in GitHub Desktop.
アセット更新時に自動的に文字列置換を行うUnityエディタ拡張
This file contains hidden or 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
| { | |
| "replaceSettings": [ | |
| { | |
| "sourcePathPattern": "\/TareminShaderDevelop\\.shader", | |
| "sourcePathReplace": "\/TareminShader.shader", | |
| "replace": [ | |
| { | |
| "textPattern": "(Pass \\{\\s*Name \"Outline\"\\s*Tags \\{)[^\\}]*(\\})", | |
| "textReplace": "$1 \"LightMode\" = \"ForwardBase\" $2" | |
| }, | |
| { | |
| "textPattern": "(Shader \")Taremin/TareminShaderDevelop(\" \\{)", | |
| "textReplace": "$1Taremin/TareminShader$2" | |
| } | |
| ] | |
| } | |
| ] | |
| } |
This file contains hidden or 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
| namespace ReplaceOnAssetsUpdate { | |
| using System.IO; | |
| using System.Text.RegularExpressions; | |
| using UnityEditor; | |
| using UnityEngine; | |
| public class ReplaceOnAssetsUpdateWindow : EditorWindow { | |
| public static string jsonPath = ""; | |
| void OnEnable () { | |
| jsonPath = EditorUserSettings.GetConfigValue ("ReplaceOnAssetsUpdate/jsonPath") ?? ""; | |
| } | |
| // Use this for initialization | |
| [MenuItem ("Window/ReplaceOnAssetsUpdate", false, 20)] | |
| public static void ShowWindow () { | |
| EditorWindow.GetWindow (typeof (ReplaceOnAssetsUpdateWindow)); | |
| } | |
| private void OnGUI () { | |
| EditorGUILayout.LabelField ("JSON Path"); | |
| using (new GUILayout.VerticalScope (GUI.skin.box)) { | |
| EditorGUILayout.LabelField (jsonPath ?? ""); | |
| } | |
| if (GUILayout.Button ("Select JSON")) { | |
| jsonPath = EditorUtility.OpenFilePanel ("Select JSON", Application.dataPath, "json"); | |
| Debug.Log ("JSON:" + jsonPath); | |
| } | |
| EditorUserSettings.SetConfigValue ("ReplaceOnAssetsUpdate/jsonPath", jsonPath); | |
| } | |
| } | |
| [System.Serializable] | |
| public class ReplaceSettings { | |
| public ReplaceSetting[] replaceSettings; | |
| } | |
| [System.Serializable] | |
| public class ReplaceSetting { | |
| public string sourcePathPattern; | |
| public string sourcePathReplace; | |
| public Replace[] replace; | |
| } | |
| [System.Serializable] | |
| public class Replace { | |
| public string textPattern; | |
| public string textReplace; | |
| } | |
| public class ReplaceOnAssetsUpdate : AssetPostprocessor { | |
| static void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { | |
| var jsonPath = EditorUserSettings.GetConfigValue ("ReplaceOnAssetsUpdate/jsonPath") ?? ""; | |
| Debug.Log (jsonPath); | |
| if (jsonPath == "") { | |
| return; | |
| } | |
| var json = File.ReadAllText (jsonPath); | |
| var deserialized = JsonUtility.FromJson<ReplaceSettings> (json); | |
| var update = false; | |
| foreach (string path in importedAssets) { | |
| foreach (ReplaceSetting setting in deserialized.replaceSettings) { | |
| var pathRegex = new Regex (setting.sourcePathPattern); | |
| var pathStr = setting.sourcePathReplace; | |
| if (pathRegex.Match (path).Success) { | |
| var asset = AssetDatabase.LoadAssetAtPath (path, typeof (Object)); | |
| var realpath = AssetDatabase.GetAssetPath (asset); | |
| var replaced = File.ReadAllText (realpath); | |
| var newPath = pathRegex.Replace (path, pathStr); | |
| foreach (Replace replace in setting.replace) { | |
| var replaceRegex = new Regex (replace.textPattern, RegexOptions.Singleline); | |
| var replaceStr = replace.textReplace; | |
| replaced = replaceRegex.Replace (replaced, replaceStr); | |
| } | |
| File.WriteAllText (newPath, replaced); | |
| update = true; | |
| } | |
| } | |
| } | |
| if (update) { | |
| AssetDatabase.Refresh (); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment