ScenePartition/Editor/ScenePartitionSO.cs

192 lines
6.3 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2023-06-24 16:43:10 +02:00
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
2023-06-24 16:43:10 +02:00
using UnityEditor.SceneManagement;
using UnityEngine;
2023-06-24 16:43:10 +02:00
using UnityEngine.SceneManagement;
2023-06-24 16:43:10 +02:00
namespace VertexColor.ScenePartition.Editor
{
[CreateAssetMenu(fileName = "Scene", menuName = "Max/ScenePartitionSO")]
public class ScenePartitionSO : ScriptableObject
{
[field: SerializeField]
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 };
2023-06-24 16:43:10 +02:00
public void CreateScene()
{
if (SceneAsset != null) return;
2023-06-24 16:43:10 +02:00
string scenePath = Path.Combine(AssetDatabase.GetAssetPath(this), $"../{this.name}.unity");
Scene scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
EditorSceneManager.SaveScene(scene, scenePath);
AssetDatabase.Refresh();
SceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(scenePath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
2023-06-24 16:43:10 +02:00
}
/// <summary>
/// Load partitions from disk and construct the scene file.
/// </summary>
2023-06-24 16:43:10 +02:00
public void LoadAll()
{
CreateScenePartitions();
SortedSet<uint> ids = new SortedSet<uint>(scenePartitionData.ScenePartitions.Keys);
LoadScenePartitions(ids);
}
private void CreateScenePartitions()
2023-06-24 16:43:10 +02:00
{
string dataPath = ScenePartitionUtils.GetDataPath(this);
string[] files = Directory.GetFiles(dataPath);
scenePartitionData.ScenePartitions = new ScenePartitionSortedList();
for (int i = 0; i < files.Length; i++)
{
ScenePartition scenePartition = new ScenePartition(files[i]);
scenePartitionData.ScenePartitions.Add(scenePartition.id, scenePartition);
}
}
private void LoadScenePartitions(SortedSet<uint> partitionIds)
{
if (!scenePartitionData.HasCreatedPartitions) return;
2023-06-24 16:43:10 +02:00
string scenePath = ScenePartitionUtils.GetScenePath(this);
List<string> data = new List<string>();
scenePartitionData.LoadedScenePartitions.Clear();
// Add default ids.
for (int i = 0; i < alwaysLoadIds.Count; i++)
{
if (scenePartitionData.ScenePartitions.ContainsKey(alwaysLoadIds[i]))
{
partitionIds.Add(alwaysLoadIds[i]);
}
}
// Create scene data.
foreach (uint id in partitionIds)
2023-06-24 16:43:10 +02:00
{
ScenePartition p = scenePartitionData.ScenePartitions[id];
data.AddRange(File.ReadAllLines(p.filePath));
scenePartitionData.LoadedScenePartitions.Add(p.id, p);
2023-06-24 16:43:10 +02:00
}
// Create scene.
2023-06-24 16:43:10 +02:00
File.WriteAllLines(scenePath, data);
AssetDatabase.Refresh();
}
/// <summary>
/// Convert scene to partitions and save them to disk.
/// </summary>
public void Save()
2023-06-24 16:43:10 +02:00
{
if (!scenePartitionData.HasCreatedPartitions) return;
if (!scenePartitionData.HasLoadedPartitions) return;
2023-06-24 16:43:10 +02:00
DeleteLoadedPartitions(); // Delete the loaded partitions from disk so we can write the new ones.
2023-06-24 16:43:10 +02:00
string pattern = @"&(\d+)";
2023-06-24 16:43:10 +02:00
string dataPath = ScenePartitionUtils.GetDataPath(this);
string scenePath = ScenePartitionUtils.GetScenePath(this);
// Read the data from the scene file.
2023-06-24 16:43:10 +02:00
string[] data = File.ReadAllLines(scenePath);
// Split it into blocks.
2023-06-24 16:43:10 +02:00
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.
{
Match match = Regex.Match(data[i], pattern);
2023-06-24 16:43:10 +02:00
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]);
}
2023-06-24 16:43:10 +02:00
lastIndex = i;
}
}
// Write header to disk.
File.WriteAllLines($"{dataPath}/{SceneName}.yaml", data[0..lastIndex]);
2023-06-24 16:43:10 +02:00
}
/// <summary>
/// Empty the scene and save it (so it has no changes in source control).
/// </summary>
2023-06-24 16:43:10 +02:00
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--)
{
DestroyImmediate(gameObjects[i]);
2023-06-24 16:43:10 +02:00
}
EditorSceneManager.SaveScene(scene);
scenePartitionData.LoadedScenePartitions.Clear();
2023-06-24 16:43:10 +02:00
AssetDatabase.Refresh();
}
private void DeleteLoadedPartitions()
{
if (!scenePartitionData.HasCreatedPartitions) return;
2023-06-24 16:43:10 +02:00
foreach (KeyValuePair<uint, ScenePartition> scenePartition in scenePartitionData.LoadedScenePartitions)
2023-06-24 16:43:10 +02:00
{
if (!File.Exists(scenePartition.Value.filePath)) continue;
2023-06-24 16:43:10 +02:00
File.Delete(scenePartition.Value.filePath);
2023-06-24 16:43:10 +02:00
}
}
public void LoadPartitions(uint[] ids)
{
SortedSet<uint> partitionIds = new SortedSet<uint>();
for (int i = 0; i < ids.Length; i++)
{
SortedSet<uint> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(scenePartitionData.ScenePartitions, ids[i]);
foreach (uint c in connections)
{
partitionIds.Add(c);
}
}
LoadScenePartitions(partitionIds);
}
}
}