Merge pull request #37 from AnnulusGames/feature-persisting-window-data

Feature: persisting window data
This commit is contained in:
Annulus Games 2024-02-18 10:39:29 +09:00 committed by GitHub
commit 99fad8f04c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,7 @@
using System.IO;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
namespace Alchemy.Editor
@ -8,6 +10,10 @@ namespace Alchemy.Editor
{
protected virtual void CreateGUI()
{
// Load data
LoadWindowData(GetWindowDataPath());
// Create root element and serialized object
var windowElement = new VisualElement();
var serializedObject = new SerializedObject(this);
@ -18,7 +24,28 @@ namespace Alchemy.Editor
// Remove "Serialized Data Model Controller" field
windowElement.hierarchy.RemoveAt(0);
// Set callback
windowElement.TrackSerializedObjectValue(serializedObject, so => SaveWindowData(GetWindowDataPath()));
rootVisualElement.Add(windowElement);
}
protected virtual string GetWindowDataPath()
{
return $"ProjectSettings/{GetType().FullName}.json";
}
protected virtual void SaveWindowData(string dataPath)
{
File.WriteAllText(dataPath, JsonUtility.ToJson(this, true));
}
protected virtual void LoadWindowData(string dataPath)
{
if (File.Exists(dataPath))
{
JsonUtility.FromJsonOverwrite(File.ReadAllText(dataPath), this);
}
}
}
}