generated from max/template-unity-project
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:
37
Runtime/SceneGrid.cs
Normal file
37
Runtime/SceneGrid.cs
Normal 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
11
Runtime/SceneGrid.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f10fd4b244f804943a8ade09f4f46771
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
70
Runtime/ScenePartition.cs
Normal file
70
Runtime/ScenePartition.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace VertexColor.ScenePartition
|
||||
{
|
||||
public class SceneIdComparer : IComparer<ScenePartition>
|
||||
{
|
||||
public int Compare(ScenePartition x, ScenePartition y)
|
||||
{
|
||||
if (x.id == y.id)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (x.id < y.id)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class ScenePartition
|
||||
{
|
||||
public uint id = 0;
|
||||
public string filePath = null;
|
||||
//public string[] data = null;
|
||||
public UintSortedSet references = new UintSortedSet();
|
||||
|
||||
public ScenePartition(string filePath)
|
||||
{
|
||||
this.filePath = filePath;
|
||||
|
||||
string[] data = File.ReadAllLines(filePath);
|
||||
|
||||
if (data == null || data.Length == 0) return;
|
||||
|
||||
{ // Get id.
|
||||
string pattern = @"&(\d+)";
|
||||
|
||||
// Find all matches using regex
|
||||
Match match = Regex.Match(data[0], pattern);
|
||||
|
||||
if (!match.Success) return;
|
||||
if (!uint.TryParse(match.Groups[1].Value, out id)) return;
|
||||
}
|
||||
|
||||
{ // Get references.
|
||||
string pattern = @"fileID:\s(\d+)";
|
||||
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
// Find the match using regex
|
||||
Match match = Regex.Match(data[i], pattern);
|
||||
|
||||
if (!match.Success) continue;
|
||||
|
||||
if (uint.TryParse(match.Groups[1].Value, out uint fileNumber))
|
||||
{
|
||||
if (fileNumber == 0) continue; // 0 == nothing.
|
||||
|
||||
references.Add(fileNumber);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Runtime/ScenePartition.cs.meta
Normal file
11
Runtime/ScenePartition.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 002f02333c6d0b844bff6ab0266ad686
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -1,26 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace VertexColor.ScenePartition
|
||||
{
|
||||
[Serializable]
|
||||
[System.Serializable]
|
||||
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
|
||||
{
|
||||
[SerializeField]
|
||||
private List<TKey> keys = new List<TKey>();
|
||||
private List<TKey> dictionaryKeys = new List<TKey>();
|
||||
|
||||
[SerializeField]
|
||||
private List<TValue> values = new List<TValue>();
|
||||
private List<TValue> dictionaryValues = new List<TValue>();
|
||||
|
||||
public void OnBeforeSerialize()
|
||||
{
|
||||
keys.Clear();
|
||||
values.Clear();
|
||||
dictionaryKeys.Clear();
|
||||
dictionaryValues.Clear();
|
||||
|
||||
foreach (KeyValuePair<TKey, TValue> pair in this)
|
||||
foreach (KeyValuePair<TKey, TValue> item in this)
|
||||
{
|
||||
keys.Add(pair.Key);
|
||||
values.Add(pair.Value);
|
||||
dictionaryKeys.Add(item.Key);
|
||||
dictionaryValues.Add(item.Value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -28,35 +28,33 @@ namespace VertexColor.ScenePartition
|
||||
{
|
||||
Clear();
|
||||
|
||||
if (keys.Count != values.Count)
|
||||
{
|
||||
throw new Exception("Error: Key and value count does not match in the dictionary.");
|
||||
}
|
||||
if (dictionaryKeys.Count != dictionaryValues.Count) return;
|
||||
|
||||
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
|
||||
{
|
||||
[SerializeField]
|
||||
private List<TKey> keys = new List<TKey>();
|
||||
private List<TKey> sortedListKeys = new List<TKey>();
|
||||
|
||||
[SerializeField]
|
||||
private List<TValue> values = new List<TValue>();
|
||||
private List<TValue> sortedListValues = new List<TValue>();
|
||||
|
||||
public void OnBeforeSerialize()
|
||||
{
|
||||
keys.Clear();
|
||||
values.Clear();
|
||||
sortedListKeys.Clear();
|
||||
sortedListValues.Clear();
|
||||
|
||||
foreach (KeyValuePair<TKey, TValue> pair in this)
|
||||
foreach (KeyValuePair<TKey, TValue> item in this)
|
||||
{
|
||||
keys.Add(pair.Key);
|
||||
values.Add(pair.Value);
|
||||
sortedListKeys.Add(item.Key);
|
||||
sortedListValues.Add(item.Value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,37 +62,34 @@ namespace VertexColor.ScenePartition
|
||||
{
|
||||
Clear();
|
||||
|
||||
if (keys.Count != values.Count)
|
||||
{
|
||||
throw new Exception("Error: Key and value count does not match in the dictionary.");
|
||||
}
|
||||
if (sortedListKeys.Count != sortedListValues.Count) return;
|
||||
|
||||
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
|
||||
{
|
||||
[SerializeField]
|
||||
private List<T> elements = new List<T>();
|
||||
private List<T> sortedSetValues = new List<T>();
|
||||
|
||||
public void OnBeforeSerialize()
|
||||
{
|
||||
elements.Clear();
|
||||
elements.AddRange(this);
|
||||
sortedSetValues.Clear();
|
||||
sortedSetValues.AddRange(this);
|
||||
}
|
||||
|
||||
public void OnAfterDeserialize()
|
||||
{
|
||||
Clear();
|
||||
|
||||
for (int i = 0; i < elements.Count; i++)
|
||||
for (int i = 0; i < sortedSetValues.Count; i++)
|
||||
{
|
||||
Add(elements[i]);
|
||||
Add(sortedSetValues[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
14
Runtime/SerializedStructures.cs
Normal file
14
Runtime/SerializedStructures.cs
Normal 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>> { }
|
||||
}
|
11
Runtime/SerializedStructures.cs.meta
Normal file
11
Runtime/SerializedStructures.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b87f21b6a1ebd2749b49c3a1812498e0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user