ScriptableSingleton for data

This commit is contained in:
max 2023-06-27 00:24:46 +02:00
parent 3118e18c20
commit 7e8d46623a
8 changed files with 130 additions and 77 deletions

View File

@ -1,9 +1,7 @@
using UnityEngine;
namespace VertexColor.ScenePartition.Editor
namespace VertexColor.ScenePartition.Editor
{
[CreateAssetMenu(fileName = "Scene", menuName = "Max/ScenePartitionDataSO")]
public class ScenePartitionDataSO : ScriptableObject
[System.Serializable]
public class ScenePartitionData
{
public bool HasCreatedPartitions => ScenePartitions != null && ScenePartitions.Count > 0;
public ScenePartitionSortedList ScenePartitions = new ScenePartitionSortedList();

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 53d6bea45132f6d4aa3aa1631099564e
guid: dfca041580276524fa12c31dc15d5a4d
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -1,53 +0,0 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace VertexColor.ScenePartition.Editor
{
[CustomEditor(typeof(ScenePartitionDataSO))]
public class ScenePartitionDataSOEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
ScenePartitionDataSO scenePartitionDataSO = (target as ScenePartitionDataSO);
DrawDefaultInspector();
serializedObject.Update();
//EditorGUILayout.PropertyField(sceneAssetProperty);
serializedObject.ApplyModifiedProperties();
EditorGUILayout.Space();
if (GUILayout.Button("Force Serialize"))
{
AssetDatabase.ForceReserializeAssets(new string[] { AssetDatabase.GetAssetPath(scenePartitionDataSO) }, ForceReserializeAssetsOptions.ReserializeAssetsAndMetadata);
}
using (new EditorGUI.DisabledGroupScope(true))
{
if (scenePartitionDataSO.HasCreatedPartitions)
{
EditorGUILayout.LabelField($"scenePartitions");
foreach (KeyValuePair<uint, ScenePartition> scenePartition in scenePartitionDataSO.ScenePartitions)
{
EditorGUILayout.IntField((int)scenePartition.Value.id);
}
}
EditorGUILayout.Space();
if (scenePartitionDataSO.HasLoadedPartitions)
{
EditorGUILayout.LabelField($"loadedScenePartitions");
foreach (KeyValuePair<uint, ScenePartition> scenePartition in scenePartitionDataSO.LoadedScenePartitions)
{
EditorGUILayout.IntField((int)scenePartition.Value.id);
}
}
}
}
}
}

View File

@ -0,0 +1,63 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace VertexColor.ScenePartition.Editor
{
public class ScenePartitionEditorWindow : EditorWindow
{
private Vector2 scrollPos = Vector2.zero;
[MenuItem("Max/ScenePartitionWindow")]
public static void ShowExample()
{
ScenePartitionEditorWindow window = GetWindow<ScenePartitionEditorWindow>();
window.titleContent = new GUIContent($"{nameof(ScenePartitionEditorWindow)}");
}
private void OnGUI()
{
DrawSceneDataCache();
}
private void DrawSceneDataCache()
{
using (EditorGUILayout.ScrollViewScope scope = new EditorGUILayout.ScrollViewScope(scrollPos))
{
scrollPos = scope.scrollPosition;
using (new EditorGUI.DisabledGroupScope(true))
{
ScenePartitionSS scenePartitionSS = ScenePartitionSS.instance;
foreach (KeyValuePair<ScenePartitionSO, ScenePartitionData> sceneData in scenePartitionSS.SceneDataCache)
{
EditorGUILayout.LabelField($"{sceneData.Key.name}");
if (sceneData.Value.HasCreatedPartitions)
{
EditorGUILayout.LabelField($"scenePartitions");
foreach (KeyValuePair<uint, ScenePartition> scenePartition in sceneData.Value.ScenePartitions)
{
EditorGUILayout.IntField((int)scenePartition.Value.id);
}
}
EditorGUILayout.Space();
if (sceneData.Value.HasLoadedPartitions)
{
EditorGUILayout.LabelField($"loadedScenePartitions");
foreach (KeyValuePair<uint, ScenePartition> scenePartition in sceneData.Value.LoadedScenePartitions)
{
EditorGUILayout.IntField((int)scenePartition.Value.id);
}
}
}
}
}
}
}
}

View File

