mirror of
https://github.com/AnnulusGames/Alchemy.git
synced 2025-01-22 08:18:51 -05:00
Merge pull request #37 from AnnulusGames/feature-persisting-window-data
Feature: persisting window data
This commit is contained in:
commit
99fad8f04c
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user