Skip to content

Commit

Permalink
feat: Add a Tools -> NDM Framework -> Apply on Build option
Browse files Browse the repository at this point in the history
Closes: #437
  • Loading branch information
bdunderscore committed Oct 5, 2024
1 parent a606812 commit 3047e41
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
12 changes: 10 additions & 2 deletions Editor/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

using System;
using nadena.dev.ndmf.config.runtime;
using UnityEditor;
using UnityEngine;

#endregion

Expand All @@ -48,6 +46,16 @@ public static bool ApplyOnPlay
}
}

public static bool ApplyOnBuild
{
get => NonPersistentConfig.instance.applyOnBuild;
set
{
NonPersistentConfig.instance.applyOnBuild = value;
NotifyChange();
}
}

/// <summary>
/// This event will be invoked when any config value changes.
/// </summary>
Expand Down
16 changes: 12 additions & 4 deletions Editor/UI/Menus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions Editor/VRChat/BuildFrameworkPreprocessHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Diagnostics;
using nadena.dev.ndmf.config;
using nadena.dev.ndmf.runtime;
using UnityEditor;
using UnityEngine;
Expand All @@ -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<AlreadyProcessedTag>()?.processingCompleted == true) return true;

Expand Down Expand Up @@ -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<AlreadyProcessedTag>()?.processingCompleted == true) return true;

var state = HookDedup.RecordAvatar(avatarGameObject);
Expand Down
6 changes: 4 additions & 2 deletions Runtime/NonPersistentConfig.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using UnityEngine;
using UnityEditor;
using UnityEngine;

namespace nadena.dev.ndmf.config.runtime
{
internal class NonPersistentConfig
#if UNITY_EDITOR
: UnityEditor.ScriptableSingleton<NonPersistentConfig>
: ScriptableSingleton<NonPersistentConfig>
#endif
{
[SerializeField] public bool applyOnPlay = true;
[SerializeField] public bool applyOnBuild = true;
}
}

0 comments on commit 3047e41

Please sign in to comment.