From 3be06cac21a68595263e73bd30123525b4f52a19 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Thu, 11 May 2023 19:52:53 +0800 Subject: [PATCH] [+] UIExtension UIButtonScale [+] UIExtension UIButtonScale --- .../UGUIExtension/MonoExtend/UIButtonScale.cs | 137 ++++++++++++++++++ .../MonoExtend/UIButtonScale.cs.meta | 3 + 2 files changed, 140 insertions(+) create mode 100644 Assets/TEngine/Runtime/Extension/UGUIExtension/MonoExtend/UIButtonScale.cs create mode 100644 Assets/TEngine/Runtime/Extension/UGUIExtension/MonoExtend/UIButtonScale.cs.meta diff --git a/Assets/TEngine/Runtime/Extension/UGUIExtension/MonoExtend/UIButtonScale.cs b/Assets/TEngine/Runtime/Extension/UGUIExtension/MonoExtend/UIButtonScale.cs new file mode 100644 index 00000000..9126e7d3 --- /dev/null +++ b/Assets/TEngine/Runtime/Extension/UGUIExtension/MonoExtend/UIButtonScale.cs @@ -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); + } + } +} diff --git a/Assets/TEngine/Runtime/Extension/UGUIExtension/MonoExtend/UIButtonScale.cs.meta b/Assets/TEngine/Runtime/Extension/UGUIExtension/MonoExtend/UIButtonScale.cs.meta new file mode 100644 index 00000000..092fb0a7 --- /dev/null +++ b/Assets/TEngine/Runtime/Extension/UGUIExtension/MonoExtend/UIButtonScale.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 95e66cfdf24a4c9d949625a7b449f54a +timeCreated: 1683804898 \ No newline at end of file