From e6fb78a0f67ed6f70be08259f674ab2f83364bd7 Mon Sep 17 00:00:00 2001 From: 45375dfhg Date: Sat, 11 Oct 2025 13:18:18 +0200 Subject: [PATCH] fix: [improve performance] code optimization --- .../Runtime/Internal/Utilities/ObjectPool.cs | 2 +- .../Utilities/ShaderVariantRegistry.cs | 18 +++-- Packages/src/Runtime/UIEffect.cs | 11 +-- Packages/src/Runtime/UIEffectContext.cs | 68 ++++++++++--------- Packages/src/Runtime/UIEffectReplica.cs | 27 ++++---- .../src/Runtime/Utilities/GraphicProxy.cs | 7 +- Packages/src/Runtime/Utilities/ImageProxy.cs | 2 +- Packages/src/Runtime/Utilities/TmpProxy.cs | 2 +- 8 files changed, 73 insertions(+), 64 deletions(-) diff --git a/Packages/src/Runtime/Internal/Utilities/ObjectPool.cs b/Packages/src/Runtime/Internal/Utilities/ObjectPool.cs index 5d01a25..a203df2 100644 --- a/Packages/src/Runtime/Internal/Utilities/ObjectPool.cs +++ b/Packages/src/Runtime/Internal/Utilities/ObjectPool.cs @@ -6,7 +6,7 @@ namespace Coffee.UIEffectInternal /// /// Object pool. /// - internal class InternalObjectPool where T : class + internal sealed class InternalObjectPool where T : class { #if UNITY_2021_1_OR_NEWER private readonly Predicate _onValid; // Delegate for checking if instances are valid diff --git a/Packages/src/Runtime/Internal/Utilities/ShaderVariantRegistry.cs b/Packages/src/Runtime/Internal/Utilities/ShaderVariantRegistry.cs index c2da4bb..4900818 100644 --- a/Packages/src/Runtime/Internal/Utilities/ShaderVariantRegistry.cs +++ b/Packages/src/Runtime/Internal/Utilities/ShaderVariantRegistry.cs @@ -17,7 +17,7 @@ using Object = UnityEngine.Object; namespace Coffee.UIEffectInternal { [Serializable] - public class ShaderVariantRegistry + public sealed class ShaderVariantRegistry { [Serializable] internal class StringPair : IEquatable @@ -27,9 +27,7 @@ namespace Coffee.UIEffectInternal public bool Equals(StringPair other) { - if (other == null) return false; - if (ReferenceEquals(this, other)) return true; - return key == other.key && value == other.value; + return other != null && key == other.key && value == other.value; } public override bool Equals(object obj) @@ -89,15 +87,15 @@ namespace Coffee.UIEffectInternal // Find optional shader. Shader optionalShader; - foreach (var pair in m_OptionalShaders) + var count = m_OptionalShaders.Count; + for (var i = 0; i < count; i++) { + var pair = m_OptionalShaders[i]; if (pair.key != shaderName) continue; optionalShader = Shader.Find(pair.value); - if (optionalShader) - { - _cachedOptionalShaders[id] = pair.value; - return optionalShader; - } + if (!optionalShader) continue; + _cachedOptionalShaders[id] = pair.value; + return optionalShader; } // Find optional shader by format. diff --git a/Packages/src/Runtime/UIEffect.cs b/Packages/src/Runtime/UIEffect.cs index 3e4b2fc..1deaaba 100644 --- a/Packages/src/Runtime/UIEffect.cs +++ b/Packages/src/Runtime/UIEffect.cs @@ -10,7 +10,7 @@ namespace Coffee.UIEffects [Icon("Packages/com.coffee.ui-effect/Editor/UIEffectIconIcon.png")] [ExecuteAlways] [DisallowMultipleComponent] - public class UIEffect : UIEffectBase + public sealed class UIEffect : UIEffectBase { [SerializeField] protected ToneFilter m_ToneFilter = ToneFilter.None; @@ -1494,7 +1494,8 @@ namespace Coffee.UIEffects public override void SetVerticesDirty() { base.SetVerticesDirty(); - for (var i = 0; i < replicas.Count; i++) + var count = replicas.Count; + for (var i = 0; i < count; i++) { if (!replicas[i]) continue; replicas[i].SetVerticesDirty(); @@ -1504,7 +1505,8 @@ namespace Coffee.UIEffects public override void SetMaterialDirty() { base.SetMaterialDirty(); - for (var i = 0; i < replicas.Count; i++) + var count = replicas.Count; + for (var i = 0; i < count; i++) { if (!replicas[i]) continue; replicas[i].SetMaterialDirty(); @@ -1579,7 +1581,8 @@ namespace Coffee.UIEffects { base.ApplyContextToMaterial(material); - for (var i = 0; i < replicas.Count; i++) + var count = replicas.Count; + for (var i = 0; i < count; i++) { if (!replicas[i]) continue; replicas[i].ApplyContextToMaterial(material); diff --git a/Packages/src/Runtime/UIEffectContext.cs b/Packages/src/Runtime/UIEffectContext.cs index 47c3f6b..bef0e05 100644 --- a/Packages/src/Runtime/UIEffectContext.cs +++ b/Packages/src/Runtime/UIEffectContext.cs @@ -8,7 +8,7 @@ using UnityEngine.UI; namespace Coffee.UIEffects { - public class UIEffectContext + public sealed class UIEffectContext { private static readonly UIEffectContext s_DefaultContext = new UIEffectContext(); private static readonly List s_WorkingVertices = new List(1024 * 8); @@ -602,16 +602,17 @@ namespace Coffee.UIEffects scale.y = 0 != (m_Flip & Flip.Vertical) ? -scale.y : scale.y; } - var pivot = new Vector2(0.5f, 0.5f); - var offset = (transitionRoot.pivot - pivot) * size; + var pivot = new Vector3(0.5f, 0.5f); + var offset = (transitionRoot.pivot - (Vector2)pivot) * size; var w2LMat = Matrix4x4.Translate(offset) * transitionRoot.worldToLocalMatrix; var rootRotation = Quaternion.Euler(0, 0, m_TransitionRotation); var rootScale = 1f / GetMultiplier(m_TransitionRotation); material.SetMatrix(s_RootViewMatrix, Matrix4x4.TRS(pivot, rootRotation, scale * rootScale) * w2LMat); - var gradRotation = Quaternion.Euler(0, 0, GetGradationRotation()); - var gradScale = 1f / GetMultiplier(GetGradationRotation()); + var rotation = GetGradationRotation(); + var gradRotation = Quaternion.Euler(0, 0, rotation); + var gradScale = 1f / GetMultiplier(rotation); material.SetMatrix(s_GradViewMatrix, Matrix4x4.TRS(pivot, gradRotation, scale * gradScale) * w2LMat); if (m_ShadowMode == ShadowMode.Mirror) @@ -620,26 +621,25 @@ namespace Coffee.UIEffects var mirrorOffset = new Vector3(0, size.y / 2 * (mirrorInv + 1) - m_ShadowDistance.y * mirrorInv, 0); var mirrorScale = new Vector3(1, mirrorInv, 1); var udScale = new Vector3(scale.x, -scale.y, scale.z); - material.SetMatrix(s_MirrorRootViewMatrix, - Matrix4x4.TRS(pivot, rootRotation, udScale * rootScale) - * Matrix4x4.TRS(mirrorOffset, Quaternion.identity, mirrorScale) * w2LMat); - material.SetMatrix(s_MirrorGradViewMatrix, - Matrix4x4.TRS(pivot, gradRotation, udScale * gradScale) - * Matrix4x4.TRS(mirrorOffset, Quaternion.identity, mirrorScale) * w2LMat); + var mirrorTranslation = Matrix4x4.TRS(mirrorOffset, Quaternion.identity, mirrorScale) * w2LMat; + material.SetMatrix( + s_MirrorRootViewMatrix, + Matrix4x4.TRS(pivot, rootRotation, udScale * rootScale) * mirrorTranslation + ); + material.SetMatrix( + s_MirrorGradViewMatrix, + Matrix4x4.TRS(pivot, gradRotation, udScale * gradScale) * mirrorTranslation + ); } - if (canvas.renderMode == RenderMode.WorldSpace) - { - material.SetMatrix(s_CanvasToWorldMatrix, Matrix4x4.identity); - } - else if (canvas.renderMode == RenderMode.ScreenSpaceOverlay || !canvas.worldCamera) - { - material.SetMatrix(s_CanvasToWorldMatrix, canvas.transform.localToWorldMatrix); - } - else - { - material.SetMatrix(s_CanvasToWorldMatrix, Matrix4x4.identity); - } + var useIdentity = + canvas.renderMode == RenderMode.WorldSpace || + (canvas.renderMode != RenderMode.ScreenSpaceOverlay && canvas.worldCamera); + + material.SetMatrix( + s_CanvasToWorldMatrix, + useIdentity ? Matrix4x4.identity : canvas.transform.localToWorldMatrix + ); } private Vector4 GetGradationScaleAndOffset() @@ -691,18 +691,22 @@ namespace Coffee.UIEffects return Mathf.Max(Mathf.Abs(cos - sin), Mathf.Abs(cos + sin)); } + #if UNITY_2021_2_OR_NEWER + private static void SetKeyword(Material material, Span keywords, int index) + #else private static void SetKeyword(Material material, string[] keywords, int index) + #endif { - for (var i = 0; i < keywords.Length; i++) + var length = keywords.Length; + if (index < length && !string.IsNullOrEmpty(keywords[index])) { - if (i != index) - { - material.DisableKeyword(keywords[i]); - } - else if (!string.IsNullOrEmpty(keywords[i])) - { - material.EnableKeyword(keywords[i]); - } + material.EnableKeyword(keywords[index]); + } + + for (var i = 0; i < length; i++) + { + if (i == index) continue; + material.DisableKeyword(keywords[i]); } } diff --git a/Packages/src/Runtime/UIEffectReplica.cs b/Packages/src/Runtime/UIEffectReplica.cs index 02add73..8027623 100644 --- a/Packages/src/Runtime/UIEffectReplica.cs +++ b/Packages/src/Runtime/UIEffectReplica.cs @@ -5,7 +5,7 @@ using UnityEngine; namespace Coffee.UIEffects { [Icon("Packages/com.coffee.ui-effect/Editor/UIEffectIconIcon.png")] - public class UIEffectReplica : UIEffectBase, ISerializationCallbackReceiver + public sealed class UIEffectReplica : UIEffectBase, ISerializationCallbackReceiver { [SerializeField] private UIEffect m_Target; [SerializeField] private UIEffectPreset m_Preset; @@ -124,19 +124,20 @@ namespace Coffee.UIEffects { get { - if (preset && useTargetTransform && !m_CustomRoot && graphic && canvas) + if (useTargetTransform) { - return canvas.transform as RectTransform; + if (preset && !m_CustomRoot && graphic && canvas) + { + return canvas.transform as RectTransform; + } + + if (isTargetInScene) + { + return target.transitionRoot; + } } - if (useTargetTransform && isTargetInScene) - { - return target.transitionRoot; - } - - return m_CustomRoot - ? m_CustomRoot - : transform as RectTransform; + return m_CustomRoot ? m_CustomRoot : transform as RectTransform; } } @@ -186,9 +187,9 @@ namespace Coffee.UIEffects target.SetEnablePreviewIfSelected(selection); } // Following the preset/preset-target. - else if (context != null) + else { - context.SetEnablePreview(selection.Contains(gameObject), effectMaterial); + context?.SetEnablePreview(selection.Contains(gameObject), effectMaterial); } } #endif diff --git a/Packages/src/Runtime/Utilities/GraphicProxy.cs b/Packages/src/Runtime/Utilities/GraphicProxy.cs index f94314e..cfb3e98 100644 --- a/Packages/src/Runtime/Utilities/GraphicProxy.cs +++ b/Packages/src/Runtime/Utilities/GraphicProxy.cs @@ -15,8 +15,10 @@ namespace Coffee.UIEffects public static void Register(GraphicProxy proxy) { // Register only once. - foreach (var p in s_Proxies) + var count = s_Proxies.Count; + for (var i = 0; i < count; i++) { + var p = s_Proxies[i]; if (p.GetType() == proxy.GetType()) return; } @@ -27,7 +29,8 @@ namespace Coffee.UIEffects { if (!graphic) return null; - for (var i = s_Proxies.Count - 1; i >= 0; i--) + var count = s_Proxies.Count; + for (var i = count - 1; i >= 0; i--) { var p = s_Proxies[i]; if (p.IsValid(graphic)) return p; diff --git a/Packages/src/Runtime/Utilities/ImageProxy.cs b/Packages/src/Runtime/Utilities/ImageProxy.cs index 0c938f4..51f323d 100644 --- a/Packages/src/Runtime/Utilities/ImageProxy.cs +++ b/Packages/src/Runtime/Utilities/ImageProxy.cs @@ -4,7 +4,7 @@ using UnityEngine.UI; namespace Coffee.UIEffects { - internal class ImageProxy : GraphicProxy + internal sealed class ImageProxy : GraphicProxy { #if UNITY_EDITOR [InitializeOnLoadMethod] diff --git a/Packages/src/Runtime/Utilities/TmpProxy.cs b/Packages/src/Runtime/Utilities/TmpProxy.cs index 4e629a6..914444c 100644 --- a/Packages/src/Runtime/Utilities/TmpProxy.cs +++ b/Packages/src/Runtime/Utilities/TmpProxy.cs @@ -10,7 +10,7 @@ using Object = UnityEngine.Object; namespace Coffee.UIEffects { - internal class TmpProxy : GraphicProxy + internal sealed class TmpProxy : GraphicProxy { /// /// Check if the graphic is valid for this proxy.