generated from max/template-unity-project
max
c42a23d6fa
- Grid generation - Grid loading - GridIds - Still having issues with serialization of the grid data structure
253 lines
8.5 KiB
C#
253 lines
8.5 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Text.RegularExpressions;
|
|
using UnityEditor;
|
|
using UnityEditor.SceneManagement;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
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;
|
|
|
|
public List<uint> alwaysLoadIds = new List<uint> { 0, 1, 2, 3, 4 };
|
|
|
|
public ScenePartitionData Data
|
|
{
|
|
get
|
|
{
|
|
// Load data from the ScriptableSingleton.
|
|
data ??= ScenePartitionSS.instance.GetScenePartitionData(this);
|
|
return data;
|
|
}
|
|
}
|
|
private ScenePartitionData data = null;
|
|
|
|
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);
|
|
Save();
|
|
|
|
AssetDatabase.SaveAssets();
|
|
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()
|
|
{
|
|
CreateScenePartitions();
|
|
SortedSet<uint> ids = new SortedSet<uint>(Data.ScenePartitions.Keys);
|
|
LoadScenePartitions(ids);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Discard changes and reload loaded partitions.
|
|
/// </summary>
|
|
public void Reload()
|
|
{
|
|
if (!Data.HasLoadedPartitions) return;
|
|
LoadScenePartitions(new SortedSet<uint>(Data.LoadedScenePartitions.Keys));
|
|
}
|
|
|
|
private void CreateScenePartitions()
|
|
{
|
|
string dataPath = ScenePartitionUtils.GetDataPath(this);
|
|
string[] files = Directory.GetFiles(dataPath);
|
|
|
|
Data.ScenePartitions = new ScenePartitionSortedList();
|
|
|
|
for (int i = 0; i < files.Length; i++)
|
|
{
|
|
ScenePartition scenePartition = new ScenePartition(files[i]);
|
|
Data.ScenePartitions.Add(scenePartition.id, scenePartition);
|
|
}
|
|
}
|
|
|
|
private void LoadScenePartitions(SortedSet<uint> partitionIds)
|
|
{
|
|
if (!Data.HasCreatedPartitions) return;
|
|
|
|
string scenePath = ScenePartitionUtils.GetScenePath(this);
|
|
|
|
List<string> sceneData = new List<string>();
|
|
|
|
Data.LoadedScenePartitions.Clear();
|
|
|
|
// Add default ids.
|
|
for (int i = 0; i < alwaysLoadIds.Count; i++)
|
|
{
|
|
if (Data.ScenePartitions.ContainsKey(alwaysLoadIds[i]))
|
|
{
|
|
partitionIds.Add(alwaysLoadIds[i]);
|
|
}
|
|
}
|
|
|
|
// Create scene data.
|
|
foreach (uint id in partitionIds)
|
|
{
|
|
ScenePartition p = Data.ScenePartitions[id];
|
|
sceneData.AddRange(File.ReadAllLines(p.filePath));
|
|
Data.LoadedScenePartitions.Add(p.id, p);
|
|
}
|
|
|
|
// Create scene.
|
|
File.WriteAllLines(scenePath, sceneData);
|
|
|
|
AssetDatabase.Refresh();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert scene to partitions and save them to disk.
|
|
/// </summary>
|
|
public void Save()
|
|
{
|
|
DeleteLoadedPartitions(); // Delete the loaded partitions from disk so we can write the new ones.
|
|
|
|
string pattern = @"&(\d+)";
|
|
string dataPath = ScenePartitionUtils.GetDataPath(this);
|
|
string scenePath = ScenePartitionUtils.GetScenePath(this);
|
|
|
|
// Read the data from the scene file.
|
|
string[] sceneData = File.ReadAllLines(scenePath);
|
|
|
|
// Split it into blocks.
|
|
int lastIndex = sceneData.Length;
|
|
for (int i = sceneData.Length - 1; i >= 0; i--)
|
|
{
|
|
if (sceneData[i].StartsWith("---")) // --- is the start of a new yaml document.
|
|
{
|
|
Match match = Regex.Match(sceneData[i], pattern);
|
|
|
|
if (match.Success)
|
|
{
|
|
// Extract the file number
|
|
string id = match.Groups[1].Value;
|
|
|
|
// Write data to disk.
|
|
File.WriteAllLines($"{dataPath}/{SceneName}-{id}.yaml", sceneData[i..lastIndex]);
|
|
}
|
|
|
|
lastIndex = i;
|
|
}
|
|
}
|
|
|
|
// Write header to disk.
|
|
File.WriteAllLines($"{dataPath}/{SceneName}.yaml", sceneData[0..lastIndex]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Empty the scene and save it (so it has no changes in source control).
|
|
/// </summary>
|
|
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]);
|
|
}
|
|
|
|
EditorSceneManager.SaveScene(scene);
|
|
|
|
Data.LoadedScenePartitions.Clear();
|
|
|
|
AssetDatabase.Refresh();
|
|
}
|
|
|
|
private void DeleteLoadedPartitions()
|
|
{
|
|
if (!Data.HasLoadedPartitions) return;
|
|
|
|
foreach (KeyValuePair<uint, ScenePartition> scenePartition in Data.LoadedScenePartitions)
|
|
{
|
|
if (!File.Exists(scenePartition.Value.filePath)) continue;
|
|
|
|
File.Delete(scenePartition.Value.filePath);
|
|
}
|
|
}
|
|
|
|
public void LoadPartitions(uint[] ids)
|
|
{
|
|
SortedSet<uint> partitionIds = new SortedSet<uint>();
|
|
|
|
for (int i = 0; i < ids.Length; i++)
|
|
{
|
|
SortedSet<uint> connections = ScenePartitionUtils.FindDeeplyLinkedObjects(Data.ScenePartitions, ids[i]);
|
|
|
|
foreach (uint c in connections)
|
|
{
|
|
partitionIds.Add(c);
|
|
}
|
|
}
|
|
|
|
LoadScenePartitions(partitionIds);
|
|
}
|
|
|
|
public void GenerateSceneGridData()
|
|
{
|
|
if (!Data.HasCreatedPartitions) return;
|
|
|
|
LoadAll();
|
|
|
|
Scene scene = EditorSceneManager.OpenScene(ScenePartitionUtils.GetScenePath(this), OpenSceneMode.Single);
|
|
|
|
GameObject[] rootGameObjects = scene.GetRootGameObjects();
|
|
|
|
Data.SceneGrid.Grid.Clear();
|
|
|
|
// Maybe later switch to getting the data from disk instead of loading the scene and then unloading it again.
|
|
foreach (GameObject gameObject in rootGameObjects)
|
|
{
|
|
// https://forum.unity.com/threads/how-to-get-the-local-identifier-in-file-for-scene-objects.265686/
|
|
PropertyInfo inspectorModeInfo = typeof(SerializedObject).GetProperty("inspectorMode", BindingFlags.NonPublic | BindingFlags.Instance);
|
|
SerializedObject serializedObject = new SerializedObject(gameObject.transform);
|
|
inspectorModeInfo.SetValue(serializedObject, InspectorMode.Debug, null);
|
|
SerializedProperty localIdProp = serializedObject.FindProperty("m_LocalIdentfierInFile");
|
|
|
|
uint localId = (uint)localIdProp.intValue;
|
|
|
|
if (Data.ScenePartitions.ContainsKey(localId))
|
|
{
|
|
Data.SceneGrid.Insert(localId, gameObject.transform.position);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"Could not find LocalIdentfierInFile for {gameObject.transform} {gameObject.name} {gameObject.transform.GetInstanceID()}");
|
|
}
|
|
}
|
|
|
|
Unload();
|
|
}
|
|
|
|
public void LoadCell(int gridId)
|
|
{
|
|
if (Data.SceneGrid.Grid.TryGetValue(gridId, out List<uint> ids))
|
|
{
|
|
LoadPartitions(ids.ToArray());
|
|
}
|
|
}
|
|
}
|
|
} |