网页浏览总次数

星期二, 三月 05, 2024

Unity常用API方法和类(带代码)-6(Animator,Time,Mathf)

用于复习和快速查询常用API


Animator 动画组件


点击状态机中的动画 取消勾选Has Exit Time 切换动作直接切换


 

 







using UnityEngine;

public class No10_Animator : MonoBehaviour

{

    private Animator animator;//挂载Animator组件,提前创建好Animator Controller和动画资源

  

    void Start()

    {

        animator = GetComponent<Animator>();

        //animator.Play("Walk");

        //animator.speed = 2.0f;

        //animator.SetFloat("Speed", 1);

        //animator.SetBool("Dead", false);

        //animator.SetInteger("LV", 2);

    }

 

    void Update()

    {

        if (Input.GetKeyDown(KeyCode.W))

        {

            animator.CrossFade("Walk", 0.5f);

        }

    }

}

 

 

Time


void Update()

{

        Debug.Log(Time.deltaTime);//完成上一帧所用时间(以秒为单位)

        Debug.Log(Time.fixedDeltaTime);//执行物理或其他固定帧率更新的时间间隔

        Debug.Log(Time.fixedTime);//启动游戏以来总时间(执行物理或其他固定帧率更新的时间间隔累计计算的)

        Debug.Log(Time.time);//启动游戏以来的总时间

        Debug.Log(Time.realtimeSinceStartup);//启动游戏以来的实际时间

        Debug.Log(Time.smoothDeltaTime);//平滑处理的Time.deltaTime

        Debug.Log(Time.timeScale);//时间流逝的标度,用来慢/快动作 或者暂停游戏

        Debug.Log(Time.timeSinceLevelLoad);//自加载上一个关卡以来的时间

}

 

 

Mathf 数学


void Start()

{

        //静态变量

        print(Mathf.Deg2Rad);//度到弧度

        print(Mathf.Rad2Deg);//弧度到度

        print(Mathf.Infinity);//正无穷大

        print(Mathf.NegativeInfinity);//负无穷大

        print(Mathf.PI);//π

        //静态函数

        print(Mathf.Abs(-1.5f));//(int或者float)的绝对值

        print(Mathf.Acos(1));//1(弧度为单位)的反余弦

        print(Mathf.Floor(2.75f));//小于或等于2.75的最大整数(返回类型float)

        print(Mathf.FloorToInt(2.75f));//小于或等于2.75的最大整数(返回类型int)

        print(Mathf.Lerp(1, 2, 0.5f));//a b 按照 t 线性插值 a+(b-a)*t

}


没有评论:

发表评论