generated from max/template-unity-project
ScenePartitionObject
- Loading - Partial loading - Saving
This commit is contained in:
parent
f2e785f3fb
commit
b067522a95
70
Editor/ScenePartition.cs
Normal file
70
Editor/ScenePartition.cs
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace VertexColor.ScenePartition.Editor
|
||||||
|
{
|
||||||
|
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 HashSet<uint> references = new HashSet<uint>();
|
||||||
|
|
||||||
|
public ScenePartition(string filePath)
|
||||||
|
{
|
||||||
|
this.filePath = filePath;
|
||||||
|
|
||||||
|
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
Editor/ScenePartition.cs.meta
Normal file
11
Editor/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,6 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEditor.SceneManagement;
|
using UnityEditor.SceneManagement;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@ -13,11 +12,13 @@ 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 { get; private set; } = "";
|
public string sceneName => sceneAsset == null ? name : sceneAsset.name;
|
||||||
|
|
||||||
public bool hasLoadedPartitions => (loadedPartitions != null && loadedPartitions.Count > 0);
|
public bool hasCreatedPartitions => scenePartitions != null && scenePartitions.Count > 0;
|
||||||
[SerializeField]
|
public SortedList<uint, ScenePartition> scenePartitions = null;
|
||||||
private List<string> loadedPartitions = null; // Should not be serialized, this would be local data only.
|
|
||||||
|
public bool hasLoadedPartitions => loadedScenePartitions != null && loadedScenePartitions.Count > 0;
|
||||||
|
public SortedList<uint, ScenePartition> loadedScenePartitions = null;
|
||||||
|
|
||||||
public void CreateScene()
|
public void CreateScene()
|
||||||
{
|
{
|
||||||
@ -29,34 +30,72 @@ public void CreateScene()
|
|||||||
EditorSceneManager.SaveScene(scene, scenePath);
|
EditorSceneManager.SaveScene(scene, scenePath);
|
||||||
|
|
||||||
AssetDatabase.Refresh();
|
AssetDatabase.Refresh();
|
||||||
|
|
||||||
|
sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(scenePath);
|
||||||
|
|
||||||
|
AssetDatabase.SaveAssets();
|
||||||
|
AssetDatabase.Refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Load partitions from disk and construct the scene file.
|
||||||
|
/// </summary>
|
||||||
public void LoadAll()
|
public void LoadAll()
|
||||||
|
{
|
||||||
|
CreateScenePartitions();
|
||||||
|
SortedSet<uint> ids = new SortedSet<uint>(scenePartitions.Keys);
|
||||||
|
LoadScenePartitions(ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CreateScenePartitions()
|
||||||
{
|
{
|
||||||
string dataPath = ScenePartitionUtils.GetDataPath(this);
|
string dataPath = ScenePartitionUtils.GetDataPath(this);
|
||||||
|
string[] files = Directory.GetFiles(dataPath);
|
||||||
|
|
||||||
|
scenePartitions = new SortedList<uint, ScenePartition>();
|
||||||
|
|
||||||
|
for (int i = 0; i < files.Length; i++)
|
||||||
|
{
|
||||||
|
ScenePartition scenePartition = new ScenePartition(files[i]);
|
||||||
|
scenePartitions.Add(scenePartition.id, scenePartition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void LoadScenePartitions(SortedSet<uint> partitionIds)
|
||||||
|
{
|
||||||
|
if (!hasCreatedPartitions) return;
|
||||||
|
|
||||||
string scenePath = ScenePartitionUtils.GetScenePath(this);
|
string scenePath = ScenePartitionUtils.GetScenePath(this);
|
||||||
|
|
||||||
List<string> data = new List<string>();
|
List<string> data = new List<string>();
|
||||||
List<string> files = Directory.GetFiles(dataPath).ToList();
|
|
||||||
files.Sort();
|
|
||||||
|
|
||||||
for (int i = 0; i < files.Count; i++)
|
loadedScenePartitions = new SortedList<uint, ScenePartition>();
|
||||||
|
|
||||||
|
// Add the header.
|
||||||
|
partitionIds.Add(0);
|
||||||
|
|
||||||
|
// Create scene data.
|
||||||
|
foreach (uint id in partitionIds)
|
||||||
{
|
{
|
||||||
data.AddRange(File.ReadAllLines(files[i]));
|
ScenePartition p = scenePartitions[id];
|
||||||
|
data.AddRange(p.data);
|
||||||
|
loadedScenePartitions.Add(p.id, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create scene.
|
||||||
File.WriteAllLines(scenePath, data);
|
File.WriteAllLines(scenePath, data);
|
||||||
|
|
||||||
loadedPartitions.AddRange(files);
|
|
||||||
|
|
||||||
AssetDatabase.Refresh();
|
AssetDatabase.Refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert scene to partitions and save them to disk.
|
||||||
|
/// </summary>
|
||||||
public void SaveAll()
|
public void SaveAll()
|
||||||
{
|
{
|
||||||
if (!hasLoadedPartitions) return;
|
if (!hasCreatedPartitions) return;
|
||||||
|
|
||||||
DeleteLoadedPartitions();
|
DeleteLoadedPartitions(); // Delete the loaded partitions from disk so we can write the new ones.
|
||||||
|
|
||||||
string dataPath = ScenePartitionUtils.GetDataPath(this);
|
string dataPath = ScenePartitionUtils.GetDataPath(this);
|
||||||
string scenePath = ScenePartitionUtils.GetScenePath(this);
|
string scenePath = ScenePartitionUtils.GetScenePath(this);
|
||||||
@ -78,8 +117,13 @@ public void SaveAll()
|
|||||||
}
|
}
|
||||||
|
|
||||||
File.WriteAllLines($"{dataPath}/{sceneName}.yaml", data[0..lastIndex]);
|
File.WriteAllLines($"{dataPath}/{sceneName}.yaml", data[0..lastIndex]);
|
||||||
|
|
||||||
|
LoadAll(); // Reload.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Empty the scene and save it (so it has no changes in source control).
|
||||||
|
/// </summary>
|
||||||
public void Unload()
|
public void Unload()
|
||||||
{
|
{
|
||||||
string dataPath = ScenePartitionUtils.GetDataPath(this);
|
string dataPath = ScenePartitionUtils.GetDataPath(this);
|
||||||
@ -91,36 +135,45 @@ public void Unload()
|
|||||||
|
|
||||||
for (int i = gameObjects.Length - 1; i >= 0; i--)
|
for (int i = gameObjects.Length - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
Object.DestroyImmediate(gameObjects[i]);
|
DestroyImmediate(gameObjects[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
EditorSceneManager.SaveScene(scene);
|
EditorSceneManager.SaveScene(scene);
|
||||||
|
|
||||||
loadedPartitions.Clear();
|
scenePartitions.Clear();
|
||||||
|
|
||||||
AssetDatabase.Refresh();
|
AssetDatabase.Refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DeleteLoadedPartitions()
|
private void DeleteLoadedPartitions()
|
||||||
{
|
{
|
||||||
if (loadedPartitions == null) return;
|
if (!hasCreatedPartitions) return;
|
||||||
|
|
||||||
for (int i = loadedPartitions.Count - 1; i >= 0; i--)
|
foreach (KeyValuePair<uint, ScenePartition> scenePartition in scenePartitions)
|
||||||
{
|
{
|
||||||
if (!File.Exists(loadedPartitions[i])) continue;
|
if (!File.Exists(scenePartition.Value.filePath)) continue;
|
||||||
|
|
||||||
File.Delete(loadedPartitions[i]);
|
File.Delete(scenePartition.Value.filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
loadedPartitions.Clear();
|
scenePartitions.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnValidate()
|
public void LoadPartitions(uint[] ids)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(sceneName) && sceneAsset != null)
|
SortedSet<uint> partitionIds = new SortedSet<uint>();
|
||||||
|
|
||||||
|
for (int i = 0; i < ids.Length; i++)
|
||||||
{
|
{
|
||||||
sceneName = sceneAsset.name;
|
SortedSet<uint> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(scenePartitions, ids[i]);
|
||||||
}
|
|
||||||
|
foreach (uint c in connections)
|
||||||
|
{
|
||||||
|
partitionIds.Add(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LoadScenePartitions(partitionIds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,15 +1,19 @@
|
|||||||
using UnityEngine;
|
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
namespace VertexColor.ScenePartition.Editor
|
namespace VertexColor.ScenePartition.Editor
|
||||||
{
|
{
|
||||||
[CustomEditor(typeof(ScenePartitionSO))]
|
[CustomEditor(typeof(ScenePartitionSO))]
|
||||||
public class ScenePartitionSOEditor : UnityEditor.Editor
|
public class ScenePartitionSOEditor : UnityEditor.Editor
|
||||||
{
|
{
|
||||||
|
private int id = 0;
|
||||||
|
|
||||||
public override void OnInspectorGUI()
|
public override void OnInspectorGUI()
|
||||||
{
|
{
|
||||||
ScenePartitionSO scenePartitionSO = (target as ScenePartitionSO);
|
ScenePartitionSO scenePartitionSO = (target as ScenePartitionSO);
|
||||||
|
|
||||||
|
DrawDefaultInspector();
|
||||||
|
|
||||||
serializedObject.Update();
|
serializedObject.Update();
|
||||||
//EditorGUILayout.PropertyField(sceneAssetProperty);
|
//EditorGUILayout.PropertyField(sceneAssetProperty);
|
||||||
serializedObject.ApplyModifiedProperties();
|
serializedObject.ApplyModifiedProperties();
|
||||||
@ -30,7 +34,7 @@ public override void OnInspectorGUI()
|
|||||||
scenePartitionSO.LoadAll();
|
scenePartitionSO.LoadAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scenePartitionSO.hasLoadedPartitions)
|
if (scenePartitionSO.hasCreatedPartitions)
|
||||||
{
|
{
|
||||||
if (GUILayout.Button("Save All"))
|
if (GUILayout.Button("Save All"))
|
||||||
{
|
{
|
||||||
@ -43,17 +47,60 @@ public override void OnInspectorGUI()
|
|||||||
scenePartitionSO.Unload();
|
scenePartitionSO.Unload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EditorGUILayout.Space();
|
||||||
|
|
||||||
if (GUILayout.Button("Open Scene Data Folder"))
|
if (GUILayout.Button("Open Scene Data Folder"))
|
||||||
{
|
{
|
||||||
EditorUtility.RevealInFinder(ScenePartitionUtils.GetDataPath(scenePartitionSO));
|
EditorUtility.RevealInFinder(ScenePartitionUtils.GetDataPath(scenePartitionSO));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EditorGUILayout.Space();
|
||||||
|
|
||||||
|
id = EditorGUILayout.IntField("id", id);
|
||||||
|
|
||||||
|
if (GUILayout.Button("Test Load"))
|
||||||
|
{
|
||||||
|
scenePartitionSO.LoadPartitions(new uint[1] { (uint)id });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
EditorGUILayout.Space();
|
EditorGUILayout.Space();
|
||||||
|
|
||||||
using (new EditorGUI.DisabledGroupScope(true))
|
using (new EditorGUI.DisabledGroupScope(true))
|
||||||
{
|
{
|
||||||
DrawDefaultInspector();
|
if (scenePartitionSO.hasCreatedPartitions)
|
||||||
|
{
|
||||||
|
EditorGUILayout.LabelField($"scenePartitions");
|
||||||
|
|
||||||
|
foreach (System.Collections.Generic.KeyValuePair<uint, ScenePartition> scenePartition in scenePartitionSO.scenePartitions)
|
||||||
|
{
|
||||||
|
EditorGUILayout.IntField((int)scenePartition.Value.id);
|
||||||
|
EditorGUI.indentLevel++;
|
||||||
|
foreach (var reference in scenePartition.Value.references)
|
||||||
|
{
|
||||||
|
EditorGUILayout.IntField((int)reference);
|
||||||
|
}
|
||||||
|
EditorGUI.indentLevel--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EditorGUILayout.Space();
|
||||||
|
|
||||||
|
if (scenePartitionSO.hasLoadedPartitions)
|
||||||
|
{
|
||||||
|
EditorGUILayout.LabelField($"loadedScenePartitions");
|
||||||
|
|
||||||
|
foreach (System.Collections.Generic.KeyValuePair<uint, ScenePartition> scenePartition in scenePartitionSO.loadedScenePartitions)
|
||||||
|
{
|
||||||
|
EditorGUILayout.IntField((int)scenePartition.Value.id);
|
||||||
|
EditorGUI.indentLevel++;
|
||||||
|
foreach (var reference in scenePartition.Value.references)
|
||||||
|
{
|
||||||
|
EditorGUILayout.IntField((int)reference);
|
||||||
|
}
|
||||||
|
EditorGUI.indentLevel--;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@ -24,5 +25,32 @@ public static string GetScenePath(ScenePartitionSO scenePartitionSO)
|
|||||||
|
|
||||||
return scenePath;
|
return scenePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static SortedSet<uint> FindDeeplyLinkedObjects(SortedList<uint, ScenePartition> scenePartitions, uint partitionId)
|
||||||
|
{
|
||||||
|
SortedSet<uint> linkedObjects = new SortedSet<uint>();
|
||||||
|
FindDeeplyLinkedObjectsRecursive(scenePartitions, partitionId, linkedObjects);
|
||||||
|
return linkedObjects;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void FindDeeplyLinkedObjectsRecursive(SortedList<uint, ScenePartition> scenePartitions, uint partitionId, SortedSet<uint> linkedObjects)
|
||||||
|
{
|
||||||
|
if (linkedObjects.Contains(partitionId)) return;
|
||||||
|
if (!scenePartitions.TryGetValue(partitionId, out ScenePartition partition)) return;
|
||||||
|
|
||||||
|
linkedObjects.Add(partitionId);
|
||||||
|
|
||||||
|
foreach (uint reference in partition.references)
|
||||||
|
{
|
||||||
|
FindDeeplyLinkedObjectsRecursive(scenePartitions, reference, linkedObjects);
|
||||||
|
if (scenePartitions.TryGetValue(reference, out ScenePartition referencedPartition))
|
||||||
|
{
|
||||||
|
foreach (uint subReference in referencedPartition.references)
|
||||||
|
{
|
||||||
|
FindDeeplyLinkedObjectsRecursive(scenePartitions, subReference, linkedObjects);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user