fix: [improve performance] code optimization

This commit is contained in:
45375dfhg
2025-10-11 13:18:18 +02:00
committed by mob-sakai
parent 451cbe4302
commit e6fb78a0f6
8 changed files with 73 additions and 64 deletions
@@ -6,7 +6,7 @@ namespace Coffee.UIEffectInternal
/// <summary>
/// Object pool.
/// </summary>
internal class InternalObjectPool<T> where T : class
internal sealed class InternalObjectPool<T> where T : class
{
#if UNITY_2021_1_OR_NEWER
private readonly Predicate<T> _onValid; // Delegate for checking if instances are valid
@@ -17,7 +17,7 @@ using Object = UnityEngine.Object;
namespace Coffee.UIEffectInternal
{
[Serializable]
public class ShaderVariantRegistry
public sealed class ShaderVariantRegistry
{
[Serializable]
internal class StringPair : IEquatable<StringPair>
@@ -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.
+7 -4
View File
@@ -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);
+36 -32
View File
@@ -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<UIVertex> s_WorkingVertices = new List<UIVertex>(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<string> 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]);
}
}
+14 -13
View File
@@ -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
@@ -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;
+1 -1
View File
@@ -4,7 +4,7 @@ using UnityEngine.UI;
namespace Coffee.UIEffects
{
internal class ImageProxy : GraphicProxy
internal sealed class ImageProxy : GraphicProxy
{
#if UNITY_EDITOR
[InitializeOnLoadMethod]
+1 -1
View File
@@ -10,7 +10,7 @@ using Object = UnityEngine.Object;
namespace Coffee.UIEffects
{
internal class TmpProxy : GraphicProxy
internal sealed class TmpProxy : GraphicProxy
{
/// <summary>
/// Check if the graphic is valid for this proxy.