Add load bundle method

This commit is contained in:
Noah Pilarski 2025-08-04 21:04:42 -04:00
parent 9233d103ac
commit 43d70b21d9

View File

@ -98,31 +98,38 @@ namespace NewHorizons.Utility.Files
/// </summary>
public static bool AreRequiredAssetsLoaded() => _loadingBundles.Count == 0;
public static T Load<T>(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) where T : UnityEngine.Object
public static AssetBundle LoadBundle(string assetBundleRelativeDir, IModBehaviour mod, bool keepLoaded = false)
{
string key = Path.GetFileName(assetBundleRelativeDir);
AssetBundle bundle;
if (AssetBundles.ContainsKey(key))
{
bundle = AssetBundles[key].bundle;
}
else
{
var completePath = Path.Combine(mod.ModHelper.Manifest.ModFolderPath, assetBundleRelativeDir);
bundle = AssetBundle.LoadFromFile(completePath);
if (bundle == null)
{
NHLogger.LogError($"Couldn't load AssetBundle at [{completePath}] for [{mod.ModHelper.Manifest.Name}]");
return null;
}
AssetBundles[key] = (bundle, keepLoaded);
}
return bundle;
}
public static T Load<T>(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) where T : UnityEngine.Object
{
T obj;
try
{
AssetBundle bundle;
if (AssetBundles.ContainsKey(key))
{
bundle = AssetBundles[key].bundle;
}
else
{
var completePath = Path.Combine(mod.ModHelper.Manifest.ModFolderPath, assetBundleRelativeDir);
bundle = AssetBundle.LoadFromFile(completePath);
if (bundle == null)
{
NHLogger.LogError($"Couldn't load AssetBundle at [{completePath}] for [{mod.ModHelper.Manifest.Name}]");
return null;
}
AssetBundles[key] = (bundle, false);
}
AssetBundle bundle = LoadBundle(assetBundleRelativeDir, mod);
obj = bundle.LoadAsset<T>(pathInBundle);
}