【Unity】シーン切り替えの前に簡単にテキストや画像を表示する

投稿者: | 2021-04-13

ゲームオーバーでシーンを再読み込みする前に、テキストや画像を表示するためのオブジェクトを簡単に作ってみました。

まずシーンにCanvasを新規作成しました。これを後でプレハブ化して、シーン切り替えをしたいときにインスタンス化することにします。

なので、他のCanvasよりもSort Orderの値を大きくして、一番手前に表示されるようにしました。また、UI Scale Modeを「Scale With Screen Size」にします。

このCanvasの子として画像やテキストを配置します。画像は中央に表示して、縦横サイズをReference Resolutionの値にすると画面いっぱいに表示されます。色を黒にすると画面が真っ暗になります。

テキストも中央に表示します。

また、このCanvasにスクリプトを付けました。インスタンス化されると数秒おきにテキストや画像が表示されます。

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.SceneManagement;
  4.  
  5. public class GameOver : MonoBehaviour
  6. {
  7. Text text;
  8. Image image;
  9. static GameOver instance;
  10.  
  11. // インスタンスを取得
  12. public static GameOver GetInstance()
  13. {
  14. // 無い時
  15. if (instance == null)
  16. {
  17. Debug.Log("インスタンスがありません");
  18. }
  19. // 既にある時
  20. else
  21. {
  22. Debug.Log(instance);
  23. }
  24.  
  25. return instance;
  26. }
  27.  
  28. private void Awake()
  29. {
  30. // インスタンスが既にある場合
  31. if (instance != null)
  32. {
  33. // 新規作成されたものは削除
  34. Destroy(gameObject);
  35. Debug.Log("削除されました");
  36. }
  37. // 無い時
  38. else
  39. {
  40. instance = this;
  41. Debug.Log("インスタンスが作成されました");
  42.  
  43. // 子のテキストを取得
  44. text = GetComponentInChildren<Text>();
  45.  
  46. // テキストが無い時
  47. if (text == null)
  48. {
  49. // 削除
  50. Destroy(gameObject);
  51. Debug.Log("削除されました");
  52. return;
  53. }
  54.  
  55. // テキストを非表示
  56. text.enabled = false;
  57.  
  58. // 画像を取得
  59. image = GetComponentInChildren<Image>();
  60.  
  61. // 画像が無い時
  62. if (image == null)
  63. {
  64. // 削除
  65. Destroy(gameObject);
  66. Debug.Log("削除されました");
  67. return;
  68. }
  69. // 画像を非表示
  70. image.enabled = false;
  71.  
  72. // 1秒後にテキストを表示
  73. Invoke("EnableText", 1f);
  74. }
  75. }
  76.  
  77. void EnableText()
  78. {
  79. // テキストを表示
  80. text.enabled = true;
  81.  
  82. // 2秒後に画像を表示
  83. Invoke("EnableImage", 2f);
  84. }
  85.  
  86. void EnableImage()
  87. {
  88. // 画像を表示
  89. image.enabled = true;
  90.  
  91. // 2秒後にシーンを再読み込み
  92. Invoke("LoadScene", 2f);
  93. }
  94.  
  95. void LoadScene()
  96. {
  97. // 現在のシーンをロード
  98. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  99. }
  100. }

シングルトンにして、既にインスタンスがある場合は削除されるようにしています。

  1. private void Awake()
  2. {
  3. // インスタンスが既にある場合
  4. if (instance != null)
  5. {
  6. // 新規作成されたものは削除
  7. Destroy(gameObject);
  8. Debug.Log("削除されました");
  9. }

また、空のゲームオブジェクトを新規作成して、AddComponentでこのスクリプトを付けた場合など、子オブジェクトがテキストや画像を持たないときも削除されます。

  1. // 子のテキストを取得
  2. text = GetComponentInChildren<Text>();
  3.  
  4. // テキストが無い時
  5. if (text == null)
  6. {
  7. // 削除
  8. Destroy(gameObject);
  9. Debug.Log("削除されました");
  10. return;
  11. }

後は、Invokeメソッドを使って、数秒おきにテキストや画像の表示、シーンの再読み込みをします。

インスタンス化は他のスクリプトで行います。キー入力して以降、毎フレーム、インスタンスの取得とインスタンス化を繰り返すようにしてみました。

  1. using UnityEngine;
  2.  
  3. public class GameScript5 : MonoBehaviour
  4. {
  5. [SerializeField] GameOver gameOverCanvas;
  6. bool gameOver;
  7.  
  8. // Start is called before the first frame update
  9. void Start()
  10. {
  11. }
  12.  
  13. // Update is called once per frame
  14. void Update()
  15. {
  16. if(Input.GetKeyDown(KeyCode.E))
  17. {
  18. gameOver = true;
  19. }
  20.  
  21. if(gameOver)
  22. {
  23. GameOver go = GameOver.GetInstance();
  24. Instantiate(gameOverCanvas.gameObject);
  25.  
  26. // GameObject obj = new GameObject();
  27. // obj.AddComponent<GameOver>();
  28.  
  29. //new GameOver();
  30. }
  31. }
  32. }

Canvasをプレハブ化してこのスクリプトにアタッチします。

これで簡単に、シーンの再読み込みと、その直前のテキストや画像の表示などを行うオブジェクトが作れました。

一度インスタンス化されると、それ以降はすぐに削除されます。

また、テキストや画像を含まないときも削除されます。

  1. GameObject obj = new GameObject();
  2. obj.AddComponent();

MonoBehaviourを継承しているので、newキーワードでのインスタンス化は禁止されています。

コメントを残す

メールアドレスが公開されることはありません。