Unity 工具类Time

Unity 学习笔记汇总
官方API使用文档

1. Time类1

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

public class test : MonoBehaviour
{
    public GameObject cube;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log(Time.deltaTime);

        cube.transform.Translate(Vector3.right * Time.deltaTime);   //每秒移动一次

        Debug.Log("Time.time : " + Time.time);  //游戏开始运行的总时间
    }
}

2. Time类2

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

public class test : MonoBehaviour
{
    public GameObject capsule;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        capsule.transform.Translate(Vector3.right * Time.deltaTime);

        if (Input.GetKeyDown(KeyCode.Q))
        {
            Time.timeScale = 3; //快
        }
        else if (Input.GetKeyDown(KeyCode.S))
        {
            Time.timeScale = 0.3f; //慢
        }
        else if (Input.GetKeyDown(KeyCode.B))
        {
            Time.timeScale = 0; //停
        }
        else if (Input.GetKeyDown(KeyCode.N))
        {
            Time.timeScale = 1; //正常
        }
    }
}

3. TestMathf1

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

public class test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Mathf.PI: " + Mathf.PI);
        Debug.Log("Mathf.Infinity: " + Mathf.Infinity);
        Debug.Log("Mathf.NegativeInfinity: " + Mathf.NegativeInfinity);

        Debug.Log("Mathf.CeilToInt(3.6f): " + Mathf.CeilToInt(3.6f)); //向上取整
        Debug.Log("Mathf.FloorToInt(3.6f): " + Mathf.FloorToInt(3.6f)); //向下取整
        Debug.Log("Mathf.RoundToInt(3.6f): " + Mathf.RoundToInt(3.6f)); //四舍五入

        Debug.Log("Mathf.Abs(-5): " + Mathf.Abs(-5)); //求绝对值
        Debug.Log("Mathf.Max(5, 6, 7): " + Mathf.Max(5, 6, 7));
        Debug.Log("Mathf.Min(5, 6, 7): " + Mathf.Min(5, 6, 7));

        Debug.Log("Mathf.Clamp(10, 2, 6)" + Mathf.Clamp(10, 2, 6));
        Debug.Log("Mathf.Clamp(9.8f, 0, Mathf.Infinity)" + Mathf.Clamp(9.8f, 0, Mathf.Infinity));
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

4. Time类作业

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

public class test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("Time.frameCount: " + Time.frameCount);
        Debug.Log("Time.realtimeSinceStartup: " + Time.realtimeSinceStartup);
        Debug.Log("Time.timeSinceLevelLoad: " + Time.timeSinceLevelLoad);
    }
}
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读