Unity入门操作_Time类_004

来源:互联网 发布:爆菊 知乎 编辑:程序博客网 时间:2024/06/04 00:29

Time.deltaTime 增量时间
Time.time 时间(游戏总共的时间,无论游戏是否暂停)
Time.timeScale 时间缩放
// Use this for initialization
void Start () {

}//计时器float timer = 0;// Update is called once per framevoid Update(){    timer += Time.deltaTime;    if (timer >= 3)    {        GameObject.Instantiate(gameObject);        timer = 0;    }}

}
//在游戏中显示的现实时间
public class Chang : MonoBehaviour {
int h;
int m;
int s;
float time;
string result;
// Use this for initialization
void Start () {

}// Update is called once per framevoid Update () {}void OnGUI(){    GUI.Label(new Rect(0, 0, 70, 20), result);    Test01();}void Test01(){   time+= Time.deltaTime;    if (time>=1)    {        time -= 1;        s++;    }    if (s>=60)    {        s = 0;        h++;    }    if (h>=60)    {        h = 0;    }    result = string.Format("{0:00}:{1:00}:{2:00}", h, m, s);  //result = h + "时" + m + "分" + s + "秒";}

}
时间缩放
public class CubeMove : MonoBehaviour {

string distanceStr = "距离:0";Vector3 startPosition;string timeText = "00:00:00";// Use this for initializationvoid Start () {    startPosition = transform.position;    Debug.Log("屏幕的宽度:"+Screen.width);    Debug.Log("屏幕的高度:"+Screen.height);}float timer = 0;int hour = 0;int minute = 0;int second = 0;// Update is called once per framevoid Update () {    timer += Time.deltaTime;    if (timer>1)    {        second++;        timer -= 1;    }    if (second>=60)    {        minute++;        second = 0;    }    if (minute>=60)    {        hour++;        minute = 0;    }    timeText = string.Format("{0:00}:{1:00}:{2:00}",hour,minute,second);    transform.Translate(transform.forward * Time.deltaTime * 2);}void OnGUI(){    GUI.Label(new Rect(65, 0, 100, 60), distanceStr);    GUIStyle style = new GUIStyle();    style.alignment = TextAnchor.MiddleCenter;    GUI.Label(new Rect(Screen.width-100,0,100,60), timeText, style);    if (GUI.Button(new Rect(0,0,60,40),"1倍速度"))    {        Time.timeScale = 1;    }    if (GUI.Button(new Rect(0, 45, 60, 40), "2倍速度"))    {        Time.timeScale = 2;    }    if (GUI.Button(new Rect(0, 90, 60, 40), "暂停"))    {        Time.timeScale = 0;    }    float distance = Vector3.Distance(startPosition,transform.position);    distanceStr = "距离:"+distance;    }

}
这里写图片描述

原创粉丝点击