@ -15,11 +15,18 @@ public class ScenePartitionSO : ScriptableObject
public SceneAsset SceneAsset { get; private set; } = null;
public string SceneName => SceneAsset == null ? name : SceneAsset.name;
[SerializeField]
private ScenePartitionDataSO scenePartitionData = null;
public List<uint> alwaysLoadIds = new List<uint> { 0, 1, 2, 3, 4 };
public ScenePartitionData ScenePartitionData
{
get
{
scenePartitionData ??= ScenePartitionSS.instance.GetScenePartitionData(this);
return scenePartitionData;
}
}
private ScenePartitionData scenePartitionData = null;
public void CreateScene()
{
if (SceneAsset != null) return;
@ -28,7 +35,9 @@ public void CreateScene()
Scene scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
EditorSceneManager.SaveScene(scene, scenePath);
Save();
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
SceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(scenePath);
@ -43,7 +52,7 @@ public void CreateScene()
public void LoadAll()
{
CreateScenePartitions();
SortedSet<uint> ids = new SortedSet<uint>(scenePartitionData.ScenePartitions.Keys);
SortedSet<uint> ids = new SortedSet<uint>(ScenePartitionData.ScenePartitions.Keys);
LoadScenePartitions(ids);
}
@ -52,29 +61,29 @@ private void CreateScenePartitions()
string dataPath = ScenePartitionUtils.GetDataPath(this);
string[] files = Directory.GetFiles(dataPath);
scenePartitionData.ScenePartitions = new ScenePartitionSortedList();
ScenePartitionData.ScenePartitions = new ScenePartitionSortedList();
for (int i = 0; i < files.Length; 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)
{
if (!scenePartitionData.HasCreatedPartitions) return;
if (!ScenePartitionData.HasCreatedPartitions) return;
string scenePath = ScenePartitionUtils.GetScenePath(this);
List<string> data = new List<string>();
scenePartitionData.LoadedScenePartitions.Clear();
ScenePartitionData.LoadedScenePartitions.Clear();
// Add default ids.
for (int i = 0; i < alwaysLoadIds.Count; i++)
{
if (scenePartitionData.ScenePartitions.ContainsKey(alwaysLoadIds[i]))
if (ScenePartitionData.ScenePartitions.ContainsKey(alwaysLoadIds[i]))
{
partitionIds.Add(alwaysLoadIds[i]);
}
@ -83,9 +92,9 @@ private void LoadScenePartitions(SortedSet<uint> partitionIds)
// Create scene data.
foreach (uint id in partitionIds)
{
ScenePartition p = scenePartitionData.ScenePartitions[id];
ScenePartition p = ScenePartitionData.ScenePartitions[id];
data.AddRange(File.ReadAllLines(p.filePath));
scenePartitionData.LoadedScenePartitions.Add(p.id, p);
ScenePartitionData.LoadedScenePartitions.Add(p.id, p);
}
// Create scene.
@ -99,9 +108,6 @@ private void LoadScenePartitions(SortedSet<uint> partitionIds)
/// </summary>
public void Save()
{
if (!scenePartitionData.HasCreatedPartitions) return;
if (!scenePartitionData.HasLoadedPartitions) return;
DeleteLoadedPartitions(); // Delete the loaded partitions from disk so we can write the new ones.
string pattern = @"&(\d+)";
@ -155,16 +161,16 @@ public void Unload()
EditorSceneManager.SaveScene(scene);
scenePartitionData.LoadedScenePartitions.Clear();
ScenePartitionData.LoadedScenePartitions.Clear();
AssetDatabase.Refresh();
}
private void DeleteLoadedPartitions()
{
if (!scenePartitionData.HasCreatedPartitions) return;
if (!ScenePartitionData.HasLoadedPartitions) return;
foreach (KeyValuePair<uint, ScenePartition> scenePartition in scenePartitionData.LoadedScenePartitions)
foreach (KeyValuePair<uint, ScenePartition> scenePartition in ScenePartitionData.LoadedScenePartitions)
{
if (!File.Exists(scenePartition.Value.filePath)) continue;
@ -178,7 +184,7 @@ public void LoadPartitions(uint[] ids)
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)
{

View File

@ -0,0 +1,28 @@
using UnityEditor;
using UnityEngine;
namespace VertexColor.ScenePartition.Editor
{
[FilePath("ScenePartition/SceneDataCache.asset", FilePathAttribute.Location.ProjectFolder)]
public class ScenePartitionSS : ScriptableSingleton<ScenePartitionSS>
{
public SceneDataCache SceneDataCache => sceneDataCache;
[SerializeField]
private SceneDataCache sceneDataCache = new SceneDataCache();
public ScenePartitionData GetScenePartitionData(ScenePartitionSO scenePartitionSO)
{
if (!sceneDataCache.TryGetValue(scenePartitionSO, out ScenePartitionData scenePartitionData))
{
scenePartitionData = new ScenePartitionData();
sceneDataCache.Add(scenePartitionSO, scenePartitionData);
}
return scenePartitionData;
}
}
[System.Serializable]
public class SceneDataCache : SerializableDictionary<ScenePartitionSO, ScenePartitionData> { }
}

View File

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