From 3047e413d9cb98ea96f55d4ef6901790224d4de2 Mon Sep 17 00:00:00 2001 From: bd_ Date: Sat, 5 Oct 2024 15:02:11 -0700 Subject: [PATCH] feat: Add a `Tools -> NDM Framework -> Apply on Build` option Closes: #437 --- Editor/Config.cs | 12 ++++++++++-- Editor/UI/Menus.cs | 16 ++++++++++++---- Editor/VRChat/BuildFrameworkPreprocessHook.cs | 9 +++++++++ Runtime/NonPersistentConfig.cs | 6 ++++-- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/Editor/Config.cs b/Editor/Config.cs index 2fa6bfbe..edfa1222 100644 --- a/Editor/Config.cs +++ b/Editor/Config.cs @@ -26,8 +26,6 @@ using System; using nadena.dev.ndmf.config.runtime; -using UnityEditor; -using UnityEngine; #endregion @@ -48,6 +46,16 @@ public static bool ApplyOnPlay } } + public static bool ApplyOnBuild + { + get => NonPersistentConfig.instance.applyOnBuild; + set + { + NonPersistentConfig.instance.applyOnBuild = value; + NotifyChange(); + } + } + /// /// This event will be invoked when any config value changes. /// diff --git a/Editor/UI/Menus.cs b/Editor/UI/Menus.cs index 29081e8d..5c043040 100644 --- a/Editor/UI/Menus.cs +++ b/Editor/UI/Menus.cs @@ -6,7 +6,6 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; using nadena.dev.ndmf.config; -using nadena.dev.ndmf.cs; using nadena.dev.ndmf.runtime; using UnityEditor; using UnityEngine; @@ -20,11 +19,13 @@ namespace nadena.dev.ndmf.ui internal static class Menus { private const string APPLY_ON_PLAY_MENU_NAME = "Tools/NDM Framework/Apply on Play"; + private const string APPLY_ON_BUILD_MENU_NAME = "Tools/NDM Framework/Apply on Build"; private const string TOPLEVEL_MANUAL_BAKE_MENU_NAME = "Tools/NDM Framework/Manual bake avatar"; internal const string ENABLE_PREVIEW_MENU_NAME = "Tools/NDM Framework/Enable Previews"; private const int APPLY_ON_PLAY_PRIO = 1; - private const int TOPLEVEL_MANUAL_BAKE_PRIO = 2; - internal const int ENABLE_PREVIEW_PRIO = 3; + private const int APPLY_ON_BUILD_PRIO = APPLY_ON_PLAY_PRIO + 1; + private const int TOPLEVEL_MANUAL_BAKE_PRIO = APPLY_ON_BUILD_PRIO + 1; + internal const int ENABLE_PREVIEW_PRIO = TOPLEVEL_MANUAL_BAKE_PRIO + 1; [InitializeOnLoadMethod] static void Init() @@ -63,11 +64,18 @@ private static void ApplyOnPlay() Config.ApplyOnPlay = !Config.ApplyOnPlay; } + + [MenuItem(APPLY_ON_BUILD_MENU_NAME, false, APPLY_ON_BUILD_PRIO)] + private static void ApplyOnBuild() + { + Config.ApplyOnBuild = !Config.ApplyOnBuild; + } + private static void OnSettingsChanged() { Menu.SetChecked(APPLY_ON_PLAY_MENU_NAME, Config.ApplyOnPlay); + Menu.SetChecked(APPLY_ON_BUILD_MENU_NAME, Config.ApplyOnBuild); } - #if NDMF_DEBUG [MenuItem("Tools/NDM Framework/Debug Tools/Domain Reload", false, 101)] private static void DomainReload() diff --git a/Editor/VRChat/BuildFrameworkPreprocessHook.cs b/Editor/VRChat/BuildFrameworkPreprocessHook.cs index 576edce8..dc229970 100644 --- a/Editor/VRChat/BuildFrameworkPreprocessHook.cs +++ b/Editor/VRChat/BuildFrameworkPreprocessHook.cs @@ -4,6 +4,7 @@ using System; using System.Diagnostics; +using nadena.dev.ndmf.config; using nadena.dev.ndmf.runtime; using UnityEditor; using UnityEngine; @@ -28,6 +29,10 @@ internal class BuildFrameworkPreprocessHook : IVRCSDKPreprocessAvatarCallback public bool OnPreprocessAvatar(GameObject avatarGameObject) { + var isPlayMode = EditorApplication.isPlayingOrWillChangePlaymode; + var shouldApply = isPlayMode ? Config.ApplyOnPlay : Config.ApplyOnBuild; + if (!shouldApply) return true; + // Legacy: For VRCF if (avatarGameObject.GetComponent()?.processingCompleted == true) return true; @@ -60,6 +65,10 @@ internal class BuildFrameworkOptimizeHook : IVRCSDKPreprocessAvatarCallback public bool OnPreprocessAvatar(GameObject avatarGameObject) { + var isPlayMode = EditorApplication.isPlayingOrWillChangePlaymode; + var shouldApply = isPlayMode ? Config.ApplyOnPlay : Config.ApplyOnBuild; + if (!shouldApply) return true; + if (avatarGameObject.GetComponent()?.processingCompleted == true) return true; var state = HookDedup.RecordAvatar(avatarGameObject); diff --git a/Runtime/NonPersistentConfig.cs b/Runtime/NonPersistentConfig.cs index 334378e2..b7f46be5 100644 --- a/Runtime/NonPersistentConfig.cs +++ b/Runtime/NonPersistentConfig.cs @@ -1,12 +1,14 @@ -using UnityEngine; +using UnityEditor; +using UnityEngine; namespace nadena.dev.ndmf.config.runtime { internal class NonPersistentConfig #if UNITY_EDITOR - : UnityEditor.ScriptableSingleton + : ScriptableSingleton #endif { [SerializeField] public bool applyOnPlay = true; + [SerializeField] public bool applyOnBuild = true; } } \ No newline at end of file