cache LoadPrefab

This commit is contained in:
JohnCorby 2025-01-29 17:10:28 -08:00
parent 043269e6ac
commit 1961076664
4 changed files with 12 additions and 8 deletions

View File

@ -36,11 +36,6 @@ namespace NewHorizons.Builder.Props
}
}
static DetailBuilder()
{
SceneManager.sceneUnloaded += SceneManager_sceneUnloaded;
}
#region obsolete
// Never change method signatures, people directly reference the NH dll and it can break backwards compatibility
// In particular, Outer Wives needs this method signature
@ -54,7 +49,7 @@ namespace NewHorizons.Builder.Props
=> Make(go, sector, mod: null, detail);
#endregion
private static void SceneManager_sceneUnloaded(Scene scene)
public static void ClearCache()
{
foreach (var prefab in _fixedPrefabCache.Values)
{

View File

@ -51,7 +51,7 @@ namespace NewHorizons.Handlers
/// </summary>
public static void SetUpStreaming(GameObject obj, Sector sector)
{
return;
// TODO: used OFTEN by detail builder. 20ms adds up to seconds. speed up!
// find the asset bundles to load
// tries the cache first, then builds

View File

@ -308,6 +308,7 @@ namespace NewHorizons
ImageUtilities.ClearCache();
AudioUtilities.ClearCache();
AssetBundleUtilities.ClearCache();
DetailBuilder.ClearCache();
}
IsSystemReady = false;

View File

@ -12,6 +12,7 @@ namespace NewHorizons.Utility.Files
public static class AssetBundleUtilities
{
public static Dictionary<string, (AssetBundle bundle, bool keepLoaded)> AssetBundles = new();
private static Dictionary<string, GameObject> _prefabCache = new();
private static readonly List<AssetBundleCreateRequest> _loadingBundles = new();
@ -52,6 +53,7 @@ namespace NewHorizons.Utility.Files
}
AssetBundles = AssetBundles.Where(x => x.Value.keepLoaded).ToDictionary(x => x.Key, x => x.Value);
_prefabCache.Clear();
}
public static void PreloadBundle(string assetBundleRelativeDir, IModBehaviour mod)
@ -113,11 +115,17 @@ namespace NewHorizons.Utility.Files
public static GameObject LoadPrefab(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod)
{
var prefab = Load<GameObject>(assetBundleRelativeDir, pathInBundle, mod);
if (_prefabCache.TryGetValue(assetBundleRelativeDir + pathInBundle, out var prefab))
return prefab;
prefab = Load<GameObject>(assetBundleRelativeDir, pathInBundle, mod);
prefab.SetActive(false);
ReplaceShaders(prefab);
// replacing shaders is expensive, so cache it
_prefabCache.Add(assetBundleRelativeDir + pathInBundle, prefab);
return prefab;
}