UnityEngine下Time 类的学习,时间管理器,计时器,时间线,

版权声明:转载 请说明出处 https://blog.csdn.net/qq2512667/article/details/88784799

在这里插入图片描述在这里插入图片描述

Time 类用的最多的 应该就是Time.deltime; 第一次接触Time类的也是 这个属性,这个就是 上一帧的偏移量,
电脑 完成 上一帧 到现在 这一帧的时间 。

简单计时器

  float Timer=0.0f;
  float WaitTime=1.0f;
  Update()
  {
  //check input
  if (Input.GetKey(KeyCode.Space))
     {
				  Timer += Time.deltaTime;
	            if (Timer>WaitTime)
	            {
	                Timer -= WaitTime;
	                //Do Something    Instantiate(Bullect)
	                      GameObject bullectGo = Instantiate(Bullect, transform.position transform.rotation);
                    bullectGo.GetComponent<Rigidbody>().AddForce(transform.forward * 1000);
	            }
	  }
}

上面这个伪代码就是 每过1秒 做些事情, 如如 按下 space键 每秒发射一颗子弹。
这里 比较粗糙,只是说说应用,就是一个简单 的计时器功能。

FPS游戏 子弹 应该有个 对象池管理,然后子弹身上也应该有自己的数据脚本,它自己的生命周期,速度,伤害,威力,作用对象…等等。

在生命周期函数 update里
因为每个 电脑性能都不一样,更新的频率也不一样,所以偏移量也 会因此不同。
这里如果只是要处理一些简单的 逻辑 就可以直接 用简单的计时器,
如果 复杂点的可以 用协同程序来解决

   IEnumerator waitTimer(float timer)
    {
        yield return new WaitForSeconds(timer);
        //Do somthing
        yield return new WaitForSeconds(timer);
        //Do .... 
        while (true)
        {
            if (condition)
            {
                break;
            }
            //在所有相机和GUI渲染之后,等待直到帧结束,就在屏幕上显示帧之前。
            yield return new WaitForEndOfFrame();  
        }
    }

协同程序背后也是一个迭代器机制。用状态机完成。
使用前 要先Stop下保证单一协同程序执行。因为协同程序 不允许同时出现。

Time.realtimeSinceStartup
游戏开始后开始读秒,真实时间线

Time.time
游戏(帧)开始的时间
Time.smoothDeltaTime
平滑的Time.DeltaTime 防止帧数波动过大, 不同机器性能不一样。

Time.frameCount 计算 帧数。 应该就是每次 传递的帧的总数了。
这个 可以用来截图, 因为帧数 是一直增加的,截图无非就是截下某一帧的画面。
Unity在 ScreenCapture类 里提供了**CaptureScreenshot()**方法。在指定目录下生成一张截取游戏屏幕的图片。在这里插入图片描述

下面是小例子,来自Unity官方。

扫描二维码关注公众号,回复: 6047113 查看本文章
    #region 截图  captureFramerate的 应用
    public string folder = "ScreenshotFolder";
    public int frameRate = 25;
   #endregion
     void Start()
    {
        //设置播放帧数(在此之后,实时将不与游戏时间相关)。
        Time.captureFramerate = frameRate;
        //创建目录
        System.IO.Directory.CreateDirectory(folder);
    }

private void OnGUI()
    {
  #region captureFramerate
        if (GUILayout.Button(new GUIContent("截图","截图工具")))
        {
            //将文件名附加到文件夹名(格式为“帧数 shot.png”)
            string name = string.Format("{0}/{1:D04} shot.png", folder, Time.frameCount);

            //  在指定 目录下捕获 屏幕截图
            ScreenCapture.CaptureScreenshot(name);
        }
        #endregion
        }

Time.timeSinceLevelLoad
加载关卡以来用的时间
在 加载 新场景的时间 会重置。

