ScenePartition/Editor/ScenePartitionUtils.cs
max ebb8ba559e MenuItems for GameObjects
- CopyObjectId
- AddToAlwaysLoad
- RemoveFromAlwaysLoad
- Added utility to get the TargetObjectId

closes #10
2023-07-27 18:23:03 +02:00

111 lines
3.9 KiB
C#

using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace VertexColor.ScenePartition.Editor
{
public static class ScenePartitionUtils
{
public static string GetDataPath(ScenePartitionSO scenePartitionSO)
{
string dataPath = Path.Combine(Application.dataPath, GetRelativeFilePath(scenePartitionSO));
if (!Directory.Exists(dataPath))
{
Directory.CreateDirectory(dataPath);
}
return dataPath;
}
public static string GetRelativeFilePath(ScenePartitionSO scenePartitionSO)
{
return $"../Data/ScenePartition/{scenePartitionSO.name}";
}
public static string GetScenePath(ScenePartitionSO scenePartitionSO)
{
string scenePath = AssetDatabase.GetAssetOrScenePath(scenePartitionSO.SceneAsset);
return scenePath;
}
public static SortedSet<ulong> FindDeeplyLinkedObjects(SortedList<ulong, ScenePartition> scenePartitions, ulong partitionId)
{
using (new ProfilerUtility.ProfilerScope($"{nameof(FindDeeplyLinkedObjects)}"))
{
SortedSet<ulong> linkedObjects = new SortedSet<ulong>();
FindDeeplyLinkedObjectsRecursive(scenePartitions, partitionId, linkedObjects);
return linkedObjects;
}
}
private static void FindDeeplyLinkedObjectsRecursive(SortedList<ulong, ScenePartition> scenePartitions, ulong partitionId, SortedSet<ulong> linkedObjects)
{
if (linkedObjects.Contains(partitionId)) return;
if (!scenePartitions.TryGetValue(partitionId, out ScenePartition partition)) return;
linkedObjects.Add(partitionId);
foreach (ulong reference in partition.references)
{
FindDeeplyLinkedObjectsRecursive(scenePartitions, reference, linkedObjects);
if (scenePartitions.TryGetValue(reference, out ScenePartition referencedPartition))
{
foreach (ulong subReference in referencedPartition.references)
{
FindDeeplyLinkedObjectsRecursive(scenePartitions, subReference, linkedObjects);
}
}
}
}
public static bool TryGetScenePartitionSOForActiveScene(out ScenePartitionSO scenePartitionSO)
{
using (new ProfilerUtility.ProfilerScope($"{nameof(TryGetScenePartitionSOForActiveScene)}"))
{
scenePartitionSO = null;
UnityEngine.SceneManagement.Scene activeScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
if (activeScene == null) return false;
string filter = $"t:{nameof(ScenePartitionSO)}";
string[] assets = AssetDatabase.FindAssets(filter);
if (assets == null || assets.Length <= 0) return false;
for (int i = 0; i < assets.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(assets[i]);
ScenePartitionSO scenePartition = AssetDatabase.LoadAssetAtPath<ScenePartitionSO>(path);
if (scenePartition == null) continue;
if (scenePartition.SceneName == activeScene.name)
{
scenePartitionSO = scenePartition;
return true;
}
}
return false;
}
}
public static ulong GetTargetObjectId(Object targetObject)
{
GlobalObjectId globalId = GlobalObjectId.GetGlobalObjectIdSlow(targetObject);
if (globalId.targetPrefabId == 0) // 0 = no prefab.
{
return globalId.targetObjectId;
}
else
{
return globalId.targetPrefabId;
}
}
}
}