UGUI-UITweenPosition

作者:追风剑情 发布于:2019-6-4 20:02 分类:Unity3d

示例:


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
/// <summary>
/// UI位置缓动
/// </summary>
public class UITweenPosition : MonoBehaviour
{
    public bool active = false;
    [Tooltip("延迟执行")]
    public float delayTime = 0;
    public RectTransform rectTransform;
    public Vector3 from;
    public Vector3 to;
    public float speed;
    public bool inverse = false;
    public bool constraintX = false; //约束X坐标
    public bool constraintY = false; //约束Y坐标

    [Serializable]
    // 定义缓动完成后要触发的事件
    public class TweenCompletedEvent : UnityEvent { }

    // 防止序列化变量重命名后丢失引用
    [FormerlySerializedAs("onCompleted")]
    [SerializeField]
    private TweenCompletedEvent m_OnCompleted = new TweenCompletedEvent();

    private float t = 0;
    private float escapeTime = 0;

    void Awake()
    {
        if (rectTransform == null)
            rectTransform = this.GetComponent<RectTransform>();

        if (constraintX)
            from.x = to.x = rectTransform.anchoredPosition.x;
        if (constraintY)
            from.y = to.y = rectTransform.anchoredPosition.y;
    }

    void Update()
    {
        if (!active)
            return;

        escapeTime += Time.deltaTime;
        if (escapeTime < delayTime)
            return;

        t += speed * Time.deltaTime;
        if (inverse)
            rectTransform.anchoredPosition = Vector3.Lerp(to, from, t);
        else
            rectTransform.anchoredPosition = Vector3.Lerp(from, to, t);

        if (t >= 1)
        {
            t = 0;
            active = false;
            m_OnCompleted.Invoke();
        }
    }

    public void Reset()
    {
        rectTransform.anchoredPosition = from;
    }

    public void Play()
    {
        inverse = false;
        t = 0;
        escapeTime = 0;
        active = true;
    }

    public void InversePlay()
    {
        inverse = true;
        t = 0;
        escapeTime = 0;
        active = true;
    }

    // 是否停靠在to指定的坐标
    public bool AtTheTo()
    {
        Vector2 pos = rectTransform.anchoredPosition;
        return Mathf.Approximately(pos.x, to.x) && Mathf.Approximately(pos.y, to.y);
    }
}


截图

1111.png

示例:对物体缓动


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;

/// <summary>
/// 物体位置缓动
/// </summary>
public class TweenPosition : MonoBehaviour
{
    public Transform target;
    public bool active = false;
    public Vector3 from;
    public Vector3 to;
    public float speed;
    public bool inverse = false;
    public bool useLocal = true; //是否采用本地坐标

    [Serializable]
    // 定义缓动完成后要触发的事件
    public class TweenCompletedEvent : UnityEvent { }

    // 防止序列化变量重命名后丢失引用
    [FormerlySerializedAs("onCompleted")]
    [SerializeField]
    private TweenCompletedEvent m_OnCompleted = new TweenCompletedEvent();

    private float t = 0;

    private void Awake()
    {
        if (target == null)
            target = transform;
    }

    void Update()
    {
        if (!active)
            return;
        if (target == null)
            return;

        t += speed * Time.deltaTime;
        if (inverse)
        {
            if (useLocal)
                target.localPosition = Vector3.Lerp(to, from, t);
            else
                target.position = Vector3.Lerp(to, from, t);
        }
        else
        {
            if (useLocal)
                target.localPosition = Vector3.Lerp(from, to, t);
            else
                target.position = Vector3.Lerp(from, to, t);
        }

        if (t >= 1)
        {
            t = 0;
            active = false;
            m_OnCompleted.Invoke();
        }
    }
}


标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号