107 lines
3.4 KiB
C#
107 lines
3.4 KiB
C#
using DG.Tweening;
|
||
using UnityEngine;
|
||
|
||
namespace Continentis.MainGame.Card
|
||
{
|
||
public abstract partial class CardViewBase
|
||
{
|
||
private Tweener hintShadowTweener;
|
||
private Tweener selectShadowTweener;
|
||
|
||
public void ClearShadows()
|
||
{
|
||
hintShadowTweener?.Kill(true);
|
||
selectShadowTweener?.Kill(true);
|
||
hintShadow.gameObject.SetActive(false);
|
||
selectShadow.gameObject.SetActive(false);
|
||
}
|
||
|
||
public void TryDisableAllShadows(bool immediately = false)
|
||
{
|
||
DisableHintShadow(immediately);
|
||
DisableSelectShadow(immediately);
|
||
}
|
||
|
||
public void EnableHintShadow(Color shadowColor)
|
||
{
|
||
hintShadow.gameObject.SetActive(true);
|
||
hintShadow.color = Color.clear;
|
||
hintShadowTweener = hintShadow.DOColor(shadowColor, 0.2f).Play();
|
||
}
|
||
|
||
public void DisableHintShadow(bool immediately = false)
|
||
{
|
||
if (immediately)
|
||
{
|
||
hintShadowTweener?.Kill(true);
|
||
hintShadow.gameObject.SetActive(false);
|
||
}
|
||
else
|
||
{
|
||
hintShadowTweener = hintShadow.DOColor(Color.clear, 0.2f)
|
||
.OnComplete(() => { hintShadow.gameObject.SetActive(false); }).Play();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据颜色智能更新提示阴影:null 关闭,非 null 启用对应颜色。
|
||
/// 避免相同颜色重复 tween 导致闪烁。
|
||
/// </summary>
|
||
public void UpdateHintShadow(Color? color)
|
||
{
|
||
if (color == null)
|
||
{
|
||
if (hintShadow.gameObject.activeSelf)
|
||
{
|
||
DisableHintShadow();
|
||
}
|
||
return;
|
||
}
|
||
|
||
Color targetColor = color.Value;
|
||
|
||
if (hintShadow.gameObject.activeSelf)
|
||
{
|
||
// 已启用:仅在颜色差异足够大时 tween,避免每帧闪烁
|
||
if (!ApproximatelyEqualColor(hintShadow.color, targetColor))
|
||
{
|
||
hintShadowTweener?.Kill();
|
||
hintShadowTweener = hintShadow.DOColor(targetColor, 0.2f).Play();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
EnableHintShadow(targetColor);
|
||
}
|
||
}
|
||
|
||
private static bool ApproximatelyEqualColor(Color a, Color b, float tolerance = 0.01f)
|
||
{
|
||
return Mathf.Abs(a.r - b.r) < tolerance
|
||
&& Mathf.Abs(a.g - b.g) < tolerance
|
||
&& Mathf.Abs(a.b - b.b) < tolerance
|
||
&& Mathf.Abs(a.a - b.a) < tolerance;
|
||
}
|
||
|
||
public void EnableSelectShadow()
|
||
{
|
||
selectShadow.gameObject.SetActive(true);
|
||
selectShadow.color = Color.clear;
|
||
selectShadowTweener = selectShadow.DOColor(new Color(0.33f, 0.66f, 1f), 0.2f).Play();
|
||
}
|
||
|
||
public void DisableSelectShadow(bool immediately = false)
|
||
{
|
||
if (immediately)
|
||
{
|
||
selectShadowTweener?.Kill(true);
|
||
selectShadow.gameObject.SetActive(false);
|
||
}
|
||
else
|
||
{
|
||
selectShadowTweener = selectShadow.DOColor(Color.clear, 0.2f)
|
||
.OnComplete(() => { selectShadow.gameObject.SetActive(false); }).Play();
|
||
}
|
||
}
|
||
}
|
||
} |