Skip to content

Commit

Permalink
Merge pull request #152 from mattak/fix/statejson_extension
Browse files Browse the repository at this point in the history
StateJsonEditor should ignore package.json #150
  • Loading branch information
mattak authored Aug 12, 2021
2 parents 1e4fe98 + 19b0ad7 commit fc5c119
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
using UnityEditor;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace Unidux.Experimental.Editor
{
[InitializeOnLoad]
public class StateJsonEditor
{
private const string FILE_EXTENSION = "json";
/// <summary>
/// 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`.
/// </summary>
private static readonly string[] FILE_EXTENSIONS = new[] { "unidux_json", "unidux.json" };

private static StateJsonFileWrapper wrapper = null;
private static bool selectionChanged = false;

Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit fc5c119

Please sign in to comment.