From 3f4885e7072c338a71469ee65dbf5033b438f00e Mon Sep 17 00:00:00 2001 From: bd_ Date: Sat, 10 Sep 2022 09:08:16 -0700 Subject: [PATCH] Avoid scary errors when entering play mode As part of our hook processing, we destroy modular-avatar components; however, when entering play mode (without av3emu), these components might be prefab assets. To avoid scary unity errors, unpack these prefabs when entering play mode. --- .../Editor/ApplyOnPlay.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Packages/net.fushizen.modular-avatar/Editor/ApplyOnPlay.cs b/Packages/net.fushizen.modular-avatar/Editor/ApplyOnPlay.cs index 906bc297..1a47d935 100644 --- a/Packages/net.fushizen.modular-avatar/Editor/ApplyOnPlay.cs +++ b/Packages/net.fushizen.modular-avatar/Editor/ApplyOnPlay.cs @@ -23,6 +23,7 @@ */ using UnityEditor; +using UnityEngine; using UnityEngine.SceneManagement; using VRC.SDK3.Avatars.Components; using VRC.SDKBase.Editor.BuildPipeline; @@ -58,11 +59,27 @@ namespace net.fushizen.modular_avatar.core.editor { if (avatar.GetComponentsInChildren(true).Length > 0) { + UnpackPrefabsCompletely(avatar.gameObject); VRCBuildPipelineCallbacks.OnPreprocessAvatar(avatar.gameObject); } } } } } + + private static void UnpackPrefabsCompletely(GameObject obj) + { + if (PrefabUtility.IsAnyPrefabInstanceRoot(obj)) + { + PrefabUtility.UnpackPrefabInstance(obj, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction); + } + else + { + foreach (Transform child in obj.transform) + { + UnpackPrefabsCompletely(child.gameObject); + } + } + } } } \ No newline at end of file