Unity中的Time类
Time.deltaTime与Time.timeScale
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyTime : MonoBehaviour
{
// Start is called before the first frame update
void Start()//用于初始化的,只调用一次
{
}
// Update is called once per frame
void Update()//每帧都会调用
{
this.transform.Rotate(0, Time.deltaTime * 1000, 0);//角度随着时间而变化
this.transform.Translate(Time.deltaTime * 10, 0, 0);//位置随着时间变化
}
void OnGUI()//此场景中是自由布局
{
if (GUILayout.Button(“slow”))//在game面板上设置一个按钮为slow
{
Time.timeScale = 0.2f;//设置时间的长度,正常的时间长度为 1,这里相当于时间变慢了。
}
if (GUILayout.Button(“stop”))
{
Time.timeScale = 0;
}
if (GUILayout.Button(“fast”))
{
Time.timeScale = 1.5f;
}
}
}
如何获取本地时间
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class DataTimeT : MonoBehaviour
{
public GUISkin gSkin;
public string str = "";
public string[] month = { "一月", "二月", "三月", "四月", "四月", "五月", "六月", "七月",
"七月", "八月", "九月", "十月", "十一月", "十二月" };
public string[] Day = {"一", "二", "三", "四", "五",
"六","七","八","九","十","十一","十二","十三","十四","十五",
"十六","十七","十八","十九","二十","二一","二十二","二十三","二十四","二十五",
"二十六", "二十七", "二十八", "二十九", "三十", "三十一" };
public System.DateTime dNow;
void OnGUI()
{
if (gSkin)
{
GUI.skin = gSkin;
}
GUILayout.Box("本地时间:" + DateTime.Now);//显示现在的时间;
GUILayout.SelectionGrid(DateTime.Now.Month, month, 3);//在game面板上显示月份,一行三个
GUILayout.SelectionGrid(DateTime.Now.Day, Day, 10);//在game面板上显示日期,一行十个。
}
}
小结
今天的内容还是比较易懂的,希望能够对你有帮助。