Work State Serialization

- Worked on serialization issues (some string data doesn't get correctly formatted, but was also not needed, so removed the data part from ScenePartition)
- Moved some files to runtime (wip)
This commit is contained in:
max 2023-06-26 01:03:14 +02:00
parent 3bba6da1a6
commit b6f9052cff
12 changed files with 169 additions and 101 deletions

View File

@ -5,10 +5,12 @@ namespace VertexColor.ScenePartition.Editor
[CreateAssetMenu(fileName = "Scene", menuName = "Max/ScenePartitionDataSO")] [CreateAssetMenu(fileName = "Scene", menuName = "Max/ScenePartitionDataSO")]
public class ScenePartitionDataSO : ScriptableObject public class ScenePartitionDataSO : ScriptableObject
{ {
public bool hasCreatedPartitions => scenePartitions != null && scenePartitions.Count > 0; public bool HasCreatedPartitions => ScenePartitions != null && ScenePartitions.Count > 0;
public SerializableSortedList<uint, ScenePartition> scenePartitions = new SerializableSortedList<uint, ScenePartition>(); public ScenePartitionSortedList ScenePartitions = new ScenePartitionSortedList();
public bool hasLoadedPartitions => loadedScenePartitions != null && loadedScenePartitions.Count > 0; public bool HasLoadedPartitions => LoadedScenePartitions != null && LoadedScenePartitions.Count > 0;
public SerializableSortedList<uint, ScenePartition> loadedScenePartitions = new SerializableSortedList<uint, ScenePartition>(); public ScenePartitionSortedList LoadedScenePartitions = new ScenePartitionSortedList();
public SceneGrid SceneGrid = new SceneGrid();
} }
} }

View File

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEditor; using UnityEditor;
using UnityEngine;
namespace VertexColor.ScenePartition.Editor namespace VertexColor.ScenePartition.Editor
{ {
@ -18,48 +19,35 @@ public override void OnInspectorGUI()
EditorGUILayout.Space(); EditorGUILayout.Space();
if (GUILayout.Button("Force Serialize"))
{
AssetDatabase.ForceReserializeAssets(new string[] { AssetDatabase.GetAssetPath(scenePartitionDataSO) }, ForceReserializeAssetsOptions.ReserializeAssetsAndMetadata);
}
using (new EditorGUI.DisabledGroupScope(true)) using (new EditorGUI.DisabledGroupScope(true))
{ {
if (scenePartitionDataSO.hasCreatedPartitions) if (scenePartitionDataSO.HasCreatedPartitions)
{ {
EditorGUILayout.LabelField($"scenePartitions"); EditorGUILayout.LabelField($"scenePartitions");
foreach (KeyValuePair<uint, ScenePartition> scenePartition in scenePartitionDataSO.scenePartitions) foreach (KeyValuePair<uint, ScenePartition> scenePartition in scenePartitionDataSO.ScenePartitions)
{ {
EditorGUILayout.IntField((int)scenePartition.Value.id); EditorGUILayout.IntField((int)scenePartition.Value.id);
if (scenePartition.Value.references != null && scenePartition.Value.references.Count > 0)
{
EditorGUI.indentLevel++;
foreach (var reference in scenePartition.Value.references)
{
EditorGUILayout.IntField((int)reference);
}
EditorGUI.indentLevel--;
}
} }
} }
EditorGUILayout.Space(); EditorGUILayout.Space();
if (scenePartitionDataSO.hasLoadedPartitions) if (scenePartitionDataSO.HasLoadedPartitions)
{ {
EditorGUILayout.LabelField($"loadedScenePartitions"); EditorGUILayout.LabelField($"loadedScenePartitions");
foreach (KeyValuePair<uint, ScenePartition> scenePartition in scenePartitionDataSO.loadedScenePartitions) foreach (KeyValuePair<uint, ScenePartition> scenePartition in scenePartitionDataSO.LoadedScenePartitions)
{ {
EditorGUILayout.IntField((int)scenePartition.Value.id); EditorGUILayout.IntField((int)scenePartition.Value.id);
if (scenePartition.Value.references != null && scenePartition.Value.references.Count > 0)
{
EditorGUI.indentLevel++;
foreach (var reference in scenePartition.Value.references)
{
EditorGUILayout.IntField((int)reference);
}
EditorGUI.indentLevel--;
}
} }
} }
} }
} }
} }
} }

