From d41d1ad21f3b4ce38bb1fae870cf8621ed61a1e8 Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:53:12 +0900 Subject: [PATCH] fix: fix potential NPE in code --- .../Timeline/Base/UIEffectColorClip.cs | 2 +- .../Timeline/Base/UIEffectFloatClip.cs | 2 +- .../Timeline/Base/UIEffectTimelineUtils.cs | 8 +++- Packages/src/Runtime/UIEffect.cs | 23 +++++++-- Packages/src/Runtime/UIEffectBase.cs | 48 ++++++++++++------- Packages/src/Runtime/UIEffectContext.cs | 5 +- Packages/src/Runtime/UIEffectPreset.cs | 2 + Packages/src/Runtime/UIEffectTweener.cs | 18 +++---- .../src/Runtime/Utilities/GraphicProxy.cs | 2 + Packages/src/Runtime/Utilities/TmpProxy.cs | 19 ++++++-- 10 files changed, 88 insertions(+), 41 deletions(-) diff --git a/Packages/src/Runtime/Timeline/Base/UIEffectColorClip.cs b/Packages/src/Runtime/Timeline/Base/UIEffectColorClip.cs index 0ba703c..9a2c3ee 100644 --- a/Packages/src/Runtime/Timeline/Base/UIEffectColorClip.cs +++ b/Packages/src/Runtime/Timeline/Base/UIEffectColorClip.cs @@ -22,7 +22,7 @@ namespace Coffee.UIEffects.Timeline public Color Get(float time) { return m_Tween - ? Color.Lerp(m_From, m_Value, m_Curve.Evaluate(time)) + ? Color.Lerp(m_From, m_Value, m_Curve != null ? m_Curve.Evaluate(time) : time) : m_Value; } } diff --git a/Packages/src/Runtime/Timeline/Base/UIEffectFloatClip.cs b/Packages/src/Runtime/Timeline/Base/UIEffectFloatClip.cs index 5a88cd7..4d102ac 100644 --- a/Packages/src/Runtime/Timeline/Base/UIEffectFloatClip.cs +++ b/Packages/src/Runtime/Timeline/Base/UIEffectFloatClip.cs @@ -20,7 +20,7 @@ namespace Coffee.UIEffects.Timeline public float Get(float time) { return m_Tween - ? Mathf.Lerp(m_From, m_Value, m_Curve.Evaluate(time)) + ? Mathf.Lerp(m_From, m_Value, m_Curve != null ? m_Curve.Evaluate(time) : time) : m_Value; } } diff --git a/Packages/src/Runtime/Timeline/Base/UIEffectTimelineUtils.cs b/Packages/src/Runtime/Timeline/Base/UIEffectTimelineUtils.cs index 76ebfbe..8273673 100644 --- a/Packages/src/Runtime/Timeline/Base/UIEffectTimelineUtils.cs +++ b/Packages/src/Runtime/Timeline/Base/UIEffectTimelineUtils.cs @@ -88,7 +88,7 @@ namespace Coffee.UIEffects.Timeline public override Playable CreatePlayable(PlayableGraph graph, GameObject owner) { - var playable = ScriptPlayable.Create(graph, m_Data); + var playable = ScriptPlayable.Create(graph, m_Data ??= new T()); var behaviour = playable.GetBehaviour(); behaviour.clip = this; return playable; @@ -126,7 +126,11 @@ namespace Coffee.UIEffects.Timeline var playableInput = (ScriptPlayable)playable.GetInput(i); var input = playableInput.GetBehaviour(); - var normalizedTime = (float)(playableInput.GetTime() / input.clip.timelineClip.duration); + var clip = input != null ? input.clip : null; + var timelineClip = clip != null ? clip.timelineClip : null; + if (timelineClip == null || timelineClip.duration <= 0) return; + + var normalizedTime = (float)(playableInput.GetTime() / timelineClip.duration); value = Add(value, input.Get(normalizedTime), weight / totalWeight); totalWeight += weight; diff --git a/Packages/src/Runtime/UIEffect.cs b/Packages/src/Runtime/UIEffect.cs index af922af..cec8f69 100644 --- a/Packages/src/Runtime/UIEffect.cs +++ b/Packages/src/Runtime/UIEffect.cs @@ -1488,6 +1488,8 @@ namespace Coffee.UIEffects internal override void SetEnablePreviewIfSelected(GameObject[] selection) { + if (selection == null) return; + var selected = 0 < selection.Length && (selection.Contains(gameObject) || replicas.Any(x => x != null && selection.Contains(x.gameObject))); @@ -1539,6 +1541,7 @@ namespace Coffee.UIEffects /// public void SetGradientKeys(Gradient gradient) { + if (gradient == null) return; SetGradientKeys(gradient.colorKeys, gradient.alphaKeys, gradient.mode); } @@ -1558,6 +1561,7 @@ namespace Coffee.UIEffects /// public void SetTransitionGradientKeys(Gradient gradient) { + if (gradient == null) return; SetTransitionGradientKeys(gradient.colorKeys, gradient.alphaKeys, gradient.mode); } @@ -2021,9 +2025,13 @@ namespace Coffee.UIEffects dst.m_TransitionColorGlow = src.m_TransitionColorGlow; dst.m_TransitionPatternReverse = src.m_TransitionPatternReverse; dst.m_TransitionAutoPlaySpeed = src.m_TransitionAutoPlaySpeed; - dst.m_TransitionGradient.SetKeys(src.m_TransitionGradient.colorKeys, - src.m_TransitionGradient.alphaKeys); - dst.m_TransitionGradient.mode = src.m_TransitionGradient.mode; + if (src.m_TransitionGradient != null) + { + dst.m_TransitionGradient ??= new Gradient(); + dst.m_TransitionGradient.SetKeys(src.m_TransitionGradient.colorKeys, + src.m_TransitionGradient.alphaKeys); + dst.m_TransitionGradient.mode = src.m_TransitionGradient.mode; + } } if (!append || src.m_TargetMode != TargetMode.None) @@ -2076,8 +2084,13 @@ namespace Coffee.UIEffects dst.m_GradationColor2 = src.m_GradationColor2; dst.m_GradationColor3 = src.m_GradationColor3; dst.m_GradationColor4 = src.m_GradationColor4; - dst.m_GradationGradient.SetKeys(src.m_GradationGradient.colorKeys, src.m_GradationGradient.alphaKeys); - dst.m_GradationGradient.mode = src.m_GradationGradient.mode; + if (src.m_GradationGradient != null) + { + dst.m_GradationGradient ??= new Gradient(); + dst.m_GradationGradient.SetKeys(src.m_GradationGradient.colorKeys, + src.m_GradationGradient.alphaKeys); + dst.m_GradationGradient.mode = src.m_GradationGradient.mode; + } dst.m_GradationOffset = src.m_GradationOffset; dst.m_GradationScale = src.m_GradationScale; dst.m_GradationRotation = src.m_GradationRotation; diff --git a/Packages/src/Runtime/UIEffectBase.cs b/Packages/src/Runtime/UIEffectBase.cs index c3ceb2f..a34b75b 100644 --- a/Packages/src/Runtime/UIEffectBase.cs +++ b/Packages/src/Runtime/UIEffectBase.cs @@ -137,7 +137,10 @@ namespace Coffee.UIEffects private void OnAfterCanvasRebuild() { - if (_material == null || graphic == null || canvas == null || context == null) return; + var g = graphic; + var c = canvas; + var ctx = context; + if (_material == null || g == null || c == null || ctx == null) return; if (_isContextDirty) { @@ -145,9 +148,14 @@ namespace Coffee.UIEffects ApplyContextToMaterial(GetCurrentMaterial()); } - if (!context.useViewMatrix) return; + if (!ctx.useViewMatrix) return; - context.UpdateViewMatrix(GetCurrentMaterial(), transitionRoot, canvas.rootCanvas); + var material = GetCurrentMaterial(); + var root = transitionRoot; + var rootCanvas = c.rootCanvas; + if (material == null || root == null || rootCanvas == null) return; + + ctx.UpdateViewMatrix(material, root, rootCanvas); } protected override void OnCanvasHierarchyChanged() @@ -162,12 +170,17 @@ namespace Coffee.UIEffects public virtual void ModifyMesh(VertexHelper vh) { - if (!isActiveAndEnabled || context == null) return; + if (!isActiveAndEnabled || vh == null) return; + + var g = graphic; + var ctx = context; + var root = transitionRoot; + if (g == null || ctx == null || root == null) return; _canModifyMesh = CanModifyMesh(); if (_canModifyMesh) { - context.ModifyMesh(graphic, canvas, transitionRoot, vh, canModifyShape); + ctx.ModifyMesh(g, canvas, root, vh, canModifyShape); } } @@ -178,6 +191,7 @@ namespace Coffee.UIEffects // The transitionRoot is same as the transform => true. var root = transitionRoot; + if (root == null) return false; if (transform == root) return true; // The transitionRoot is not visible => false. @@ -251,22 +265,22 @@ namespace Coffee.UIEffects public virtual void SetVerticesDirty() { - if (graphic != null) - { - graphic.SetVerticesDirty(); - GraphicProxy.Find(graphic).SetVerticesDirty(graphic, enabled); - Misc.QueuePlayerLoopUpdate(); - } + var g = graphic; + if (g == null) return; + + g.SetVerticesDirty(); + GraphicProxy.Find(g)?.SetVerticesDirty(g, enabled); + Misc.QueuePlayerLoopUpdate(); } public virtual void SetMaterialDirty() { - if (graphic != null) - { - graphic.SetMaterialDirty(); - SetMaterialContextDirty(); - Misc.QueuePlayerLoopUpdate(); - } + var g = graphic; + if (g == null) return; + + g.SetMaterialDirty(); + SetMaterialContextDirty(); + Misc.QueuePlayerLoopUpdate(); } public virtual void SetMaterialContextDirty() diff --git a/Packages/src/Runtime/UIEffectContext.cs b/Packages/src/Runtime/UIEffectContext.cs index aa1fe4c..19740c9 100644 --- a/Packages/src/Runtime/UIEffectContext.cs +++ b/Packages/src/Runtime/UIEffectContext.cs @@ -589,7 +589,7 @@ namespace Coffee.UIEffects public void UpdateViewMatrix(Material material, RectTransform transitionRoot, Canvas canvas) { - if (material == null) return; + if (material == null || transitionRoot == null || canvas == null) return; var size = transitionRoot.rect.size; var scale = new Vector3(1f / size.x, 1f / size.y, 1f); @@ -715,6 +715,8 @@ namespace Coffee.UIEffects public void ModifyMesh(Graphic graphic, Canvas canvas, RectTransform transitionRoot, VertexHelper vh, bool canModifyShape) { + if (graphic == null || transitionRoot == null || vh == null) return; + // Apply flip (without effect). ApplyFlipWithoutEffect(vh, m_Flip); @@ -722,6 +724,7 @@ namespace Coffee.UIEffects if (!willModifyVertex || count == 0) return; var effectProxy = GraphicProxy.Find(graphic); + if (effectProxy == null) return; var isText = effectProxy.IsText(graphic); effectProxy.OnPreModifyMesh(graphic, canvas); diff --git a/Packages/src/Runtime/UIEffectPreset.cs b/Packages/src/Runtime/UIEffectPreset.cs index 5a8b004..6ba0e5e 100644 --- a/Packages/src/Runtime/UIEffectPreset.cs +++ b/Packages/src/Runtime/UIEffectPreset.cs @@ -121,6 +121,8 @@ namespace Coffee.UIEffects public void UpdateContext(UIEffectContext dst) { + if (dst == null) return; + var src = this; dst.m_ToneFilter = src.m_ToneFilter; dst.m_ToneIntensity = src.m_ToneIntensity; diff --git a/Packages/src/Runtime/UIEffectTweener.cs b/Packages/src/Runtime/UIEffectTweener.cs index 70a9fbd..2a19534 100644 --- a/Packages/src/Runtime/UIEffectTweener.cs +++ b/Packages/src/Runtime/UIEffectTweener.cs @@ -313,7 +313,7 @@ namespace Coffee.UIEffects /// public AnimationCurve curve { - get => m_Curve; + get => m_Curve ??= AnimationCurve.Linear(0, 0, 1, 1); set => m_Curve = value; } @@ -325,19 +325,19 @@ namespace Coffee.UIEffects public AnimationCurve reverseCurve { - get => m_ReverseCurve; + get => m_ReverseCurve ??= AnimationCurve.Linear(0, 0, 1, 1); set => m_ReverseCurve = value; } /// /// Event to invoke when the tween has completed. /// - public UnityEvent onComplete => m_OnComplete; + public UnityEvent onComplete => m_OnComplete ??= new UnityEvent(); /// /// Event to invoke when the rate was changed. /// - public TweenerEvent onChangedRate => m_OnChangedRate; + public TweenerEvent onChangedRate => m_OnChangedRate ??= new TweenerEvent(); /// /// Is the tween playing? @@ -421,7 +421,7 @@ namespace Coffee.UIEffects if (!isTweening) { - m_OnComplete.Invoke(); + onComplete.Invoke(); } } @@ -442,7 +442,7 @@ namespace Coffee.UIEffects if (!isTweening) { - m_OnComplete.Invoke(); + onComplete.Invoke(); } } @@ -463,7 +463,7 @@ namespace Coffee.UIEffects if (!isTweening) { - m_OnComplete.Invoke(); + onComplete.Invoke(); } } @@ -540,7 +540,7 @@ namespace Coffee.UIEffects if (prevTweening && !isTweening) { - m_OnComplete.Invoke(); + onComplete.Invoke(); } return; @@ -574,7 +574,7 @@ namespace Coffee.UIEffects if (prevTweening && !isTweening) { - m_OnComplete.Invoke(); + onComplete.Invoke(); } } } diff --git a/Packages/src/Runtime/Utilities/GraphicProxy.cs b/Packages/src/Runtime/Utilities/GraphicProxy.cs index d5f75f8..352c496 100644 --- a/Packages/src/Runtime/Utilities/GraphicProxy.cs +++ b/Packages/src/Runtime/Utilities/GraphicProxy.cs @@ -14,6 +14,8 @@ namespace Coffee.UIEffects public static void Register(GraphicProxy proxy) { + if (proxy == null) throw new ArgumentNullException(nameof(proxy)); + // Register only once. var count = s_Proxies.Count; for (var i = 0; i < count; i++) diff --git a/Packages/src/Runtime/Utilities/TmpProxy.cs b/Packages/src/Runtime/Utilities/TmpProxy.cs index 288eacd..9637a8f 100644 --- a/Packages/src/Runtime/Utilities/TmpProxy.cs +++ b/Packages/src/Runtime/Utilities/TmpProxy.cs @@ -213,6 +213,12 @@ namespace Coffee.UIEffects private static void ModifyMesh(TextMeshProUGUI textMeshProUGUI) { + if (!textMeshProUGUI || !textMeshProUGUI.isActiveAndEnabled) return; + if (textMeshProUGUI.textInfo == null || textMeshProUGUI.textInfo.meshInfo == null) return; + + var canvasRenderer = textMeshProUGUI.canvasRenderer; + if (canvasRenderer == null) return; + if (s_Mesh == null) { s_Mesh = new Mesh(); @@ -229,18 +235,18 @@ namespace Coffee.UIEffects for (var i = 0; i < textMeshProUGUI.textInfo.meshInfo.Length; i++) { var meshInfo = textMeshProUGUI.textInfo.meshInfo[i]; - if (meshInfo.vertexCount == 0) + if (meshInfo.vertexCount == 0 || meshInfo.mesh == null) { // Clear mesh if no vertices. s_Mesh.Clear(false); if (i == 0) { - textMeshProUGUI.canvasRenderer.SetMesh(s_Mesh); + canvasRenderer.SetMesh(s_Mesh); } else { var subMeshUI = GetSubMeshUI(subMeshes, meshInfo.material, meshInfo.mesh, i - 1); - if (subMeshUI != null) + if (subMeshUI != null && subMeshUI.canvasRenderer != null) { subMeshUI.canvasRenderer.SetMesh(s_Mesh); } @@ -259,7 +265,7 @@ namespace Coffee.UIEffects } s_VertexHelper.FillMesh(s_Mesh); - textMeshProUGUI.canvasRenderer.SetMesh(s_Mesh); + canvasRenderer.SetMesh(s_Mesh); } else if (i - 1 < subMeshes.Count) { @@ -290,7 +296,10 @@ namespace Coffee.UIEffects } s_VertexHelper.FillMesh(s_Mesh); - subMeshUI.canvasRenderer.SetMesh(s_Mesh); + if (subMeshUI.canvasRenderer != null) + { + subMeshUI.canvasRenderer.SetMesh(s_Mesh); + } } else {