From 952a94f50b3ae4279a48b03d019f341e46b2f5b7 Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Thu, 13 Mar 2025 09:16:44 +0900 Subject: [PATCH] feat: `TransitionAutoPlaySpeed`, `TransitionTexSpeed`, and `EdgeShinyAutoPlaySpeed` are previewed only when the object is selected in edit mode close #319 --- Packages/src/Runtime/UIEffect.cs | 15 ++++++++++++ Packages/src/Runtime/UIEffectBase.cs | 14 ++++++++++- Packages/src/Runtime/UIEffectContext.cs | 8 +++++++ .../src/Runtime/UIEffectProjectSettings.cs | 24 +++++++++++++++---- Packages/src/Runtime/UIEffectReplica.cs | 6 +++++ 5 files changed, 62 insertions(+), 5 deletions(-) diff --git a/Packages/src/Runtime/UIEffect.cs b/Packages/src/Runtime/UIEffect.cs index 9be26f4..9b3bc2b 100644 --- a/Packages/src/Runtime/UIEffect.cs +++ b/Packages/src/Runtime/UIEffect.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using Coffee.UIEffectInternal; using UnityEngine; using UnityEngine.Rendering; @@ -988,6 +989,20 @@ namespace Coffee.UIEffects context?.SetGradationDirty(); base.OnValidate(); } + + internal override void SetEnablePreviewIfSelected(GameObject[] selection) + { + var selected = 0 < selection.Length + && (selection.Contains(gameObject) + || replicas.Any(x => x && selection.Contains(x.gameObject))); + + context.SetEnablePreview(selected, effectMaterial); + foreach (var r in replicas) + { + if (!r || !r.isActiveAndEnabled || r.context == null) continue; + r.context.SetEnablePreview(selected, r.effectMaterial); + } + } #endif protected override void OnBeforeCanvasRebuild() diff --git a/Packages/src/Runtime/UIEffectBase.cs b/Packages/src/Runtime/UIEffectBase.cs index 25f0b24..9e8bb8f 100644 --- a/Packages/src/Runtime/UIEffectBase.cs +++ b/Packages/src/Runtime/UIEffectBase.cs @@ -26,6 +26,7 @@ namespace Coffee.UIEffects private bool _canModifyMesh; private Matrix4x4 _prevTransformHash; + public Material effectMaterial => _material; public Graphic graphic => _graphic ? _graphic : _graphic = GetComponent(); public virtual uint effectId => (uint)GetInstanceID(); public virtual float actualSamplingScale => 1; @@ -153,6 +154,10 @@ namespace Coffee.UIEffects { OnValidate(); } + + internal virtual void SetEnablePreviewIfSelected(GameObject[] selection) + { + } #endif protected override void OnRectTransformDimensionsChange() @@ -207,10 +212,17 @@ namespace Coffee.UIEffects if (!isActiveAndEnabled || context == null || !material) return; context.ApplyToMaterial(material, actualSamplingScale); - Misc.QueuePlayerLoopUpdate(); #if UNITY_EDITOR + Profiler.BeginSample("(Editor/UIE)[UIEffect] ApplyContextToMaterial Post Process"); + if (!Application.isPlaying) + { + SetEnablePreviewIfSelected(Selection.gameObjects); + Misc.QueuePlayerLoopUpdate(); + } + UIEffectProjectSettings.shaderRegistry.RegisterVariant(material, "UI > UIEffect"); + Profiler.EndSample(); #endif } diff --git a/Packages/src/Runtime/UIEffectContext.cs b/Packages/src/Runtime/UIEffectContext.cs index 1c641f4..ecf1cbc 100644 --- a/Packages/src/Runtime/UIEffectContext.cs +++ b/Packages/src/Runtime/UIEffectContext.cs @@ -429,6 +429,14 @@ namespace Coffee.UIEffects Profiler.EndSample(); } + public void SetEnablePreview(bool enable, Material material) + { + if (!material) return; + material.SetVector(s_TransitionTex_Speed, enable ? (Vector4)transitionTexSpeed : Vector4.zero); + material.SetFloat(s_TransitionAutoPlaySpeed, enable ? transitionAutoPlaySpeed : 0); + material.SetFloat(s_EdgeShinyAutoPlaySpeed, enable ? edgeShinyAutoPlaySpeed : 0); + } + private static void SetKeyword(Material material, string[] keywords, int index) { for (var i = 0; i < keywords.Length; i++) diff --git a/Packages/src/Runtime/UIEffectProjectSettings.cs b/Packages/src/Runtime/UIEffectProjectSettings.cs index f27bc94..86390cb 100644 --- a/Packages/src/Runtime/UIEffectProjectSettings.cs +++ b/Packages/src/Runtime/UIEffectProjectSettings.cs @@ -128,6 +128,24 @@ namespace Coffee.UIEffects ("c67f821adbb6348a3a21573a25b55de0", "Hidden-TMP_SDF-UIEffect-Unity6.shader") }); EditorApplication.update += ShaderSampleImporter.Update; + + Selection.selectionChanged += () => + { + if (Application.isPlaying || Misc.isBatchOrBuilding) return; + + var selection = Selection.gameObjects; + foreach (var c in FindAll()) + { + if (!c || !c.isActiveAndEnabled) continue; + c.SetEnablePreviewIfSelected(selection); + } + }; + } + + private static IEnumerable FindAll() + { + return Misc.FindObjectsOfType() + .Concat(Misc.GetAllComponentsInPrefabStage()); } protected override void OnEnable() @@ -222,8 +240,7 @@ namespace Coffee.UIEffects } // Refresh all UIEffect instances. - foreach (var c in Misc.FindObjectsOfType() - .Concat(Misc.GetAllComponentsInPrefabStage())) + foreach (var c in FindAll()) { c.ReleaseMaterial(); } @@ -232,8 +249,7 @@ namespace Coffee.UIEffects // Refresh all UIEffect instances. EditorApplication.delayCall += () => { - foreach (var c in Misc.FindObjectsOfType() - .Concat(Misc.GetAllComponentsInPrefabStage())) + foreach (var c in FindAll()) { c.SetMaterialDirty(); c.SetVerticesDirty(); diff --git a/Packages/src/Runtime/UIEffectReplica.cs b/Packages/src/Runtime/UIEffectReplica.cs index c059ba8..4b4006e 100644 --- a/Packages/src/Runtime/UIEffectReplica.cs +++ b/Packages/src/Runtime/UIEffectReplica.cs @@ -89,6 +89,12 @@ namespace Coffee.UIEffects RefreshTarget(target); base.OnValidate(); } + + internal override void SetEnablePreviewIfSelected(GameObject[] selection) + { + if (!target) return; + target.SetEnablePreviewIfSelected(selection); + } #endif private void RefreshTarget(UIEffect newTarget)