0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ゲームの入力制御を状態管理で分ける

Last updated at Posted at 2025-04-05

名前を入力中に他のスプリクトが「Enter」を拾ってしまう -> 入力のバッティング

「ゲーム状態」をGameManagerで一旦管理して、スクリプトごとに「この状態の時だけ動く」と制限をかけた。

名前を入力する際に後ろの吹き出しの入力を止める方法


まずゲーム全体の状態を管理する

新しいスクリプトex)GameManagerを作成して、以下のようにする

public enum GameState
{
    Playing,    // 普通の状態(探索とか)
    Naming,     // 名前入力中
}

public class GameManager : MonoBehaviour
{
    public static GameManager instance; // どこからでもアクセスできるようにする

    public GameState gameState = GameState.Playing;

    void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject); // シーンをまたいでも保持
        }
        else
        {
            Destroy(gameObject); // ダブり防止
        }
    }
}

コードの説明

public enum GameState
{
    Playing,    // 普通の状態(探索とか)
    Naming,     // 名前入力中
}

ここはゲームの状態を分けるためのを作成

  • enum GameDtateとは
     名前付きの数字
// 実際にはこういう数字が割り当てられてる
Playing = 0,
Naming  = 1

なので比較演算子などで使用が可能

  • Awake()メソッド
     初期化の処理
void Awake()
{
   if (instance == null) // 初めて作られたかチェック
   {
       instance = this; // このオブジェクトを自分自身として登録
       DontDestroyOnLoad(gameObject); // シーンを切り替えても消えないようにする
   }
   else // すでに1つあったら、2つ目を破棄
   {
       Destroy(gameObject);
   }
}

GameManagerスクリプトの使い方

最初はPlayingにしておいて、名前を入力する際に

GameManager.instance.gameState = GameState.Naming;

を設定する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?