From 19b0ad7f572a7996116b477e87b0aa50e81054f7 Mon Sep 17 00:00:00 2001 From: mattak Date: Thu, 12 Aug 2021 22:20:52 +0900 Subject: [PATCH] :recycle: fix/statejson_extension change default file name extension for using Unidux StatePanel. It avoids UnityEditor drawer for package.json. --- .../Scripts/Experimental/Editor/StateJsonEditor.cs | 13 ++++++++++--- .../Experimental/Editor/UniduxPanelSettingTab.cs | 10 ++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Assets/Plugins/Unidux/Scripts/Experimental/Editor/StateJsonEditor.cs b/Assets/Plugins/Unidux/Scripts/Experimental/Editor/StateJsonEditor.cs index ee77d02..ed2a34b 100644 --- a/Assets/Plugins/Unidux/Scripts/Experimental/Editor/StateJsonEditor.cs +++ b/Assets/Plugins/Unidux/Scripts/Experimental/Editor/StateJsonEditor.cs @@ -1,4 +1,5 @@ -using UnityEditor; +using System.Linq; +using UnityEditor; using UnityEngine; namespace Unidux.Experimental.Editor @@ -6,7 +7,13 @@ namespace Unidux.Experimental.Editor [InitializeOnLoad] public class StateJsonEditor { - private const string FILE_EXTENSION = "json"; + /// + /// Unidux specific extension is defined for avoiding conflicts of UnityEngine or the other assets json handling. + /// `.unidux_json` extension is defined for using EditorUtility.SaveFilePanel. + /// Because the function cannot handle double dot filename such as `.unidux.json`. + /// + private static readonly string[] FILE_EXTENSIONS = new[] { "unidux_json", "unidux.json" }; + private static StateJsonFileWrapper wrapper = null; private static bool selectionChanged = false; @@ -33,7 +40,7 @@ private static void Update() if (Selection.activeObject != wrapper) { string fn = AssetDatabase.GetAssetPath(Selection.activeInstanceID); - if (fn.ToLower().EndsWith(FILE_EXTENSION)) + if (FILE_EXTENSIONS.Any(extension => fn.ToLower().EndsWith(extension))) { if (wrapper == null) { diff --git a/Assets/Plugins/Unidux/Scripts/Experimental/Editor/UniduxPanelSettingTab.cs b/Assets/Plugins/Unidux/Scripts/Experimental/Editor/UniduxPanelSettingTab.cs index 1aa6966..ed39e4e 100644 --- a/Assets/Plugins/Unidux/Scripts/Experimental/Editor/UniduxPanelSettingTab.cs +++ b/Assets/Plugins/Unidux/Scripts/Experimental/Editor/UniduxPanelSettingTab.cs @@ -9,7 +9,7 @@ public class UniduxPanelSettingTab { private const string DefaultStateJsonPath = "Assets/state.json"; private const string JsonPathKey = "UniduxPanel.JsonSavePath"; - + public void Render(IStoreAccessor _store, ISerializeFactory serializer) { if (_store == null) @@ -39,7 +39,8 @@ public void Render(IStoreAccessor _store, ISerializeFactory serializer) EditorGUILayout.BeginHorizontal(); if (!existsJson && GUILayout.Button("Create")) { - jsonPath = EditorUtility.SaveFilePanel("Create empty json", "Assets", "state.json", "json"); + // XXX: Unity cannot handle the extension including `.` character. such as `.unidux.json`. So we use `unidux_json` + jsonPath = EditorUtility.SaveFilePanel("Create empty json", "Assets", "state", "unidux_json"); jsonPath = ReplaceJsonPath(jsonPath); if (!string.IsNullOrEmpty(jsonPath)) { @@ -68,15 +69,16 @@ public void Render(IStoreAccessor _store, ISerializeFactory serializer) var content = File.ReadAllBytes(jsonPath); _store.StoreObject.ObjectState = serializer.Deserialize(content, _store.StoreObject.StateType); } + EditorGUILayout.EndHorizontal(); } - + private string ReplaceJsonPath(string path) { var projectPath = Application.dataPath.Replace("Assets", ""); return path.Replace(projectPath, ""); } - + private void RecordJsonPath(string path) { EditorPrefs.SetString(JsonPathKey, path);