Unity入门操作_Time类_004

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

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

}
//在游戏中显示的现实时间
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 frame
void 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 initialization
void 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 frame
void 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;
    }

}
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值