UGUI-UITweenRotation

作者:追风剑情 发布于:2019-6-5 9:35 分类: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 UITweenRotation : MonoBehaviour
{
    public bool active = false;
    public RectTransform rectTransform;
    [Tooltip("旋转速度")]
    public float speed = 1f;
    [Tooltip("持续时间")]
    public float durationTime = -1;
    [Tooltip("反向旋转")]
    public bool reverse = false;

    [Serializable]
    // 定义缓动完成后要触发的事件
    public class TweenCompletedEvent : UnityEvent { }
    // 防止序列化变量重命名后丢失引用
    [FormerlySerializedAs("onCompleted")]
    [SerializeField]
    private TweenCompletedEvent m_OnCompleted = new TweenCompletedEvent();

    private float t = 0f;

    void Awake ()
    {
        if (rectTransform == null)
            rectTransform = this.GetComponent<RectTransform>();
    }
	
	void Update ()
    {
        if (!active)
            return;
        float angle = speed * Time.deltaTime;
        if (reverse)
            angle = -angle;
        rectTransform.Rotate(Vector3.forward, angle);
        t += Time.deltaTime;

        if (durationTime <= 0)
            return;
        if (t >= durationTime)
        {
            t = 0f;
            active = false;
            m_OnCompleted.Invoke();
        }
	}

    public TweenCompletedEvent OnCompleted
    {
        get
        {
            return m_OnCompleted;
        }
    }
}


截图

1111.png

示例:对物体旋转

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 物体旋转缓动
/// </summary>
public class TweenRotation : MonoBehaviour
{
    public Transform target;
    public bool active = false;
    public Vector3 rotate;
    public bool useLocal = false;

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

    void Update()
    {
        if (!active)
            return;
        if (target == null)
            return;
        target.Rotate(rotate * Time.deltaTime, useLocal ? Space.Self : Space.World);
    }
}


示例:模拟开门/关门

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 开门/关门旋转缓动
/// </summary>
public class TweenRotationDoor : MonoBehaviour
{
    public Transform target;
    public bool active = false;
    public float from = 0;
    public float to = 120;
    public float speed = 1;
    [SerializeField]
    private RoundAxis _axis = RoundAxis.Y;
    private Vector3 axis = Vector3.up;
    private Quaternion quaFrom;
    private Quaternion quaTo;
    private bool isOpen = false;
    private float t;

    //绕轴
    public enum RoundAxis
    {
        X,
        Y,
        Z
    }

    public bool IsOpen { get { return isOpen; } }

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

        switch(_axis)
        {
            case RoundAxis.X: axis = Vector3.right; break;
            case RoundAxis.Y: axis = Vector3.up;    break;
            case RoundAxis.Z: axis = Vector3.forward; break;
        }

        if (active)
        {
            quaFrom = Quaternion.AngleAxis(from, axis);
            quaTo = Quaternion.AngleAxis(to, axis);
            isOpen = true;
        }
    }

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

        t += speed * Time.deltaTime;
        Quaternion rot = Quaternion.LerpUnclamped(quaFrom, quaTo, t);
        target.localRotation = rot;

        if (t >= 1.0f)
            active = false;
    }

    //打开窗门
    public void Open()
    {
        if (isOpen)
            return;
        quaFrom = target.localRotation;
        quaTo = Quaternion.AngleAxis(to, axis);
        isOpen = true;
        t = 0;
        active = true;
    }

    //关闭窗门
    public void Close()
    {
        if (!isOpen)
            return;
        quaTo = Quaternion.AngleAxis(from, axis);
        quaFrom = target.localRotation;
        isOpen = false;
        t = 0;
        active = true;
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号