修复UIMotionTween导致的动态列表透明度异常
parent
3e71817bf1
commit
82a47850ea
|
|
@ -3122,7 +3122,8 @@ MonoBehaviour:
|
|||
m_PostInfinity: 2
|
||||
m_RotationOrder: 4
|
||||
excludeAlphaRoots:
|
||||
- {fileID: 4044137556368593417}
|
||||
- root: {fileID: 6214088998326444255}
|
||||
canvasGroupFade: 1
|
||||
--- !u!1 &3682885418409460412
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
|
|
|||
|
|
@ -1496,8 +1496,8 @@ MonoBehaviour:
|
|||
m_TargetGraphic: {fileID: 422689078839839864}
|
||||
m_HandleRect: {fileID: 1996466272081860649}
|
||||
m_Direction: 2
|
||||
m_Value: 0.9999253
|
||||
m_Size: 0.32036763
|
||||
m_Value: 0.99647784
|
||||
m_Size: 0.98556924
|
||||
m_NumberOfSteps: 0
|
||||
m_OnValueChanged:
|
||||
m_PersistentCalls:
|
||||
|
|
@ -4098,8 +4098,8 @@ MonoBehaviour:
|
|||
m_TargetGraphic: {fileID: 2990743840472524351}
|
||||
m_HandleRect: {fileID: 724326926352012884}
|
||||
m_Direction: 2
|
||||
m_Value: 0.99980855
|
||||
m_Size: 0.41623282
|
||||
m_Value: 0.9799981
|
||||
m_Size: 0.9944131
|
||||
m_NumberOfSteps: 0
|
||||
m_OnValueChanged:
|
||||
m_PersistentCalls:
|
||||
|
|
@ -4564,7 +4564,8 @@ MonoBehaviour:
|
|||
m_PostInfinity: 2
|
||||
m_RotationOrder: 4
|
||||
excludeAlphaRoots:
|
||||
- {fileID: 3948447934287971886}
|
||||
- root: {fileID: 3948447934287971886}
|
||||
canvasGroupFade: 1
|
||||
--- !u!1 &8573013733800426889
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
|
|
|||
|
|
@ -1022,7 +1022,7 @@ GameObject:
|
|||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
m_IsActive: 0
|
||||
--- !u!224 &1421009597845883921
|
||||
RectTransform:
|
||||
m_ObjectHideFlags: 0
|
||||
|
|
@ -6241,7 +6241,7 @@ RectTransform:
|
|||
m_Father: {fileID: 2652922614850330198}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
m_AnchorMin: {x: 0, y: 0}
|
||||
m_AnchorMax: {x: 0, y: 0}
|
||||
m_AnchorMax: {x: 1, y: 1}
|
||||
m_AnchoredPosition: {x: 0, y: 0.6719971}
|
||||
m_SizeDelta: {x: 0, y: -1.3379974}
|
||||
m_Pivot: {x: 0.5, y: 0.5}
|
||||
|
|
@ -8139,7 +8139,8 @@ MonoBehaviour:
|
|||
m_PostInfinity: 2
|
||||
m_RotationOrder: 4
|
||||
excludeAlphaRoots:
|
||||
- {fileID: 4459553860173251564}
|
||||
- root: {fileID: 8139542802305275716}
|
||||
canvasGroupFade: 1
|
||||
--- !u!1 &9000638637982704173
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
// - 渐变:另一条 AnimationCurve 驱动当前 GameObject 下所有 Graphic 的 alpha
|
||||
// 两条动画可独立启用,共用同一个时长和播放控制。
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
|
@ -12,6 +13,15 @@ namespace NLD.UI
|
|||
{
|
||||
public class UIMotionTween : MonoBehaviour
|
||||
{
|
||||
[Serializable]
|
||||
public class ExcludeAlphaRootEntry
|
||||
{
|
||||
public GameObject root;
|
||||
|
||||
[Tooltip("勾选后该根节点通过 CanvasGroup.alpha 参与渐变,其子级 Graphic 不走逐组件渐变")]
|
||||
public bool canvasGroupFade;
|
||||
}
|
||||
|
||||
// ── 通用设置 ──────────────────────────────────────────────────
|
||||
[Header("通用")]
|
||||
[Tooltip("动画总时长(秒)")]
|
||||
|
|
@ -48,9 +58,9 @@ namespace NLD.UI
|
|||
public AnimationCurve fadeCurve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
|
||||
|
||||
[Tooltip("不参与透明度渐变的根节点:自身及子物体上的 Graphic 均不随动画改 alpha,\n" +
|
||||
"也不参与「初始透明」与 OnDisable 时的清零。")]
|
||||
"也不参与「初始透明」与 OnDisable 时的清零;勾选 canvasGroupFade 的项改由 CanvasGroup 渐变。")]
|
||||
[SerializeField]
|
||||
private List<GameObject> excludeAlphaRoots = new List<GameObject>();
|
||||
private List<ExcludeAlphaRootEntry> excludeAlphaRoots = new List<ExcludeAlphaRootEntry>();
|
||||
|
||||
// ── 内部状态 ──────────────────────────────────────────────────
|
||||
public bool IsPlaying => _isPlaying;
|
||||
|
|
@ -65,6 +75,9 @@ namespace NLD.UI
|
|||
private readonly List<float> _originalAlphas = new List<float>();
|
||||
private readonly List<Graphic> _graphicsScanBuffer = new List<Graphic>();
|
||||
|
||||
private readonly List<CanvasGroup> _excludeCanvasGroups = new List<CanvasGroup>();
|
||||
private readonly List<float> _excludeCanvasGroupAlphas = new List<float>();
|
||||
|
||||
// ── 生命周期 ──────────────────────────────────────────────────
|
||||
private void Awake()
|
||||
{
|
||||
|
|
@ -175,6 +188,8 @@ namespace NLD.UI
|
|||
{
|
||||
_graphics.Clear();
|
||||
_originalAlphas.Clear();
|
||||
_excludeCanvasGroups.Clear();
|
||||
_excludeCanvasGroupAlphas.Clear();
|
||||
_graphicsScanBuffer.Clear();
|
||||
GetComponentsInChildren(true, _graphicsScanBuffer);
|
||||
foreach (var g in _graphicsScanBuffer)
|
||||
|
|
@ -184,6 +199,20 @@ namespace NLD.UI
|
|||
_graphics.Add(g);
|
||||
_originalAlphas.Add(g.color.a);
|
||||
}
|
||||
|
||||
if (excludeAlphaRoots == null) return;
|
||||
for (int i = 0; i < excludeAlphaRoots.Count; i++)
|
||||
{
|
||||
var entry = excludeAlphaRoots[i];
|
||||
if (entry == null || !entry.canvasGroupFade || entry.root == null) continue;
|
||||
|
||||
var cg = entry.root.GetComponent<CanvasGroup>();
|
||||
if (cg == null)
|
||||
cg = entry.root.AddComponent<CanvasGroup>();
|
||||
|
||||
_excludeCanvasGroups.Add(cg);
|
||||
_excludeCanvasGroupAlphas.Add(cg.alpha);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -193,10 +222,11 @@ namespace NLD.UI
|
|||
{
|
||||
if (excludeAlphaRoots == null || excludeAlphaRoots.Count == 0) return false;
|
||||
var t = graphic.transform;
|
||||
foreach (var root in excludeAlphaRoots)
|
||||
for (int i = 0; i < excludeAlphaRoots.Count; i++)
|
||||
{
|
||||
if (root == null) continue;
|
||||
var r = root.transform;
|
||||
var entry = excludeAlphaRoots[i];
|
||||
if (entry == null || entry.root == null) continue;
|
||||
var r = entry.root.transform;
|
||||
if (t == r || t.IsChildOf(r)) return true;
|
||||
}
|
||||
return false;
|
||||
|
|
@ -224,6 +254,13 @@ namespace NLD.UI
|
|||
c.a = progress * _originalAlphas[i];
|
||||
graphic.color = c;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _excludeCanvasGroups.Count; i++)
|
||||
{
|
||||
var cg = _excludeCanvasGroups[i];
|
||||
if (cg == null) continue;
|
||||
cg.alpha = progress * _excludeCanvasGroupAlphas[i];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>将所有 Graphic 设置为完全透明。</summary>
|
||||
|
|
@ -237,6 +274,13 @@ namespace NLD.UI
|
|||
c.a = 0f;
|
||||
graphic.color = c;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _excludeCanvasGroups.Count; i++)
|
||||
{
|
||||
var cg = _excludeCanvasGroups[i];
|
||||
if (cg == null) continue;
|
||||
cg.alpha = 0f;
|
||||
}
|
||||
}
|
||||
|
||||
// 判断该 Graphic 是否应跳过渐变:
|
||||
|
|
|
|||
Loading…
Reference in New Issue