茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1674973 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_00107.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2015/1/20 9:27:13
Count Down Timer.

Ok, what I'm trying to do is make a count down timer that will start, at maybe 2 minutes, at the start of the game, and when It reaches 0:00 then some text will appear on the screen. I know how to do the text and how to start it, but I don't know how to make the actual timer, any Ideas?
P.S. I did check out the other two questions about counters and timers, but i couldn't do anything with that :/

I got a simple version of a countdown timer here. I'm sure there are other and more elegant versions out there (e.g. see this forum thread), but they are also a bit lengthy. See for yourself.

private var startTime; private var restSeconds : int; private var roundedRestSeconds : int; private var displaySeconds : int; private var displayMinutes : int;
var countDownSeconds : int;
function Awake() { startTime = Time.time; }
function OnGUI () { //make sure that your time is based on when this script was first called //instead of when your game started var guiTime = Time.time - startTime;
    restSeconds = countDownSeconds - (guiTime);

    //display messages or whatever here -->do stuff based on your timer
    if (restSeconds == 60) {
    print ("One Minute Left");
    }
    if (restSeconds == 0) {
    print ("Time is Over");
    //do stuff here
    }

    //display the timer
    roundedRestSeconds = Mathf.CeilToInt(restSeconds);
    displaySeconds = roundedRestSeconds % 60;
    displayMinutes = roundedRestSeconds / 60;

    text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
    GUI.Label (Rect (400, 25, 100, 30), text);

}


there are many ways to do this. for example another way would be
var showText=false;
function StopWatch (time:float) { yield WaitForSeconds (time); showText=true; } function OnGUI () { if (showText==true) { GUI.Label (Rect(100,100,300,150),"this text will be shown after the timer ends"): } }

so how did you sort the rounding problem?? nevermind i got it all you need to do is change some variables. if anyone needs this in c# here you go :) you will need to change public CountDownSeconds in the inspector to any value of your choice ie, 10:00, you would write 600 in the inspector panel.
using UnityEngine;
using System.Collections;

public class TimerScript : MonoBehaviour
{
private float startTime;
private float restSeconds;
private int roundedRestSeconds;
private float displaySeconds;
private float displayMinutes;
public int CountDownSeconds=120;
private float Timeleft;
string timetext;
// Use this for initialization void Start ()
{
 startTime=Time.deltaTime;
}
void OnGUI()
{
 Timeleft= Time.time-startTime;
 restSeconds = CountDownSeconds-(Timeleft);
 roundedRestSeconds=Mathf.CeilToInt(restSeconds);
 displaySeconds = roundedRestSeconds % 60;
 displayMinutes = (roundedRestSeconds / 60)%60;
 timetext = (displayMinutes.ToString()+":");
 if (displaySeconds > 9)
 {
  timetext = timetext + displaySeconds.ToString();
 }
 else
 {
  timetext = timetext + "0" + displaySeconds.ToString();
  }
  GUI.Label(new Rect(650.0f, 0.0f, 100.0f, 75.0f), timetext);
  }
 }
}


Here, very simple (I am using the code in a 3Dtext):
    public var time : float;
    function Update () {
    time -= Time.deltaTime;
    var minutes : int = time / 60;
    var seconds : int = time % 60;
    var fraction : int = (time * 100) % 100;
    if (time>0)
    //displaying in the 3Dtext
    GetComponent(TextMesh).text = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
    }



原文出處:Count Down Timer. - Unity Answers
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|