fix: fix potential NPE in code

This commit is contained in:
mob-sakai
2026-11-15 13:53:12 +09:00
parent 739ae50549
commit d41d1ad21f
10 changed files with 88 additions and 41 deletions
@@ -22,7 +22,7 @@ namespace Coffee.UIEffects.Timeline
public Color Get(float time) public Color Get(float time)
{ {
return m_Tween 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; : m_Value;
} }
} }
@@ -20,7 +20,7 @@ namespace Coffee.UIEffects.Timeline
public float Get(float time) public float Get(float time)
{ {
return m_Tween 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; : m_Value;
} }
} }
@@ -88,7 +88,7 @@ namespace Coffee.UIEffects.Timeline
public override Playable CreatePlayable(PlayableGraph graph, GameObject owner) public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
{ {
var playable = ScriptPlayable<T>.Create(graph, m_Data); var playable = ScriptPlayable<T>.Create(graph, m_Data ??= new T());
var behaviour = playable.GetBehaviour(); var behaviour = playable.GetBehaviour();
behaviour.clip = this; behaviour.clip = this;
return playable; return playable;
@@ -126,7 +126,11 @@ namespace Coffee.UIEffects.Timeline
var playableInput = (ScriptPlayable<TBehavior>)playable.GetInput(i); var playableInput = (ScriptPlayable<TBehavior>)playable.GetInput(i);
var input = playableInput.GetBehaviour(); 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); value = Add(value, input.Get(normalizedTime), weight / totalWeight);
totalWeight += weight; totalWeight += weight;
+18 -5
View File
@@ -1488,6 +1488,8 @@ namespace Coffee.UIEffects
internal override void SetEnablePreviewIfSelected(GameObject[] selection) internal override void SetEnablePreviewIfSelected(GameObject[] selection)
{ {
if (selection == null) return;
var selected = 0 < selection.Length var selected = 0 < selection.Length
&& (selection.Contains(gameObject) && (selection.Contains(gameObject)
|| replicas.Any(x => x != null && selection.Contains(x.gameObject))); || replicas.Any(x => x != null && selection.Contains(x.gameObject)));
@@ -1539,6 +1541,7 @@ namespace Coffee.UIEffects
/// </summary> /// </summary>
public void SetGradientKeys(Gradient gradient) public void SetGradientKeys(Gradient gradient)
{ {
if (gradient == null) return;
SetGradientKeys(gradient.colorKeys, gradient.alphaKeys, gradient.mode); SetGradientKeys(gradient.colorKeys, gradient.alphaKeys, gradient.mode);
} }
@@ -1558,6 +1561,7 @@ namespace Coffee.UIEffects
/// </summary> /// </summary>
public void SetTransitionGradientKeys(Gradient gradient) public void SetTransitionGradientKeys(Gradient gradient)
{ {
if (gradient == null) return;
SetTransitionGradientKeys(gradient.colorKeys, gradient.alphaKeys, gradient.mode); SetTransitionGradientKeys(gradient.colorKeys, gradient.alphaKeys, gradient.mode);
} }
@@ -2021,9 +2025,13 @@ namespace Coffee.UIEffects
dst.m_TransitionColorGlow = src.m_TransitionColorGlow; dst.m_TransitionColorGlow = src.m_TransitionColorGlow;
dst.m_TransitionPatternReverse = src.m_TransitionPatternReverse; dst.m_TransitionPatternReverse = src.m_TransitionPatternReverse;
dst.m_TransitionAutoPlaySpeed = src.m_TransitionAutoPlaySpeed; dst.m_TransitionAutoPlaySpeed = src.m_TransitionAutoPlaySpeed;
dst.m_TransitionGradient.SetKeys(src.m_TransitionGradient.colorKeys, if (src.m_TransitionGradient != null)
src.m_TransitionGradient.alphaKeys); {
dst.m_TransitionGradient.mode = src.m_TransitionGradient.mode; 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) if (!append || src.m_TargetMode != TargetMode.None)
@@ -2076,8 +2084,13 @@ namespace Coffee.UIEffects
dst.m_GradationColor2 = src.m_GradationColor2; dst.m_GradationColor2 = src.m_GradationColor2;
dst.m_GradationColor3 = src.m_GradationColor3; dst.m_GradationColor3 = src.m_GradationColor3;
dst.m_GradationColor4 = src.m_GradationColor4; dst.m_GradationColor4 = src.m_GradationColor4;
dst.m_GradationGradient.SetKeys(src.m_GradationGradient.colorKeys, src.m_GradationGradient.alphaKeys); if (src.m_GradationGradient != null)
dst.m_GradationGradient.mode = src.m_GradationGradient.mode; {
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_GradationOffset = src.m_GradationOffset;
dst.m_GradationScale = src.m_GradationScale; dst.m_GradationScale = src.m_GradationScale;
dst.m_GradationRotation = src.m_GradationRotation; dst.m_GradationRotation = src.m_GradationRotation;
+31 -17
View File
@@ -137,7 +137,10 @@ namespace Coffee.UIEffects
private void OnAfterCanvasRebuild() 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) if (_isContextDirty)
{ {
@@ -145,9 +148,14 @@ namespace Coffee.UIEffects
ApplyContextToMaterial(GetCurrentMaterial()); 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() protected override void OnCanvasHierarchyChanged()
@@ -162,12 +170,17 @@ namespace Coffee.UIEffects
public virtual void ModifyMesh(VertexHelper vh) 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(); _canModifyMesh = CanModifyMesh();
if (_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. // The transitionRoot is same as the transform => true.
var root = transitionRoot; var root = transitionRoot;
if (root == null) return false;
if (transform == root) return true; if (transform == root) return true;
// The transitionRoot is not visible => false. // The transitionRoot is not visible => false.
@@ -251,22 +265,22 @@ namespace Coffee.UIEffects
public virtual void SetVerticesDirty() public virtual void SetVerticesDirty()
{ {
if (graphic != null) var g = graphic;
{ if (g == null) return;
graphic.SetVerticesDirty();
GraphicProxy.Find(graphic).SetVerticesDirty(graphic, enabled); g.SetVerticesDirty();
Misc.QueuePlayerLoopUpdate(); GraphicProxy.Find(g)?.SetVerticesDirty(g, enabled);
} Misc.QueuePlayerLoopUpdate();
} }
public virtual void SetMaterialDirty() public virtual void SetMaterialDirty()
{ {
if (graphic != null) var g = graphic;
{ if (g == null) return;
graphic.SetMaterialDirty();
SetMaterialContextDirty(); g.SetMaterialDirty();
Misc.QueuePlayerLoopUpdate(); SetMaterialContextDirty();
} Misc.QueuePlayerLoopUpdate();
} }
public virtual void SetMaterialContextDirty() public virtual void SetMaterialContextDirty()
+4 -1
View File
@@ -589,7 +589,7 @@ namespace Coffee.UIEffects
public void UpdateViewMatrix(Material material, RectTransform transitionRoot, Canvas canvas) 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 size = transitionRoot.rect.size;
var scale = new Vector3(1f / size.x, 1f / size.y, 1f); 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, public void ModifyMesh(Graphic graphic, Canvas canvas, RectTransform transitionRoot, VertexHelper vh,
bool canModifyShape) bool canModifyShape)
{ {
if (graphic == null || transitionRoot == null || vh == null) return;
// Apply flip (without effect). // Apply flip (without effect).
ApplyFlipWithoutEffect(vh, m_Flip); ApplyFlipWithoutEffect(vh, m_Flip);
@@ -722,6 +724,7 @@ namespace Coffee.UIEffects
if (!willModifyVertex || count == 0) return; if (!willModifyVertex || count == 0) return;
var effectProxy = GraphicProxy.Find(graphic); var effectProxy = GraphicProxy.Find(graphic);
if (effectProxy == null) return;
var isText = effectProxy.IsText(graphic); var isText = effectProxy.IsText(graphic);
effectProxy.OnPreModifyMesh(graphic, canvas); effectProxy.OnPreModifyMesh(graphic, canvas);
+2
View File
@@ -121,6 +121,8 @@ namespace Coffee.UIEffects
public void UpdateContext(UIEffectContext dst) public void UpdateContext(UIEffectContext dst)
{ {
if (dst == null) return;
var src = this; var src = this;
dst.m_ToneFilter = src.m_ToneFilter; dst.m_ToneFilter = src.m_ToneFilter;
dst.m_ToneIntensity = src.m_ToneIntensity; dst.m_ToneIntensity = src.m_ToneIntensity;
+9 -9
View File
@@ -313,7 +313,7 @@ namespace Coffee.UIEffects
/// </summary> /// </summary>
public AnimationCurve curve public AnimationCurve curve
{ {
get => m_Curve; get => m_Curve ??= AnimationCurve.Linear(0, 0, 1, 1);
set => m_Curve = value; set => m_Curve = value;
} }
@@ -325,19 +325,19 @@ namespace Coffee.UIEffects
public AnimationCurve reverseCurve public AnimationCurve reverseCurve
{ {
get => m_ReverseCurve; get => m_ReverseCurve ??= AnimationCurve.Linear(0, 0, 1, 1);
set => m_ReverseCurve = value; set => m_ReverseCurve = value;
} }
/// <summary> /// <summary>
/// Event to invoke when the tween has completed. /// Event to invoke when the tween has completed.
/// </summary> /// </summary>
public UnityEvent onComplete => m_OnComplete; public UnityEvent onComplete => m_OnComplete ??= new UnityEvent();
/// <summary> /// <summary>
/// Event to invoke when the rate was changed. /// Event to invoke when the rate was changed.
/// </summary> /// </summary>
public TweenerEvent onChangedRate => m_OnChangedRate; public TweenerEvent onChangedRate => m_OnChangedRate ??= new TweenerEvent();
/// <summary> /// <summary>
/// Is the tween playing? /// Is the tween playing?
@@ -421,7 +421,7 @@ namespace Coffee.UIEffects
if (!isTweening) if (!isTweening)
{ {
m_OnComplete.Invoke(); onComplete.Invoke();
} }
} }
@@ -442,7 +442,7 @@ namespace Coffee.UIEffects
if (!isTweening) if (!isTweening)
{ {
m_OnComplete.Invoke(); onComplete.Invoke();
} }
} }
@@ -463,7 +463,7 @@ namespace Coffee.UIEffects
if (!isTweening) if (!isTweening)
{ {
m_OnComplete.Invoke(); onComplete.Invoke();
} }
} }
@@ -540,7 +540,7 @@ namespace Coffee.UIEffects
if (prevTweening && !isTweening) if (prevTweening && !isTweening)
{ {
m_OnComplete.Invoke(); onComplete.Invoke();
} }
return; return;
@@ -574,7 +574,7 @@ namespace Coffee.UIEffects
if (prevTweening && !isTweening) if (prevTweening && !isTweening)
{ {
m_OnComplete.Invoke(); onComplete.Invoke();
} }
} }
} }
@@ -14,6 +14,8 @@ namespace Coffee.UIEffects
public static void Register(GraphicProxy proxy) public static void Register(GraphicProxy proxy)
{ {
if (proxy == null) throw new ArgumentNullException(nameof(proxy));
// Register only once. // Register only once.
var count = s_Proxies.Count; var count = s_Proxies.Count;
for (var i = 0; i < count; i++) for (var i = 0; i < count; i++)
+14 -5
View File
@@ -213,6 +213,12 @@ namespace Coffee.UIEffects
private static void ModifyMesh(TextMeshProUGUI textMeshProUGUI) 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) if (s_Mesh == null)
{ {
s_Mesh = new Mesh(); s_Mesh = new Mesh();
@@ -229,18 +235,18 @@ namespace Coffee.UIEffects
for (var i = 0; i < textMeshProUGUI.textInfo.meshInfo.Length; i++) for (var i = 0; i < textMeshProUGUI.textInfo.meshInfo.Length; i++)
{ {
var meshInfo = textMeshProUGUI.textInfo.meshInfo[i]; var meshInfo = textMeshProUGUI.textInfo.meshInfo[i];
if (meshInfo.vertexCount == 0) if (meshInfo.vertexCount == 0 || meshInfo.mesh == null)
{ {
// Clear mesh if no vertices. // Clear mesh if no vertices.
s_Mesh.Clear(false); s_Mesh.Clear(false);
if (i == 0) if (i == 0)
{ {
textMeshProUGUI.canvasRenderer.SetMesh(s_Mesh); canvasRenderer.SetMesh(s_Mesh);
} }
else else
{ {
var subMeshUI = GetSubMeshUI(subMeshes, meshInfo.material, meshInfo.mesh, i - 1); var subMeshUI = GetSubMeshUI(subMeshes, meshInfo.material, meshInfo.mesh, i - 1);
if (subMeshUI != null) if (subMeshUI != null && subMeshUI.canvasRenderer != null)
{ {
subMeshUI.canvasRenderer.SetMesh(s_Mesh); subMeshUI.canvasRenderer.SetMesh(s_Mesh);
} }
@@ -259,7 +265,7 @@ namespace Coffee.UIEffects
} }
s_VertexHelper.FillMesh(s_Mesh); s_VertexHelper.FillMesh(s_Mesh);
textMeshProUGUI.canvasRenderer.SetMesh(s_Mesh); canvasRenderer.SetMesh(s_Mesh);
} }
else if (i - 1 < subMeshes.Count) else if (i - 1 < subMeshes.Count)
{ {
@@ -290,7 +296,10 @@ namespace Coffee.UIEffects
} }
s_VertexHelper.FillMesh(s_Mesh); s_VertexHelper.FillMesh(s_Mesh);
subMeshUI.canvasRenderer.SetMesh(s_Mesh); if (subMeshUI.canvasRenderer != null)
{
subMeshUI.canvasRenderer.SetMesh(s_Mesh);
}
} }
else else
{ {