Skip to content

Preserve asset and scene registry entry order on update #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions Editor/GuidRegistryUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,23 @@ public static void UpdateAssetsGuidRegistry(IEnumerable<string> scenePaths)
}
}

// Avoid the asset being re-written because the order in
// which assets populated can change
foreach (AssetGuidRegistryEntry entry in prevAssetsGuid.GetAllEntries())
{
if (assets.Contains(entry.@object))
{
assetsGuidRegistry.TryAdd(entry);
}
}

foreach (var asset in assets.Where(IsAsset))
{
if (asset.GetType().Namespace == typeof(UnityEditor.Editor).Namespace)
continue;
var hasPrevGuid = prevAssetsGuid.TryGetEntry(asset, out var prevAssetGuid);
var hasPrevGuid = assetsGuidRegistry.TryGetEntry(asset, out var _);

if (hasPrevGuid)
assetsGuidRegistry.TryAdd(prevAssetGuid);
else
if (!hasPrevGuid)
assetsGuidRegistry.GetOrCreateEntry(asset);
}

Expand Down Expand Up @@ -148,16 +156,26 @@ public static void UpdateScenesGuidRegistry(IEnumerable<string> scenePaths)
sceneObjects.AddRange(EditorUtility.CollectDependencies(scene.GetRootGameObjects())
.Where(dependency => dependency != null));

// The order of the objects added is not the same everytime sceneObjects is populated.
// To SceneGuidRegistry being updated at each bild/playModeStateChanged.
// 1. Add all objects already in the registry in the same order
foreach (SceneGuidRegistryEntry entry in prevSceneObjectsGuid.GetAllEntries())
{
if (sceneObjects.Contains(entry.@object))
{
sceneObjectsGuidRegistry.TryAdd(entry);
}
}

// 2. Add new objects
foreach (var sceneObject in sceneObjects.Where(sceneObject => !IsAsset(sceneObject)))
{
if (sceneObject.GetType().Namespace == typeof(UnityEditor.Editor).Namespace)
continue;

var hasPrevGuid = prevSceneObjectsGuid.TryGetEntry(sceneObject, out var prevSceneObjectGuid);
var hasPrevGuid = sceneObjectsGuidRegistry.TryGetEntry(sceneObject, out var _);

if (hasPrevGuid)
sceneObjectsGuidRegistry.TryAdd(prevSceneObjectGuid);
else
if (!hasPrevGuid)
sceneObjectsGuidRegistry.GetOrCreateEntry(sceneObject);
}

Expand Down
2 changes: 1 addition & 1 deletion Runtime/AssetsGuidRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public GuidRegistry<AssetGuidRegistryEntry> Copy()
return registry.Copy();
}

public bool TryGetValue(Object obj, out AssetGuidRegistryEntry entry)
public bool TryGetEntry(Object obj, out AssetGuidRegistryEntry entry)
{
return registry.TryGetEntry(obj, out entry);
}
Expand Down