分享

动画缩放和移动

 印度阿三17 2020-12-26
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace XGUI
{
    public class XScaleTween : MonoBehaviour
    {
        public Ease m_ease = Ease.InOutBack;
        public Vector3 startValue = new Vector3(0.8f, 0.8f, 0.8f);
        public Vector3 endValue = new Vector3(1f, 1f, 1f);
        public float duration = 1.0f;
        public float delay = 0.0f;
        public int loops = 0;
        public LoopType loopType;
        private RectTransform rm_transform;
        public bool playAutomatically = false;
        private void Awake()
        {
            rm_transform = gameObject.GetComponent<RectTransform>();
        }

        public void SetPivot(float fx, float fy)
        {
            Vector2 sizeD = rm_transform.sizeDelta;

            rm_transform.pivot = new Vector2(sizeD.x / fx, sizeD.y / fy);
        }

        private void OnEnable()
        {
            if (playAutomatically) Play();
        }
        private void OnDisable()
        {
            Stop();
        }

        public void Play()
        {
            if (rm_transform == null) return;
            ResetEx();

            if (delay > 0)
                Invoke("DoPlay", this.delay);
            else
                DoPlay();

        }

        private void DoPlay()
        {

            rm_transform.localScale = startValue;
            rm_transform.DOScale(endValue, duration).SetLoops(loops, loopType);
        }

        public void ResetEx()
        {
            CancelInvoke();
            rm_transform.localScale = startValue;
            rm_transform.DOKill();
        }

        public void Stop()
        {
            rm_transform.localScale = endValue;
            rm_transform.DOKill();
        }
        private void OnDestroy()
        {
            rm_transform.DOKill();
        }
    }
}
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
namespace XGUI
{
    public class XSimpleMove : MonoBehaviour
    {

        public enum EaseType
        {
            dotweenEase = 1,
            animCurve = 2,
        }

        [SerializeField]
        private AnimationCurve animCurve = new AnimationCurve(new Keyframe[] { new Keyframe(0f, 0f), new Keyframe(3f, 40f) });

        public EaseType m_easeType = EaseType.dotweenEase;
        public Ease m_ease = Ease.InOutBack;
        public RectTransform startPos;
        public RectTransform endPos;
        public float duration = 1.0f;
        public float delay = 0.0f;
        private RectTransform m_transform;
        public bool playAutomatically = false;

        void Awake()
        {
            m_transform = gameObject.GetComponent<RectTransform>();
         
        }

        private void OnEnable()
        {
            if (playAutomatically) Play(null);
        }
        private void OnDisable()
        {
            Stop();
        }

        public void Play(TweenCallback onCompleFun)
        {
            if (m_transform == null || startPos == null) return;
            ResetEx();
            m_transform.anchoredPosition = startPos.anchoredPosition;

            if (delay > 0)
                StartCoroutine(DoWait(onCompleFun));
            else
                DoPlay(onCompleFun);
        }

        private IEnumerator DoWait(TweenCallback onCompleFun)
        {
            yield return new WaitForSeconds(delay);

            DoPlay(onCompleFun);
        }

        private void DoPlay(TweenCallback onCompleFun)
        {
            Tweener tweener = m_transform.DOAnchorPos(endPos.anchoredPosition, duration);
            if (m_easeType == EaseType.animCurve)
                tweener.SetEase(animCurve);
            else
                tweener.SetEase(m_ease);

            if (onCompleFun != null)
            {
                tweener.OnComplete(delegate () {
                    onCompleFun();
                });
            }
        }

        public void ResetEx()
        {
            if (startPos == null) return;
            m_transform.anchoredPosition = startPos.anchoredPosition;
            m_transform.DOKill();
        }

        public void Stop()
        {
            if(m_transform != null && endPos != null)
            {
                m_transform.anchoredPosition = endPos.anchoredPosition;
                m_transform.DOKill();
            }
        }

        private void OnDestroy()
        {
            m_transform.DOKill();
        }
    }

}
来源:https://www./content-4-799251.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约