*やりたい事
unity2Dシューティングゲームにて、ゲームをクリアした時だけに、プレイに要した時間を表示させたい。このために、二つのスクリプトを作りました。
①TimerScript:時間の計測はゲームスタート時START、ゲームクリア時にSTOPさせたい
②GameController:ヒエラルキー上のUICanvasに配置したTimerを表示・非表示等管理
*できていること
*再生ボタンを押し、ゲームクリアをするとTimerは表示され、Timerも動く。
問題点とやりたいこと
TimerTextが表示された時からカウントが始まってしまうので、カウントはゲームスタート時から始まり、ゲームクリア時にカウントストップさせたい。
unity
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5using UnityEngine.SceneManagement; 6 7public class Timer : MonoBehaviour 8{ 9 using System.Collections; 10using System.Collections.Generic; 11using UnityEngine; 12using UnityEngine.UI; 13using UnityEngine.SceneManagement; 14using System; 15 16public class Timer : MonoBehaviour 17{ 18 private int minute; 19 private float seconds; 20 private float beforeSeconds; 21 private Text timerText; 22 public void Start() 23 { 24 minute = 0; 25 seconds = 0f; 26 beforeSeconds = 0f; 27 timerText = GetComponentInChildren<Text>(); 28 DateTime now = DateTime.Now; 29 } 30 31 public void Update() 32 { 33 34 seconds += Time.deltaTime; 35 if (seconds >= 60f) 36 { 37 minute++; 38 seconds = seconds - 60; 39 40 } 41 // 値が変わった時テキストを更新 42 if ((int)seconds != (int)beforeSeconds) 43 { 44 timerText.text = minute.ToString("00") + ":" + ((int)seconds).ToString("00"); 45 } 46 beforeSeconds = seconds; 47 } 48 49 50} 51
public class GameController : MonoBehaviour using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class GameController : MonoBehaviour { public Timer timer; public GameObject TitleText; public GameObject gameOverText; public Text scoreText; public GameObject Timer; public GameObject GameCrearStageNextText; public int score = 0; // ステージ数のテキスト public Text stageNumberText; public GameObject stageNextButton; public GameObject retryButton; public GameObject Playership1; public GameObject shootingenemy1Prefab; public GameObject advancedEnemy1Prefab; public GameObject Enemy3rdPrefab; public void Start() //ゲームオーバーテキストは消えてる・スコアテキストの表示・ハイドボタン(stagenextbuttonは消す・リトライ消す) { gameOverText.SetActive(false); stageNextButton.SetActive(false); retryButton.SetActive(false); GameCrearStageNextText.SetActive(false); //timer = GameObject.Find("Timer").GetComponent<Timer>(); timer.Start(); timer.Update(); Timer.SetActive(false); scoreText.text = "SCORE:" + score; } public void AddScore() //Scoreを100ずつ増やす { score += 100; scoreText.text = "SCORE:" + score; if (score == 100) { stageNextButton.SetActive(true); GameCrearStageNextText.SetActive(true); Timer.SetActive(true); Destroy(Playership1); Destroy(shootingenemy1Prefab); Destroy(advancedEnemy1Prefab); Destroy(Enemy3rdPrefab); } } public void GameOver() //GameOver文字を表示・リトライボタンを出す { gameOverText.SetActive(true); retryButton.SetActive(true); } public void RetryScene() //Mainシーンをロード { SceneManager.LoadScene("first"); } public void FirstToSecondButton() { SceneManager.LoadScene("Second"); } public void SecondToThirdButton() { SceneManager.LoadScene("Third"); } public void ThirdTo4ThButton() { SceneManager.LoadScene("4th"); } }
回答2件
あなたの回答
tips
プレビュー