Saving & unloading

This commit is contained in:
max 2023-06-24 16:43:10 +02:00
parent bdbfaf34c0
commit f2e785f3fb
3 changed files with 141 additions and 93 deletions

View File

@ -1,8 +1,12 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor; using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement;
namespace VertexColor.ScenePartition namespace VertexColor.ScenePartition.Editor
{ {
[CreateAssetMenu(fileName = "Scene", menuName = "Max/ScenePartitionSO")] [CreateAssetMenu(fileName = "Scene", menuName = "Max/ScenePartitionSO")]
public class ScenePartitionSO : ScriptableObject public class ScenePartitionSO : ScriptableObject
@ -11,7 +15,105 @@ public class ScenePartitionSO : ScriptableObject
public SceneAsset sceneAsset { get; private set; } = null; public SceneAsset sceneAsset { get; private set; } = null;
public string sceneName { get; private set; } = ""; public string sceneName { get; private set; } = "";
private List<string> loadedPartitions = new List<string>(); public bool hasLoadedPartitions => (loadedPartitions != null && loadedPartitions.Count > 0);
[SerializeField]
private List<string> loadedPartitions = null; // Should not be serialized, this would be local data only.
public void CreateScene()
{
if (sceneAsset != null) return;
string scenePath = Path.Combine(AssetDatabase.GetAssetPath(this), $"../{this.name}.unity");
Scene scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
EditorSceneManager.SaveScene(scene, scenePath);
AssetDatabase.Refresh();
}
public void LoadAll()
{
string dataPath = ScenePartitionUtils.GetDataPath(this);
string scenePath = ScenePartitionUtils.GetScenePath(this);
List<string> data = new List<string>();
List<string> files = Directory.GetFiles(dataPath).ToList();
files.Sort();
for (int i = 0; i < files.Count; i++)
{
data.AddRange(File.ReadAllLines(files[i]));
}
File.WriteAllLines(scenePath, data);
loadedPartitions.AddRange(files);
AssetDatabase.Refresh();
}
public void SaveAll()
{
if (!hasLoadedPartitions) return;
DeleteLoadedPartitions();
string dataPath = ScenePartitionUtils.GetDataPath(this);
string scenePath = ScenePartitionUtils.GetScenePath(this);
string[] data = File.ReadAllLines(scenePath);
int lastIndex = data.Length;
for (int i = data.Length - 1; i >= 0; i--)
{
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.
int idLength = data[i].Length;
File.WriteAllLines($"{dataPath}/{sceneName}-{data[i][idStartIndex..idLength]}.yaml", data[i..lastIndex]);
lastIndex = i;
}
}
File.WriteAllLines($"{dataPath}/{sceneName}.yaml", data[0..lastIndex]);
}
public void Unload()
{
string dataPath = ScenePartitionUtils.GetDataPath(this);
string scenePath = ScenePartitionUtils.GetScenePath(this);
Scene scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Single);
GameObject[] gameObjects = scene.GetRootGameObjects();
for (int i = gameObjects.Length - 1; i >= 0; i--)
{
Object.DestroyImmediate(gameObjects[i]);
}
EditorSceneManager.SaveScene(scene);
loadedPartitions.Clear();
AssetDatabase.Refresh();
}
private void DeleteLoadedPartitions()
{
if (loadedPartitions == null) return;
for (int i = loadedPartitions.Count - 1; i >= 0; i--)
{
if (!File.Exists(loadedPartitions[i])) continue;
File.Delete(loadedPartitions[i]);
}
loadedPartitions.Clear();
}
private void OnValidate() private void OnValidate()
{ {

View File

@ -1,48 +1,60 @@
using UnityEngine; using UnityEngine;
using UnityEditor; using UnityEditor;
using System.IO;
namespace VertexColor.ScenePartition.Editor namespace VertexColor.ScenePartition.Editor
{ {
[CustomEditor(typeof(ScenePartitionSO))] [CustomEditor(typeof(ScenePartitionSO))]
public class ScenePartitionSOEditor : UnityEditor.Editor public class ScenePartitionSOEditor : UnityEditor.Editor
{ {
//SerializedProperty property;
void OnEnable()
{
//property = serializedObject.FindProperty("myProperty");
}
public override void OnInspectorGUI() public override void OnInspectorGUI()
{ {
DrawDefaultInspector();
ScenePartitionSO scenePartitionSO = (target as ScenePartitionSO); ScenePartitionSO scenePartitionSO = (target as ScenePartitionSO);
serializedObject.Update(); serializedObject.Update();
//EditorGUILayout.PropertyField(property); //EditorGUILayout.PropertyField(sceneAssetProperty);
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
if (GUILayout.Button("Load")) EditorGUILayout.Space();
if(scenePartitionSO.sceneAsset == null)
{ {
ScenePartitionUtils.Load(scenePartitionSO); if (GUILayout.Button("Create Scene"))
{
scenePartitionSO.CreateScene();
}
}
else
{
if (GUILayout.Button("Load All"))
{
scenePartitionSO.LoadAll();
} }
if (GUILayout.Button("Save")) if (scenePartitionSO.hasLoadedPartitions)
{ {
ScenePartitionUtils.Save(scenePartitionSO); if (GUILayout.Button("Save All"))
{
scenePartitionSO.SaveAll();
}
} }
if (GUILayout.Button("Unload")) if (GUILayout.Button("Unload"))
{ {
ScenePartitionUtils.Unload(scenePartitionSO); scenePartitionSO.Unload();
} }
if(GUILayout.Button("Open Scene Data")) if (GUILayout.Button("Open Scene Data Folder"))
{ {
EditorUtility.RevealInFinder(ScenePartitionUtils.GetDataPath(scenePartitionSO)); EditorUtility.RevealInFinder(ScenePartitionUtils.GetDataPath(scenePartitionSO));
} }
} }
EditorGUILayout.Space();
using (new EditorGUI.DisabledGroupScope(true))
{
DrawDefaultInspector();
}
}
} }
} }

View File

@ -1,10 +1,6 @@
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using UnityEditor; using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement;
namespace VertexColor.ScenePartition.Editor namespace VertexColor.ScenePartition.Editor
{ {
@ -28,67 +24,5 @@ public static string GetScenePath(ScenePartitionSO scenePartitionSO)
return scenePath; return scenePath;
} }
public static void Load(ScenePartitionSO scenePartitionSO)
{
string dataPath = GetDataPath(scenePartitionSO);
string scenePath = GetScenePath(scenePartitionSO);
List<string> data = new List<string>();
List<string> files = Directory.GetFiles(dataPath).ToList();
files.Sort();
for (int i = 0; i < files.Count; i++)
{
data.AddRange(File.ReadAllLines(files[i]));
}
File.WriteAllLines(scenePath, data);
AssetDatabase.Refresh();
}
public static void Save(ScenePartitionSO scenePartitionSO)
{
string dataPath = GetDataPath(scenePartitionSO);
string scenePath = GetScenePath(scenePartitionSO);
string[] data = File.ReadAllLines(scenePath);
int lastIndex = data.Length;
for (int i = data.Length - 1; i >= 0; i--)
{
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.
int idLength = data[i].Length;
File.WriteAllLines($"{dataPath}/{scenePartitionSO.sceneName}-{data[i][idStartIndex..idLength]}.yaml", data[i..lastIndex]);
lastIndex = i;
}
}
File.WriteAllLines($"{dataPath}/{scenePartitionSO.sceneName}.yaml", data[0..lastIndex]);
}
public static void Unload(ScenePartitionSO scenePartitionSO)
{
string dataPath = GetDataPath(scenePartitionSO);
string scenePath = GetScenePath(scenePartitionSO);
Scene scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Single);
GameObject[] gameObjects = scene.GetRootGameObjects();
for (int i = gameObjects.Length - 1; i >= 0; i--)
{
Object.DestroyImmediate(gameObjects[i]);
}
EditorSceneManager.SaveScene(scene);
AssetDatabase.Refresh();
}
} }
} }