diff --git a/JotunnLib/Managers/MockSystem/MockManager.cs b/JotunnLib/Managers/MockSystem/MockManager.cs index b995a21a6..369d0ef26 100644 --- a/JotunnLib/Managers/MockSystem/MockManager.cs +++ b/JotunnLib/Managers/MockSystem/MockManager.cs @@ -168,7 +168,7 @@ public static Object GetRealPrefabFromMock(Object unityObject, Type mockObjectTy if (!prefab) { - throw new MockResolveException($"Object with name '{assetName}' was not found.", assetName, mockObjectType); + throw new MockResolveException($"GameObject with name '{assetName}' was not found.", assetName, mockObjectType); } if (childNames.Count > 0) @@ -183,7 +183,7 @@ public static Object GetRealPrefabFromMock(Object unityObject, Type mockObjectTy prefab = child.gameObject; } - if (TryFindAssetInSelfOrChildComponents(prefab, mockObjectType, out asset)) + if (PrefabManager.TryFindAssetInSelfOrChildComponents(prefab, mockObjectType, out asset)) { return asset; } @@ -549,57 +549,5 @@ private static bool FixShader(Material material) return true; } - - private static bool TryFindAssetOfComponent(Component unityObject, Type objectType, out Object asset) - { - var type = unityObject.GetType(); - ClassMember classMember = ClassMember.GetClassMember(type); - - foreach (var member in classMember.Members) - { - if (member.MemberType == objectType && member.HasGetMethod) - { - asset = (Object)member.GetValue(unityObject); - if (asset != null) - { - return asset; - } - } - } - - asset = null; - return false; - } - - private static bool TryFindAssetInSelfOrChildComponents(GameObject unityObject, Type objectType, out Object asset) - { - if (unityObject == null) - { - asset = null; - return false; - } - - foreach (var component in unityObject.GetComponents()) - { - if (!(component is Transform)) - { - if (TryFindAssetOfComponent(component, objectType, out asset)) - { - return (bool)asset; - } - } - } - - foreach (Transform tf in unityObject.transform) - { - if (TryFindAssetInSelfOrChildComponents(tf.gameObject, objectType, out asset)) - { - return (bool)asset; - } - } - - asset = null; - return false; - } } } diff --git a/JotunnLib/Managers/PrefabManager.cs b/JotunnLib/Managers/PrefabManager.cs index 2b58c1039..6062f7646 100644 --- a/JotunnLib/Managers/PrefabManager.cs +++ b/JotunnLib/Managers/PrefabManager.cs @@ -445,6 +445,12 @@ public static Object GetPrefab(Type type, string name) { // load an never release reference to keep it loaded asset.Load(); + + if (asset.Asset.GetType() != type && asset.Asset is GameObject gameObject && TryFindAssetInSelfOrChildComponents(gameObject, type, out Object childAsset)) + { + return childAsset; + } + return asset.Asset; } } @@ -590,5 +596,57 @@ public static void Clear() where T : Object dictionaryCache.Remove(typeof(T)); } } + + private static bool TryFindAssetOfComponent(Component unityObject, Type objectType, out Object asset) + { + var type = unityObject.GetType(); + ClassMember classMember = ClassMember.GetClassMember(type); + + foreach (var member in classMember.Members) + { + if (member.MemberType == objectType && member.HasGetMethod) + { + asset = (Object)member.GetValue(unityObject); + if (asset != null) + { + return asset; + } + } + } + + asset = null; + return false; + } + + internal static bool TryFindAssetInSelfOrChildComponents(GameObject unityObject, Type objectType, out Object asset) + { + if (!unityObject) + { + asset = null; + return false; + } + + foreach (var component in unityObject.GetComponents()) + { + if (!(component is Transform)) + { + if (TryFindAssetOfComponent(component, objectType, out asset)) + { + return (bool)asset; + } + } + } + + foreach (Transform tf in unityObject.transform) + { + if (TryFindAssetInSelfOrChildComponents(tf.gameObject, objectType, out asset)) + { + return (bool)asset; + } + } + + asset = null; + return false; + } } }