Time.timeScale 时间流逝的速率, 影响Time的流逝。0.5是 放慢时间速率 放慢2倍。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class TestTime : MonoBehaviour
{
    // Start is called before the first frame update

    #region 截图  captureFramerate的 应用
    public string folder = "ScreenshotFolder";
    public int frameRate = 25;

    public bool isFire=false;

    #endregion

    float Timer = 0.0f;
    public float WaitTime = 1.0f;
    public GameObject Bullect;

    public float TimeScaleRate=1;

    void Start()
    {

        DontDestroyOnLoad(this);
        //设置播放帧数(在此之后,实时将不与游戏时间相关)。
        Time.captureFramerate = frameRate;

        //创建目录
        System.IO.Directory.CreateDirectory(folder);

    }

    private void OnGUI()
    {
        GUILayout.Space(20);
        GUILayout.Label("RealTime(游戏开始后开始读秒,真实时间线):" + Time.realtimeSinceStartup);
        GUILayout.Label("Time.Time(游戏(帧)开始的时间):" + Time.time);
        GUILayout.Label("Time.smoothDelaTime:" + Time.smoothDeltaTime);
        GUILayout.Label("Time.captureFramerate(减慢游戏播放时间,以便在帧之间保存屏幕截图):" + Time.captureFramerate);
        GUILayout.Label("Time.timeSinceLevelLoad (加载关卡以来的时间):" + Time.timeSinceLevelLoad);
        GUILayout.TextArea("renderedFrameCount:" + Time.renderedFrameCount);

        //GUIStyle slider = new GUIStyle(GUI.skin.horizontalSlider);
        //GUIStyle sliderThumb = new GUIStyle(GUI.skin.horizontalSliderThumb);

        //TimeScaleRate= GUI.HorizontalSlider(new Rect(Screen.width / 8, Screen.height / 4, Screen.width - (4 * Screen.width / 8), Screen.height - (2 * Screen.height/ 4)),TimeScaleRate, 0, 10,slider,sliderThumb);
        //Time.timeScale = TimeScaleRate;
        //slider.fixedHeight = Screen.height / 32;
        //slider.fixedWidth = 12*(Screen.width / 200);
        //sliderThumb.fixedHeight = Screen.height / 8;
        //sliderThumb.fixedWidth = Screen.width / 8;
       
        #region captureFramerate
        if (GUILayout.Button(new GUIContent("截图","截图工具")))
        {
            //将文件名附加到文件夹名(格式为“帧数 shot.png”)
            string name = string.Format("{0}/{1:D04} shot.png", folder, Time.frameCount);

            //  在指定 目录下捕获 屏幕截图
            ScreenCapture.CaptureScreenshot(name);
        }
        #endregion

        if (GUILayout.Button("Time.timeSinceLevelLoad"))
        {
            Debug.Log ("Time.timeSinceLevelLoad:要加载了场景,就会重新计算");
            SceneManager.LoadScene(0);
        }

        if (GUILayout.Button("Fire a bullect per second"))
        {
            isFire = !isFire;
            
        }
        
    }

 

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

        if (isFire)
        {

            if (Input.GetKey(KeyCode.Space))
            {

                Timer += Time.deltaTime;
                if (Timer > WaitTime)
                {
                    Timer -= WaitTime;

                    GameObject bullectGo = Instantiate(Bullect, transform.position ,transform.rotation);
                    bullectGo.GetComponent<Rigidbody>().AddForce(transform.forward * 1000);

                }
            }
         
            
        }
    }


}

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

public class TimeDealTimeExp : MonoBehaviour
{

    float waitTime = 2.0f;
    private float timer = 0.0f;
    float visualTime = 0.0f;
    int width, height;
    float value = 10.0f;
    float scrollBar=1.0f;

    private void Awake()
    {
        width = Screen.width;
        height = Screen.height;
        Time.timeScale = scrollBar;

        Debug.Log((int)5.5f);
        Debug.Log("Mathf.RoundToInt(10.5f):" + Mathf.RoundToInt(10.5f));
        Debug.Log("Mathf.RoundToInt(10.6f):" + Mathf.RoundToInt(10.6f));
        Debug.Log("10.4f:" + Mathf.RoundToInt(10.4f));
        Debug.Log("-10.5f:" + Mathf.RoundToInt(-10.5f));
        Debug.Log("-10.4f:" + Mathf.RoundToInt(-10.4f));
        Debug.Log("-10.6f:" + Mathf.RoundToInt(-10.6f));
        Debug.Log("Mathf.RoundToInt(-11.5f):" + Mathf.RoundToInt(-11.5f));
        Debug.Log("-12.5f:" + Mathf.RoundToInt(-12.5f));

        
         
        Debug.Log("f1:"+5.555f.ToString("f1"));
        Debug.Log("f2:"+5.555f.ToString("f2"));
        Debug.Log("f3:"+5.5555f.ToString("f3"));
        Debug.Log("四舍五入,结尾是0.5f ,奇偶 放回偶");
    }   
 
    // Update is called once per frame
    void Update()
    {
        timer += Time.deltaTime;

        // 检查是否超过2秒
        // 到达时间2秒  减去2   会比重置为0精确
        if (timer>waitTime)
        {
            visualTime = timer;

            timer = timer - waitTime;
            Time.timeScale = scrollBar;
        }
    }
    private void OnGUI()
    {
        GUIStyle sliderDetails = new GUIStyle(GUI.skin.GetStyle("horizontalSlider"));
        GUIStyle sliderThumbDetails = new GUIStyle(GUI.skin.GetStyle("horizontalSliderThumb"));
        GUIStyle labelDelDetails = new GUIStyle(GUI.skin.GetStyle("laber"));


        labelDelDetails.fontSize=6*(width/200);
        sliderDetails.fixedHeight = height / 32;
        sliderDetails.fontSize = 12 * (width / 200);
        sliderThumbDetails.fixedHeight = height / 32;
        sliderThumbDetails.fixedWidth = width / 32;

        //显示 滑动条 使比例比需要的尺寸 大10倍
        value = GUI.HorizontalSlider(new Rect(width / 8, height / 4, width - (4 * width / 8), height - (2 * height / 4)), value, 5.0f, 20.0f, sliderDetails, sliderThumbDetails);

        //显示 滑动条的值,从0.5 以此 递增 0.1 到2.0 显示
        float v = ((float)Mathf.RoundToInt(value)) / 10.0f;
        GUI.Label(new Rect(width / 8, height / 3.25f, width - (2 * width / 8), height - (2 * height / 4)), "time.TimeScale:" + v.ToString("f1"), labelDelDetails);
        scrollBar = v;


        //以一定的大小显示 记录的时间
        labelDelDetails.fontSize = 14 * (width / 200);

        GUI.Label(new Rect(width / 8, height / 2, width - (2 * width / 8), height - (2 * height / 4)), "Timer value is: " + visualTime.ToString("f1") + " seconds.", labelDelDetails);


    }
}

猜你喜欢

转载自blog.csdn.net/qq2512667/article/details/88784799