2016-06-22 3 views
-1

私のゲームでは、ユーザーがクリックしたときにタイマーを開始したい。ここで私が持っているコードは、これまでのところです:入力があるとタイマーを始めるには?

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 

public class Countdown : MonoBehaviour 
{ 
    float timeLeft = 30.0f; 
    public Text text; 
    public Text scoretext; 
    public Text finalscore; 
    public AudioSource ping; 
    public GameObject ball; 
    // Use this for initialization 
    void Start() 
    { 
     finalscore.text = "";   
    } 

    void countdownfunction() 
    { 
     timeLeft -= Time.deltaTime; 
     text.text = "Time Left: " + Mathf.Round(timeLeft) + " seconds"; 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     countdownfunction(); 

     if (timeLeft < 0) 
     { 
      ping = GetComponent<AudioSource>(); 
      text.text = "Time's up!"; 
      ping.Play(); 
      ball.SetActive(false); 
      finalscore.text = "Final score ^";    
     } 
    } 
} 

あなたが見ることができるように、タイマーは、すぐにゲームが起動すると起動しますが、私は、ユーザーがクリックを去ったとき、それは開始したいのですが、私を聞かせてくださいこれを行う方法があるかどうかを知ってください、ありがとう。

+0

トピックはありませんが、各フレームで 'GetComponent ()'を呼び出すのはやや非効率です。なぜあなたはそれをどこかに保管しないのですか? – MickyD

+0

@MickyD良いアイデア、私は昨日始めたUnityとC#の新機能です。しかし、ヒントをありがとう –

+0

心配しないで、ちょうど友好的なヒントでした:)楽しい同友Unityユーザー – MickyD

答えて

1

Update関数で、Input.GetMouseButtonDown(0)関数を使用して、ユーザーが左クリックしたかどうかを確認します。これは、アップデート機能からあなたのタイマーを分離するために良いことだ

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 

public class Countdown : MonoBehaviour 
{ 
    float timeLeft = 30.0f; 
    public Text text; 
    public Text scoretext; 
    public Text finalscore; 
    public AudioSource ping; 
    public GameObject ball; 
    bool timerStarted = false; 
    // Use this for initialization 
    void Start() 
    { 
     finalscore.text = "";   
    } 

    void countdownfunction() 
    { 
     timeLeft -= Time.deltaTime; 
     text.text = "Time Left: " + Mathf.Round(timeLeft) + " seconds"; 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     if(Input.GetMouseButtonDown(0)) 
      timerStarted = true; 
     if(timerStarted) 
      countdownfunction(); 

     if (timeLeft < 0) 
     { 
      ping = GetComponent<AudioSource>(); 
      text.text = "Time's up!"; 
      ping.Play(); 
      ball.SetActive(false); 
      finalscore.text = "Final score ^";    
     } 
    } 
} 
+0

ありがとう!完璧に働いた。 –

2

: あなたのコードは次のようになります。別の機能を作ります。コルーチンは、あなたがそれをしばらくの間待ってから操作を再開するのに簡単に使うことができるので、この種のものには完璧です。また、コンポーネントを複数回使用する場合は、コンポーネントをキャッシュします。 ping変数を多く使用するので、キャッシュに意味があるのはAwakeまたはStartです。あなたの既存のコードに次の行を追加しますonMouseDownイベント

を使用する必要が

void Start() 
{ 
    finalscore.text = ""; 
    ping = GetComponent<AudioSource>(); 
} 

void Update() 
{ 
    //Check if left mouse button is clicked 
    if (Input.GetMouseButton(0)) 
    { 
     StartCoroutine(startTimer(30)); 
    } 
} 

IEnumerator startTimer(float timeLeft) 
{ 
    while (timeLeft > 0) 
    { 
     timeLeft -= Time.deltaTime; 
     text.text = "Time Left: " + Mathf.Round(timeLeft) + " seconds"; 
     yield return null; 
    } 

    text.text = "Time's up!"; 
    ping.Play(); 
    ball.SetActive(false); 
    finalscore.text = "Final score ^"; 
} 
0

: 、ブール変数を作成しfalseに設定し、それが本当れるonmousedown作り、それがtrueに設定されます場合にのみ、タイマーを開始:

private bool mouseClicked=false; 

void OnMouseDown(){ 
mouseClicked = true; 
} 

void Update() { 

    if (mouseClicked){ 
     countdownfunction(); 
    } 

    if (timeLeft < 0) 
    { 
     ping = GetComponent<AudioSource>(); 
     text.text = "Time's up!"; 
     ping.Play(); 
     ball.SetActive(false); 
     finalscore.text = "Final score ^"; 


    } 
} 
関連する問題