mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
Fix asset bundle issue and made a class of them
This commit is contained in:
parent
e274fbc030
commit
eebccceaab
@ -22,7 +22,7 @@ namespace NewHorizons.Builder.Props
|
||||
|
||||
if (detail.assetBundle != null)
|
||||
{
|
||||
var prefab = PropBuildManager.LoadPrefab(detail.assetBundle, detail.path, uniqueModName, mod);
|
||||
var prefab = AssetBundleUtilities.LoadPrefab(detail.assetBundle, detail.path, mod);
|
||||
|
||||
detailGO = MakeDetail(go, sector, prefab, detail.position, detail.rotation, detail.scale, detail.alignToNormal);
|
||||
}
|
||||
@ -31,7 +31,7 @@ namespace NewHorizons.Builder.Props
|
||||
try
|
||||
{
|
||||
var prefab = mod.ModHelper.Assets.Get3DObject(detail.objFilePath, detail.mtlFilePath);
|
||||
PropBuildManager.ReplaceShaders(prefab);
|
||||
AssetBundleUtilities.ReplaceShaders(prefab);
|
||||
prefab.SetActive(false);
|
||||
detailGO = MakeDetail(go, sector, prefab, detail.position, detail.rotation, detail.scale, detail.alignToNormal);
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@ using NewHorizons.Utility;
|
||||
using OWML.Common;
|
||||
using NewHorizons.Builder.ShipLog;
|
||||
using NewHorizons.External.Configs;
|
||||
using System.IO;
|
||||
|
||||
namespace NewHorizons.Builder.Props
|
||||
{
|
||||
@ -174,66 +175,5 @@ namespace NewHorizons.Builder.Props
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static GameObject LoadPrefab(string assetBundle, string path, string uniqueModName, IModBehaviour mod)
|
||||
{
|
||||
string key = assetBundle;
|
||||
AssetBundle bundle;
|
||||
GameObject prefab;
|
||||
|
||||
try
|
||||
{
|
||||
if (Main.AssetBundles.ContainsKey(key)) bundle = Main.AssetBundles[key];
|
||||
else
|
||||
{
|
||||
var completePath = mod.ModHelper.Manifest.ModFolderPath + assetBundle;
|
||||
bundle = AssetBundle.LoadFromFile(completePath);
|
||||
if(bundle == null)
|
||||
{
|
||||
Logger.LogError($"Couldn't load AssetBundle at [{completePath}] for [{mod.ModHelper.Manifest.Name}]");
|
||||
return null;
|
||||
}
|
||||
|
||||
Main.AssetBundles[key] = bundle;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogError($"Couldn't load AssetBundle {assetBundle} : {e.Message}");
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
prefab = bundle.LoadAsset<GameObject>(path);
|
||||
prefab.SetActive(false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogError($"Couldn't load asset {path} from AssetBundle {assetBundle} : {e.Message}");
|
||||
return null;
|
||||
}
|
||||
|
||||
ReplaceShaders(prefab);
|
||||
|
||||
return prefab;
|
||||
}
|
||||
|
||||
public static void ReplaceShaders(GameObject prefab)
|
||||
{
|
||||
foreach (var renderer in prefab.GetComponentsInChildren<Renderer>(true))
|
||||
{
|
||||
foreach (var material in renderer.sharedMaterials)
|
||||
{
|
||||
if (material == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var replacementShader = Shader.Find(material.shader.name);
|
||||
if (replacementShader != null) material.shader = replacementShader;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,7 +46,7 @@ namespace NewHorizons.Builder.Props
|
||||
Random.InitState(propInfo.seed);
|
||||
|
||||
GameObject prefab;
|
||||
if (propInfo.assetBundle != null) prefab = PropBuildManager.LoadPrefab(propInfo.assetBundle, propInfo.path, uniqueModName, mod);
|
||||
if (propInfo.assetBundle != null) prefab = AssetBundleUtilities.LoadPrefab(propInfo.assetBundle, propInfo.path, mod);
|
||||
else prefab = GameObject.Find(propInfo.path);
|
||||
for (int i = 0; i < propInfo.count; i++)
|
||||
{
|
||||
|
||||
@ -10,44 +10,10 @@ namespace NewHorizons.Builder.StarSystem
|
||||
{
|
||||
public class SkyboxBuilder
|
||||
{
|
||||
public static Material LoadMaterial(string assetBundle, string path, string uniqueModName, IModBehaviour mod)
|
||||
{
|
||||
string key = uniqueModName + "." + assetBundle;
|
||||
AssetBundle bundle;
|
||||
Material cubemap;
|
||||
|
||||
try
|
||||
{
|
||||
if (Main.AssetBundles.ContainsKey(key)) bundle = Main.AssetBundles[key];
|
||||
else
|
||||
{
|
||||
bundle = mod.ModHelper.Assets.LoadBundle(assetBundle);
|
||||
Main.AssetBundles[key] = bundle;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogError($"Couldn't load AssetBundle {assetBundle} : {e.Message}");
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
cubemap = bundle.LoadAsset<Material>(path);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Log($"Couldn't load asset {path} from AssetBundle {assetBundle} : {e.Message}");
|
||||
return null;
|
||||
}
|
||||
|
||||
return cubemap;
|
||||
}
|
||||
|
||||
public static void Make(StarSystemConfig.SkyboxConfig info, IModBehaviour mod)
|
||||
{
|
||||
Logger.Log("Building Skybox");
|
||||
Material skyBoxMaterial = LoadMaterial(info.assetBundle, info.path, mod.ModHelper.Manifest.UniqueName, mod);
|
||||
var skyBoxMaterial = AssetBundleUtilities.Load<Material>(info.assetBundle, info.path, mod);
|
||||
RenderSettings.skybox = skyBoxMaterial;
|
||||
DynamicGI.UpdateEnvironment();
|
||||
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() =>
|
||||
|
||||
@ -42,7 +42,6 @@ namespace NewHorizons
|
||||
|
||||
public static Dictionary<string, NewHorizonsSystem> SystemDict = new Dictionary<string, NewHorizonsSystem>();
|
||||
public static Dictionary<string, List<NewHorizonsBody>> BodyDict = new Dictionary<string, List<NewHorizonsBody>>();
|
||||
public static Dictionary<string, AssetBundle> AssetBundles = new Dictionary<string, AssetBundle>();
|
||||
public static List<IModBehaviour> MountedAddons = new List<IModBehaviour>();
|
||||
|
||||
public static float SecondsLeftInLoop = -1;
|
||||
@ -115,13 +114,6 @@ namespace NewHorizons
|
||||
}
|
||||
};
|
||||
|
||||
foreach (var pair in AssetBundles)
|
||||
{
|
||||
if (pair.Value == null) Logger.LogError($"The asset bundle for {pair.Key} was null when trying to unload");
|
||||
else pair.Value.Unload(true);
|
||||
}
|
||||
AssetBundles.Clear();
|
||||
|
||||
if (!resetTranslation) return;
|
||||
TranslationHandler.ClearTables();
|
||||
TextTranslation.Get().SetLanguage(TextTranslation.Get().GetLanguage());
|
||||
@ -180,6 +172,7 @@ namespace NewHorizons
|
||||
SearchUtilities.ClearCache();
|
||||
ImageUtilities.ClearCache();
|
||||
AudioUtilities.ClearCache();
|
||||
AssetBundleUtilities.ClearCache();
|
||||
IsSystemReady = false;
|
||||
}
|
||||
|
||||
|
||||
91
NewHorizons/Utility/AssetBundleUtilities.cs
Normal file
91
NewHorizons/Utility/AssetBundleUtilities.cs
Normal file
@ -0,0 +1,91 @@
|
||||
using OWML.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NewHorizons.Utility
|
||||
{
|
||||
public static class AssetBundleUtilities
|
||||
{
|
||||
public static Dictionary<string, AssetBundle> AssetBundles = new Dictionary<string, AssetBundle>();
|
||||
|
||||
public static void ClearCache()
|
||||
{
|
||||
foreach (var pair in AssetBundles)
|
||||
{
|
||||
if (pair.Value == null) Logger.LogError($"The asset bundle for {pair.Key} was null when trying to unload");
|
||||
else pair.Value.Unload(true);
|
||||
}
|
||||
AssetBundles.Clear();
|
||||
}
|
||||
|
||||
public static T Load<T>(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) where T : UnityEngine.Object
|
||||
{
|
||||
string key = Path.GetFileName(assetBundleRelativeDir);
|
||||
T obj;
|
||||
|
||||
try
|
||||
{
|
||||
AssetBundle bundle;
|
||||
|
||||
if (AssetBundles.ContainsKey(key))
|
||||
{
|
||||
bundle = AssetBundles[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
var completePath = mod.ModHelper.Manifest.ModFolderPath + assetBundleRelativeDir;
|
||||
bundle = AssetBundle.LoadFromFile(completePath);
|
||||
if (bundle == null)
|
||||
{
|
||||
Logger.LogError($"Couldn't load AssetBundle at [{completePath}] for [{mod.ModHelper.Manifest.Name}]");
|
||||
return null;
|
||||
}
|
||||
|
||||
AssetBundles[key] = bundle;
|
||||
}
|
||||
|
||||
obj = bundle.LoadAsset<T>(pathInBundle);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogError($"Couldn't load asset {pathInBundle} from AssetBundle {assetBundleRelativeDir} : {e.Message}");
|
||||
return null;
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public static GameObject LoadPrefab(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod)
|
||||
{
|
||||
var prefab = Load<GameObject>(assetBundleRelativeDir, pathInBundle, mod);
|
||||
|
||||
prefab.SetActive(false);
|
||||
|
||||
ReplaceShaders(prefab);
|
||||
|
||||
return prefab;
|
||||
}
|
||||
|
||||
public static void ReplaceShaders(GameObject prefab)
|
||||
{
|
||||
foreach (var renderer in prefab.GetComponentsInChildren<Renderer>(true))
|
||||
{
|
||||
foreach (var material in renderer.sharedMaterials)
|
||||
{
|
||||
if (material == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var replacementShader = Shader.Find(material.shader.name);
|
||||
if (replacementShader != null) material.shader = replacementShader;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user