mirror of
https://github.com/mob-sakai/UIEffect.git
synced 2026-11-19 17:22:37 +08:00
fix: fix potential NPE in code
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ namespace Coffee.UIEffects.Timeline
|
||||
|
||||
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();
|
||||
behaviour.clip = this;
|
||||
return playable;
|
||||
@@ -126,7 +126,11 @@ namespace Coffee.UIEffects.Timeline
|
||||
|
||||
var playableInput = (ScriptPlayable<TBehavior>)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;
|
||||
|
||||
@@ -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
|
||||
/// </summary>
|
||||
public void SetGradientKeys(Gradient gradient)
|
||||
{
|
||||
if (gradient == null) return;
|
||||
SetGradientKeys(gradient.colorKeys, gradient.alphaKeys, gradient.mode);
|
||||
}
|
||||
|
||||
@@ -1558,6 +1561,7 @@ namespace Coffee.UIEffects
|
||||
/// </summary>
|
||||
public void SetTransitionGradientKeys(Gradient gradient)
|
||||
{
|
||||
if (gradient == null) return;
|
||||
SetTransitionGradientKeys(gradient.colorKeys, gradient.alphaKeys, gradient.mode);
|
||||
}
|
||||
|
||||
@@ -2021,10 +2025,14 @@ namespace Coffee.UIEffects
|
||||
dst.m_TransitionColorGlow = src.m_TransitionColorGlow;
|
||||
dst.m_TransitionPatternReverse = src.m_TransitionPatternReverse;
|
||||
dst.m_TransitionAutoPlaySpeed = src.m_TransitionAutoPlaySpeed;
|
||||
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);
|
||||
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;
|
||||
|
||||
@@ -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,23 +265,23 @@ namespace Coffee.UIEffects
|
||||
|
||||
public virtual void SetVerticesDirty()
|
||||
{
|
||||
if (graphic != null)
|
||||
{
|
||||
graphic.SetVerticesDirty();
|
||||
GraphicProxy.Find(graphic).SetVerticesDirty(graphic, enabled);
|
||||
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();
|
||||
var g = graphic;
|
||||
if (g == null) return;
|
||||
|
||||
g.SetMaterialDirty();
|
||||
SetMaterialContextDirty();
|
||||
Misc.QueuePlayerLoopUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void SetMaterialContextDirty()
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -313,7 +313,7 @@ namespace Coffee.UIEffects
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event to invoke when the tween has completed.
|
||||
/// </summary>
|
||||
public UnityEvent onComplete => m_OnComplete;
|
||||
public UnityEvent onComplete => m_OnComplete ??= new UnityEvent();
|
||||
|
||||
/// <summary>
|
||||
/// Event to invoke when the rate was changed.
|
||||
/// </summary>
|
||||
public TweenerEvent onChangedRate => m_OnChangedRate;
|
||||
public TweenerEvent onChangedRate => m_OnChangedRate ??= new TweenerEvent();
|
||||
|
||||
/// <summary>
|
||||
/// 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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++)
|
||||
|
||||
@@ -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,8 +296,11 @@ namespace Coffee.UIEffects
|
||||
}
|
||||
|
||||
s_VertexHelper.FillMesh(s_Mesh);
|
||||
if (subMeshUI.canvasRenderer != null)
|
||||
{
|
||||
subMeshUI.canvasRenderer.SetMesh(s_Mesh);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user