天天看点

unity获取系统时间

基于上篇自创建的计时器,下面是获取系统时间的显示,创建一个GUI Texture,javascript脚本,就下面代码挂上,即可实时获取系统时间

区别: C#是用DateTime类来获取时间,js是用Date对象来获取时间。

#pragma strict

var currentSystemTine:String;//定义获取系统时间

function Start()

{

}

function Update () {

 currentSystemTine = Date.Now.ToString("hh:mm:ss"); //获取系统时间,可以只显示小时和分钟,自己决定即可

 guiText.text=currentSystemTine;

}

//下面为javascript代码,1分钟倒计时

将GUIText拖入面板中即可

var display : GUIText;

var maxTime : float =60;    //时间

private var time : float;

function OnEnable()

{

UpdateTime();

}

function UpdateTime () {

time = maxTime;

while(time > 0)

{

display.text = Mathf.Floor(time / 60) + ":" + time % 60;

yield WaitForSeconds(1);

time -= 1;

}

}

var display : GUIText;

var maxTime : float =60;    //时间

var object : Object;

private var time : float;

function OnEnable()

{

}

function UpdateTime () {

time = maxTime;

while(time > 0)

{

display.text = Mathf.Floor(time / 60) + ":" + time % 60;

yield WaitForSeconds(1);

time -= 1;

}

//object.DoThings();   //时间到,可以了,

}

function OnGUI()

{

if(GUILayout.Button("开始计时"))

{

UpdateTime();

}

}

如果想点击按钮后才开始倒计时,那么只需要添加OnGUI()函数

var display : GUIText;

var maxTime : float =60;    //时间

private var time : float;

function UpdateTime () {

time = maxTime;

while(time > 0)

{

display.text = Mathf.Floor(time / 60) + ":" + time % 60;

yield WaitForSeconds(1);

time -= 1;

}

}

function OnGUI()

{

if(GUILayout.Button("开始计时"))

{

UpdateTime();

}

}