From 5549332a68b6b7c8db6d8f5d59d735fabeafbfce Mon Sep 17 00:00:00 2001 From: bd_ Date: Mon, 29 Aug 2022 18:34:22 -0700 Subject: [PATCH] Add option to auto-apply on play without av3emu --- .../Editor/ApplyOnPlay.cs | 45 +++++++++++++++++++ .../Editor/ApplyOnPlay.cs.meta | 3 ++ .../Editor/ModularAvatarSettings.cs | 19 ++++++++ .../Editor/ModularAvatarSettings.cs.meta | 3 ++ 4 files changed, 70 insertions(+) create mode 100644 Packages/net.fushizen.modular-avatar/Editor/ApplyOnPlay.cs create mode 100644 Packages/net.fushizen.modular-avatar/Editor/ApplyOnPlay.cs.meta create mode 100644 Packages/net.fushizen.modular-avatar/Editor/ModularAvatarSettings.cs create mode 100644 Packages/net.fushizen.modular-avatar/Editor/ModularAvatarSettings.cs.meta diff --git a/Packages/net.fushizen.modular-avatar/Editor/ApplyOnPlay.cs b/Packages/net.fushizen.modular-avatar/Editor/ApplyOnPlay.cs new file mode 100644 index 00000000..c67f8026 --- /dev/null +++ b/Packages/net.fushizen.modular-avatar/Editor/ApplyOnPlay.cs @@ -0,0 +1,45 @@ +using UnityEditor; +using UnityEngine; +using UnityEngine.SceneManagement; +using VRC.SDK3.Avatars.Components; +using VRC.SDKBase.Editor.BuildPipeline; + +namespace net.fushizen.modular_avatar.core.editor +{ + [InitializeOnLoad] + public static class ApplyOnPlay + { + private const string MENU_NAME = "Tools/Modular Avatar/Apply on Play"; + + static ApplyOnPlay() + { + EditorApplication.playModeStateChanged += OnPlayModeStateChanged; + Menu.SetChecked(MENU_NAME, ModularAvatarSettings.applyOnPlay); + } + + [MenuItem(MENU_NAME)] + private static void ToggleApplyOnPlay() + { + ModularAvatarSettings.applyOnPlay = !ModularAvatarSettings.applyOnPlay; + Menu.SetChecked(MENU_NAME, ModularAvatarSettings.applyOnPlay); + } + + private static void OnPlayModeStateChanged(PlayModeStateChange obj) + { + if (obj == PlayModeStateChange.EnteredPlayMode && ModularAvatarSettings.applyOnPlay) + { + // TODO - only apply modular avatar changes? + foreach (var root in SceneManager.GetActiveScene().GetRootGameObjects()) + { + foreach (var avatar in root.GetComponentsInChildren(true)) + { + if (avatar.GetComponentsInChildren(true).Length > 0) + { + VRCBuildPipelineCallbacks.OnPreprocessAvatar(avatar.gameObject); + } + } + } + } + } + } +} \ No newline at end of file diff --git a/Packages/net.fushizen.modular-avatar/Editor/ApplyOnPlay.cs.meta b/Packages/net.fushizen.modular-avatar/Editor/ApplyOnPlay.cs.meta new file mode 100644 index 00000000..b2f1a06b --- /dev/null +++ b/Packages/net.fushizen.modular-avatar/Editor/ApplyOnPlay.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 246c8f6cb27c4758972ceac5e8700add +timeCreated: 1661822272 \ No newline at end of file diff --git a/Packages/net.fushizen.modular-avatar/Editor/ModularAvatarSettings.cs b/Packages/net.fushizen.modular-avatar/Editor/ModularAvatarSettings.cs new file mode 100644 index 00000000..1d99bd9d --- /dev/null +++ b/Packages/net.fushizen.modular-avatar/Editor/ModularAvatarSettings.cs @@ -0,0 +1,19 @@ +using UnityEditor; +using UnityEngine; + +namespace net.fushizen.modular_avatar.core.editor +{ + public static class ModularAvatarSettings + { + private const string PREFKEY_APPLY_ON_PLAY = "net.fushizen.modular-avatar.applyOnPlay"; +#if UNITY_EDITOR + public static bool applyOnPlay + { + get => EditorPrefs.GetBool(PREFKEY_APPLY_ON_PLAY, true); + set => EditorPrefs.SetBool(PREFKEY_APPLY_ON_PLAY, value); + } + #else + public static bool applyOnPlay = false; + #endif + } +} \ No newline at end of file diff --git a/Packages/net.fushizen.modular-avatar/Editor/ModularAvatarSettings.cs.meta b/Packages/net.fushizen.modular-avatar/Editor/ModularAvatarSettings.cs.meta new file mode 100644 index 00000000..14b5b04f --- /dev/null +++ b/Packages/net.fushizen.modular-avatar/Editor/ModularAvatarSettings.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: bf636eb59d084fb3ae6ce8277adde230 +timeCreated: 1661822033 \ No newline at end of file