Files
TEngine/Assets/TEngine/Runtime/Extension/UGUIExtension/MonoExtend/UIButtonScale.cs
ALEXTANG 3be06cac21 [+] UIExtension UIButtonScale
[+] UIExtension UIButtonScale
2023-05-11 19:52:53 +08:00

138 lines
3.2 KiB
C#

using DG.Tweening;
using UnityEngine;
using UnityEngine.EventSystems;
[DisallowMultipleComponent]
public class UIButtonScale : MonoBehaviour,
IPointerUpHandler,
IPointerDownHandler,
IPointerEnterHandler,
IPointerExitHandler
{
public GameObject tweenTarget;
public Vector3 pressedScale = new Vector3(0.95f, 0.95f, 0.95f);
public float duration = 0.1f;
//是否移除当前gameObject身上所有的LeanTween。一般如果当前gameObject身上还有其他的Tween,例如Tween.Move,不建议移除所有的Tween
public bool needRemoveAllTween = true;
private Vector3 _cacheScale;
private Tweener _tweener;
private bool _started = false;
private bool _pressed = false;
void Start()
{
Init();
}
void Init()
{
if (!_started)
{
_started = true;
if (tweenTarget == null) tweenTarget = gameObject;
_cacheScale = tweenTarget.transform.localScale;
}
}
public void OnDestroy()
{
if (_tweener != null)
{
DOTween.Kill(_tweener.id);
}
tweenTarget.transform.localScale = _cacheScale;
}
void OnEnable()
{
if (_started)
{
_pressed = false;
tweenTarget.transform.localScale = _cacheScale;
}
}
void OnDisable()
{
if (_started)
{
if (tweenTarget != null)
{
if (needRemoveAllTween == false && _tweener != null)
{
DOTween.Kill(_tweener.id);
}
tweenTarget.transform.localScale = _cacheScale;
}
}
}
void OnPress(bool isPressed)
{
if (!_started)
{
Init();
}
if (enabled)
{
if (needRemoveAllTween == false)
{
if (_tweener != null)
{
DOTween.Kill(_tweener.id);
}
}
if (isPressed)
{
Vector3 destScale = new Vector3(pressedScale.x * _cacheScale.x, pressedScale.y * _cacheScale.y, pressedScale.z * _cacheScale.z);
_tweener = tweenTarget.transform.DOScale(destScale, duration);
_tweener.SetUpdate(true);
}
else
{
_tweener = tweenTarget.transform.DOScale(_cacheScale, duration);
_tweener.SetUpdate(true);
}
}
}
public void OnPointerUp(PointerEventData eventData)
{
_pressed = false;
OnPress(_pressed);
}
public void OnPointerDown(PointerEventData eventData)
{
_pressed = true;
OnPress(_pressed);
}
public void OnPointerEnter(PointerEventData eventData)
{
OnPress(_pressed);
}
public void OnPointerExit(PointerEventData eventData)
{
OnPress(false);
}
public void SetDefaultScale(Vector3 scale)
{
if (!_started)
{
Init();
}
_cacheScale = scale;
if (needRemoveAllTween == false && _tweener != null)
{
DOTween.Kill(_tweener.id);
}
}
}