Skip to content

Commit e6f1311

Browse files
committed
Added ClearScenesAndPrefabForReload
The method clears the loaded scenes/prefabs when reloading the alc, so that even if a scene/prefab is holding a reference to a user-defined script it won't stop the ALC from reloading
1 parent 31d1f88 commit e6f1311

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

Prowl.Editor/AssetsDatabase/EditorAssetDatabase.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,41 @@ public string[] GetAllAssetPaths()
695695
/// <summary>Get an already-loaded asset from memory without triggering import. Returns null if not loaded.</summary>
696696
public EngineObject? GetLoadedAsset(Guid guid) => _loadedAssets.GetValueOrDefault(guid);
697697

698+
/// <summary>
699+
/// Clear the cache for <see cref="_loadedAssets"/> on assembly reload so that scenes/prefabs that might hold
700+
/// user-defined scripts won't stop the ALC from reloading
701+
/// </summary>
702+
[OnAssemblyUnload]
703+
internal static void ClearScenesAndPrefabForReload()
704+
{
705+
var db = Instance;
706+
if (db == null) return;
707+
708+
foreach (var kv in db._loadedAssets.ToArray())
709+
{
710+
EngineObject? asset = kv.Value;
711+
if (asset is null) continue;
712+
713+
bool sensitive = asset is Runtime.Resources.Scene
714+
|| asset is Runtime.Resources.PrefabAsset
715+
|| asset.GetType().Assembly.IsCollectible;
716+
717+
if (!sensitive) continue;
718+
719+
if (db._loadedAssets.TryRemove(kv.Key, out var removed))
720+
{
721+
try
722+
{
723+
removed?.Dispose();
724+
}
725+
catch(Exception e)
726+
{
727+
Debug.LogException(e);
728+
}
729+
}
730+
}
731+
}
732+
698733
/// <summary>Load a cached thumbnail for an asset. Returns (width, height, pixels) or null.</summary>
699734
public (int width, int height, byte[] pixels)? LoadThumbnail(Guid guid) => ThumbnailGenerator.LoadThumbnail(guid, _project.ThumbnailsPath);
700735

Prowl.Runtime/Resources/Scene.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,13 @@ public override void OnDispose()
563563
// Clear any remaining references
564564
_allObj.Clear();
565565
_allObjSet.Clear();
566+
567+
// Remove all identifiers and reference to any possible gameobject that could hold a
568+
// user-defined script as it might leave the ALC alive
569+
serializeObj = null;
570+
_goIdentifiers = null;
571+
_compIdentifiers = null;
572+
_compIdOffsets = null;
566573
}
567574

568575
public void OnBeforeSerialize()

0 commit comments

Comments
 (0)