仿照NGUI封装 DoTween功能系列(七)-TweenRotation

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yuyingwin/article/details/80074640

using DG.Tweening;
using UnityEngine;

namespace UGUITweener
{
    [ExecuteInEditMode]
    public class TweenRotation : TweenerSetting
    {
        private Component component;
        public Vector3 from = Vector3.zero;
        public Vector3 to = Vector3.zero;

        public override void TweenPlay(bool value)
        {
            if (component)
            {
                Vector3 startValue = value ? from : to;
                Vector3 endvalue = value ? to : from;

                if (component.GetType() == typeof(RectTransform))
                {
                    ((RectTransform)component).eulerAngles = startValue;
                    tween = ((RectTransform)component).DOLocalRotate(endvalue, duration).OnComplete(unityEvent.Invoke);
                }
                else
                {
                    ((Transform)component).eulerAngles = startValue;
                    tween = ((Transform)component).DOLocalRotate(endvalue, duration).OnComplete(unityEvent.Invoke);
                }

                SetTweenData();
            }
        }

        public override void SetStartToCurrentValue()
        {
            base.SetStartToCurrentValue();

            if (!component)
            {
                component = (Component)GetComponent<RectTransform>() ? (Component)GetComponent<RectTransform>() : (Component)GetComponent<Transform>();
            }
        }

#if UNITY_EDITOR

        [ContextMenu("设置 From")]
        private void SetFrom()
        {
            if (component)
            {
                if (component.GetType() == typeof(RectTransform))
                {
                    from = ((RectTransform)component).eulerAngles;
                }
                else
                {
                    from = ((Transform)component).eulerAngles;
                }
            }
        }

        [ContextMenu("设置 Transform")]
        private void SetTransform()
        {
            if (component)
            {
                if (component.GetType() == typeof(RectTransform))
                {
                    ((RectTransform)component).eulerAngles = from;
                }
                else
                {
                    ((Transform)component).eulerAngles = from;
                }
            }
        }

#endif
    }
}

猜你喜欢

转载自blog.csdn.net/yuyingwin/article/details/80074640