View File

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text.RegularExpressions;
using UnityEditor; using UnityEditor;
using UnityEditor.SceneManagement; using UnityEditor.SceneManagement;
using UnityEngine; using UnityEngine;
@ -11,15 +12,17 @@ namespace VertexColor.ScenePartition.Editor
public class ScenePartitionSO : ScriptableObject public class ScenePartitionSO : ScriptableObject
{ {
[field: SerializeField] [field: SerializeField]
public SceneAsset sceneAsset { get; private set; } = null; public SceneAsset SceneAsset { get; private set; } = null;
public string sceneName => sceneAsset == null ? name : sceneAsset.name; public string SceneName => SceneAsset == null ? name : SceneAsset.name;
public ScenePartitionDataSO scenePartitionData = null;
[SerializeField]
private ScenePartitionDataSO scenePartitionData = null;
public List<uint> alwaysLoadIds = new List<uint> { 0, 1, 2, 3, 4 }; public List<uint> alwaysLoadIds = new List<uint> { 0, 1, 2, 3, 4 };
public void CreateScene() public void CreateScene()
{ {
if (sceneAsset != null) return; if (SceneAsset != null) return;
string scenePath = Path.Combine(AssetDatabase.GetAssetPath(this), $"../{this.name}.unity"); string scenePath = Path.Combine(AssetDatabase.GetAssetPath(this), $"../{this.name}.unity");
@ -28,7 +31,7 @@ public void CreateScene()
AssetDatabase.Refresh(); AssetDatabase.Refresh();
sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(scenePath); SceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(scenePath);
AssetDatabase.SaveAssets(); AssetDatabase.SaveAssets();
AssetDatabase.Refresh(); AssetDatabase.Refresh();
@ -39,9 +42,13 @@ public void CreateScene()
/// </summary> /// </summary>
public void LoadAll() public void LoadAll()
{ {
AssetDatabase.StartAssetEditing();
CreateScenePartitions(); CreateScenePartitions();
SortedSet<uint> ids = new SortedSet<uint>(scenePartitionData.scenePartitions.Keys); SortedSet<uint> ids = new SortedSet<uint>(scenePartitionData.ScenePartitions.Keys);
LoadScenePartitions(ids); LoadScenePartitions(ids);
AssetDatabase.StopAssetEditing();
} }
private void CreateScenePartitions() private void CreateScenePartitions()
@ -49,29 +56,29 @@ private void CreateScenePartitions()
string dataPath = ScenePartitionUtils.GetDataPath(this); string dataPath = ScenePartitionUtils.GetDataPath(this);
string[] files = Directory.GetFiles(dataPath); string[] files = Directory.GetFiles(dataPath);
scenePartitionData.scenePartitions = new SerializableSortedList<uint, ScenePartition>(); scenePartitionData.ScenePartitions = new ScenePartitionSortedList();
for (int i = 0; i < files.Length; i++) for (int i = 0; i < files.Length; i++)
{ {
ScenePartition scenePartition = new ScenePartition(files[i]); ScenePartition scenePartition = new ScenePartition(files[i]);
scenePartitionData.scenePartitions.Add(scenePartition.id, scenePartition); scenePartitionData.ScenePartitions.Add(scenePartition.id, scenePartition);
} }
} }
private void LoadScenePartitions(SortedSet<uint> partitionIds) private void LoadScenePartitions(SortedSet<uint> partitionIds)
{ {
if (!scenePartitionData.hasCreatedPartitions) return; if (!scenePartitionData.HasCreatedPartitions) return;
string scenePath = ScenePartitionUtils.GetScenePath(this); string scenePath = ScenePartitionUtils.GetScenePath(this);
List<string> data = new List<string>(); List<string> data = new List<string>();
scenePartitionData.loadedScenePartitions.Clear(); scenePartitionData.LoadedScenePartitions.Clear();
// Add default ids. // Add default ids.
for (int i = 0; i < alwaysLoadIds.Count; i++) for (int i = 0; i < alwaysLoadIds.Count; i++)
{ {
if (scenePartitionData.scenePartitions.ContainsKey(alwaysLoadIds[i])) if (scenePartitionData.ScenePartitions.ContainsKey(alwaysLoadIds[i]))
{ {
partitionIds.Add(alwaysLoadIds[i]); partitionIds.Add(alwaysLoadIds[i]);
} }
@ -80,9 +87,9 @@ private void LoadScenePartitions(SortedSet<uint> partitionIds)
// Create scene data. // Create scene data.
foreach (uint id in partitionIds) foreach (uint id in partitionIds)
{ {
ScenePartition p = scenePartitionData.scenePartitions[id]; ScenePartition p = scenePartitionData.ScenePartitions[id];
data.AddRange(p.data); data.AddRange(File.ReadAllLines(p.filePath));
scenePartitionData.loadedScenePartitions.Add(p.id, p); scenePartitionData.LoadedScenePartitions.Add(p.id, p);
} }
// Create scene. // Create scene.
@ -96,33 +103,41 @@ private void LoadScenePartitions(SortedSet<uint> partitionIds)
/// </summary> /// </summary>
public void Save() public void Save()
{ {
if (!scenePartitionData.hasCreatedPartitions) return; if (!scenePartitionData.HasCreatedPartitions) return;
if (!scenePartitionData.hasLoadedPartitions) return; if (!scenePartitionData.HasLoadedPartitions) return;
DeleteLoadedPartitions(); // Delete the loaded partitions from disk so we can write the new ones. DeleteLoadedPartitions(); // Delete the loaded partitions from disk so we can write the new ones.
string pattern = @"&(\d+)";
string dataPath = ScenePartitionUtils.GetDataPath(this); string dataPath = ScenePartitionUtils.GetDataPath(this);
string scenePath = ScenePartitionUtils.GetScenePath(this); string scenePath = ScenePartitionUtils.GetScenePath(this);
// Read the data from the scene file.
string[] data = File.ReadAllLines(scenePath); string[] data = File.ReadAllLines(scenePath);
// Split it into blocks.
int lastIndex = data.Length; int lastIndex = data.Length;
for (int i = data.Length - 1; i >= 0; i--) for (int i = data.Length - 1; i >= 0; i--)
{ {
if (data[i].StartsWith("---")) // --- is the start of a new yaml document. if (data[i].StartsWith("---")) // --- is the start of a new yaml document.
{ {
int idStartIndex = data[i].IndexOf(" &") + 2; // & is the start of the object id. Match match = Regex.Match(data[i], pattern);
int idLength = data[i].Length;
File.WriteAllLines($"{dataPath}/{sceneName}-{data[i][idStartIndex..idLength]}.yaml", data[i..lastIndex]); if (match.Success)
{
// Extract the file number
string id = match.Groups[1].Value;
// Write data to disk.
File.WriteAllLines($"{dataPath}/{SceneName}-{id}.yaml", data[i..lastIndex]);
}
lastIndex = i; lastIndex = i;
} }
} }
File.WriteAllLines($"{dataPath}/{sceneName}.yaml", data[0..lastIndex]); // Write header to disk.
File.WriteAllLines($"{dataPath}/{SceneName}.yaml", data[0..lastIndex]);
LoadAll(); // Reload.
} }
/// <summary> /// <summary>
@ -144,23 +159,21 @@ public void Unload()
EditorSceneManager.SaveScene(scene); EditorSceneManager.SaveScene(scene);
scenePartitionData.scenePartitions.Clear(); scenePartitionData.LoadedScenePartitions.Clear();
AssetDatabase.Refresh(); AssetDatabase.Refresh();
} }
private void DeleteLoadedPartitions() private void DeleteLoadedPartitions()
{ {
if (!scenePartitionData.hasCreatedPartitions) return; if (!scenePartitionData.HasCreatedPartitions) return;
foreach (KeyValuePair<uint, ScenePartition> scenePartition in scenePartitionData.scenePartitions) foreach (KeyValuePair<uint, ScenePartition> scenePartition in scenePartitionData.LoadedScenePartitions)
{ {
if (!File.Exists(scenePartition.Value.filePath)) continue; if (!File.Exists(scenePartition.Value.filePath)) continue;
File.Delete(scenePartition.Value.filePath); File.Delete(scenePartition.Value.filePath);
} }
scenePartitionData.loadedScenePartitions.Clear();
} }
public void LoadPartitions(uint[] ids) public void LoadPartitions(uint[] ids)
@ -169,7 +182,7 @@ public void LoadPartitions(uint[] ids)
for (int i = 0; i < ids.Length; i++) for (int i = 0; i < ids.Length; i++)
{ {
SortedSet<uint> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(scenePartitionData.scenePartitions, ids[i]); SortedSet<uint> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(scenePartitionData.ScenePartitions, ids[i]);
foreach (uint c in connections) foreach (uint c in connections)
{ {

View File

@ -20,7 +20,7 @@ public override void OnInspectorGUI()
EditorGUILayout.Space(); EditorGUILayout.Space();
if (scenePartitionSO.sceneAsset == null) if (scenePartitionSO.SceneAsset == null)
{ {
if (GUILayout.Button("Create Scene")) if (GUILayout.Button("Create Scene"))
{ {
@ -34,12 +34,9 @@ public override void OnInspectorGUI()
scenePartitionSO.LoadAll(); scenePartitionSO.LoadAll();
} }
if (scenePartitionSO.scenePartitionData.hasCreatedPartitions) if (GUILayout.Button("Save"))
{ {
if (GUILayout.Button("Save")) scenePartitionSO.Save();
{
scenePartitionSO.Save();
}
} }
if (GUILayout.Button("Unload")) if (GUILayout.Button("Unload"))

View File

@ -21,7 +21,7 @@ public static string GetDataPath(ScenePartitionSO scenePartitionSO)
public static string GetScenePath(ScenePartitionSO scenePartitionSO) public static string GetScenePath(ScenePartitionSO scenePartitionSO)
{ {
string scenePath = AssetDatabase.GetAssetOrScenePath(scenePartitionSO.sceneAsset); string scenePath = AssetDatabase.GetAssetOrScenePath(scenePartitionSO.SceneAsset);
return scenePath; return scenePath;
} }

37
Runtime/SceneGrid.cs Normal file
View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace VertexColor.ScenePartition.Editor
{
[Serializable]
public class SceneGrid
{
[SerializeField]
private int cellSize = 10;
[SerializeField]
private SceneGridDictionary grid = new SceneGridDictionary();
public void Insert(uint id, Vector2 point)
{
Vector2 gridPos = CalculateGridPosition(point);
if (grid.TryGetValue(gridPos, out List<uint> ids))
{
ids.Add(id);
}
else
{
grid.Add(gridPos, new List<uint> { id });
}
}
public Vector2 CalculateGridPosition(Vector2 point)
{
int x = Mathf.FloorToInt(point.x / cellSize);
int y = Mathf.FloorToInt(point.y / cellSize);
return new Vector2(x, y);
}
}
}

11
Runtime/SceneGrid.cs.meta Normal file
View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f10fd4b244f804943a8ade09f4f46771
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,7 +2,7 @@
using System.IO; using System.IO;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace VertexColor.ScenePartition.Editor namespace VertexColor.ScenePartition
{ {
public class SceneIdComparer : IComparer<ScenePartition> public class SceneIdComparer : IComparer<ScenePartition>
{ {
@ -26,14 +26,14 @@ public class ScenePartition
{ {
public uint id = 0; public uint id = 0;
public string filePath = null; public string filePath = null;
public string[] data = null; //public string[] data = null;
public SerializableSortedSet<uint> references = new SerializableSortedSet<uint>(); public UintSortedSet references = new UintSortedSet();
public ScenePartition(string filePath) public ScenePartition(string filePath)
{ {
this.filePath = filePath; this.filePath = filePath;
data = File.ReadAllLines(filePath); string[] data = File.ReadAllLines(filePath);
if (data == null || data.Length == 0) return; if (data == null || data.Length == 0) return;

View File

@ -1,26 +1,26 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
namespace VertexColor.ScenePartition namespace VertexColor.ScenePartition
{ {
[Serializable] [System.Serializable]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
{ {
[SerializeField] [SerializeField]
private List<TKey> keys = new List<TKey>(); private List<TKey> dictionaryKeys = new List<TKey>();
[SerializeField] [SerializeField]
private List<TValue> values = new List<TValue>(); private List<TValue> dictionaryValues = new List<TValue>();
public void OnBeforeSerialize() public void OnBeforeSerialize()
{ {
keys.Clear(); dictionaryKeys.Clear();
values.Clear(); dictionaryValues.Clear();
foreach (KeyValuePair<TKey, TValue> pair in this) foreach (KeyValuePair<TKey, TValue> item in this)
{ {
keys.Add(pair.Key); dictionaryKeys.Add(item.Key);
values.Add(pair.Value); dictionaryValues.Add(item.Value);
} }
} }
@ -28,35 +28,33 @@ public void OnAfterDeserialize()
{ {
Clear(); Clear();
if (keys.Count != values.Count) if (dictionaryKeys.Count != dictionaryValues.Count) return;
{
throw new Exception("Error: Key and value count does not match in the dictionary.");
}
for (int i = 0; i < keys.Count; i++) for (int i = 0; i < dictionaryKeys.Count; i++)
{ {
Add(keys[i], values[i]); Add(dictionaryKeys[i], dictionaryValues[i]);
} }
} }
} }
[Serializable] [System.Serializable]
public class SerializableSortedList<TKey, TValue> : SortedList<TKey, TValue>, ISerializationCallbackReceiver public class SerializableSortedList<TKey, TValue> : SortedList<TKey, TValue>, ISerializationCallbackReceiver
{ {
[SerializeField] [SerializeField]
private List<TKey> keys = new List<TKey>(); private List<TKey> sortedListKeys = new List<TKey>();
[SerializeField] [SerializeField]
private List<TValue> values = new List<TValue>(); private List<TValue> sortedListValues = new List<TValue>();
public void OnBeforeSerialize() public void OnBeforeSerialize()
{ {
keys.Clear(); sortedListKeys.Clear();
values.Clear(); sortedListValues.Clear();
foreach (KeyValuePair<TKey, TValue> pair in this) foreach (KeyValuePair<TKey, TValue> item in this)
{ {
keys.Add(pair.Key); sortedListKeys.Add(item.Key);
values.Add(pair.Value); sortedListValues.Add(item.Value);
} }
} }
@ -64,37 +62,34 @@ public void OnAfterDeserialize()
{ {
Clear(); Clear();
if (keys.Count != values.Count) if (sortedListKeys.Count != sortedListValues.Count) return;
{
throw new Exception("Error: Key and value count does not match in the dictionary.");
}
for (int i = 0; i < keys.Count; i++) for (int i = 0; i < sortedListKeys.Count; i++)
{ {
Add(keys[i], values[i]); Add(sortedListKeys[i], sortedListValues[i]);
} }
} }
} }
[Serializable] [System.Serializable]
public class SerializableSortedSet<T> : SortedSet<T>, ISerializationCallbackReceiver public class SerializableSortedSet<T> : SortedSet<T>, ISerializationCallbackReceiver
{ {
[SerializeField] [SerializeField]
private List<T> elements = new List<T>(); private List<T> sortedSetValues = new List<T>();
public void OnBeforeSerialize() public void OnBeforeSerialize()
{ {
elements.Clear(); sortedSetValues.Clear();
elements.AddRange(this); sortedSetValues.AddRange(this);
} }
public void OnAfterDeserialize() public void OnAfterDeserialize()
{ {
Clear(); Clear();
for (int i = 0; i < elements.Count; i++) for (int i = 0; i < sortedSetValues.Count; i++)
{ {
Add(elements[i]); Add(sortedSetValues[i]);
} }
} }
} }

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
using UnityEngine;
namespace VertexColor.ScenePartition
{
[System.Serializable]
public class ScenePartitionSortedList : SerializableSortedList<uint, ScenePartition> { }
[System.Serializable]
public class UintSortedSet : SerializableSortedSet<uint> { }
[System.Serializable]
public class SceneGridDictionary : SerializableDictionary<Vector2, List<uint>> { }
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b87f21b6a1ebd2749b49c3a1812498e0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: