mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
[+] UIExtension UIButtonScale
[+] UIExtension UIButtonScale
This commit is contained in:
@@ -0,0 +1,137 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 95e66cfdf24a4c9d949625a7b449f54a
|
||||||
|
timeCreated: 1683804898
|
Reference in New Issue
Block a user