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?

More than 3 years have passed since last update.

Unity 3D入門 #5 [メッセージウィンドウの作成]

Posted at

ゲーム作成に不可欠なメッセージウィンドウを作ります。

UI->Panelを作成し、そこにテキストを追加したいと思います。
参考URL:https://xr-hub.com/archives/11782

参考URLに従い作成している途中、下のような画面となり、何か失敗したかと思いましたが、Gameビューで見ると、2枚目のように表示されていたので問題はなさそうです。
スクリーンショット 2020-07-20 17.41.29.png

スクリーンショット 2020-07-20 17.42.55.png

また、メッセージウィンドウを出し入れするプログラムを書きます。
プログラムはプレイヤーにアタッチし、特定のキーを入力することで表示/非表示されるようにします。

オブジェクト検索の参考URL:https://marunouchi-tech.i-studio.co.jp/2266/
キー入力参考URL:https://tech.pjin.jp/blog/2015/09/30/unityのキー入力まとめ-~inputクラス~/
キーコード参考URL:https://docs.unity3d.com/ja/2019.4/ScriptReference/KeyCode.html

しかし、下のようなコードを書いた場合、問題が発生しました。

void Update()
    {
        if (Input.GetKeyDown(KeyCode.I)){
            if (messagePanel.activeSelf){
                messagePanel.SetActive(false);
            }else{
                messagePanel.SetActive(true);
            }
        }
    }

一度「i」を押しただけで二回コードが呼ばれ、messagePanelのsetActiveがtrue->false->trueのように一瞬で変更され、結局メッセージウィンドウが表示され続けてしまいました。

>上のスクリプトを二つ別々のオブジェクトにアタッチしていたことが問題でした。

最後に、次に向けてタイマーを画面上に表示させようと思います。
タイマー作成参考URL:https://techacademy.jp/magazine/9311

float countTime = 0;
void Start() {
    Timer = GameObject.Find("Timer");
}
void Update() {
    countTime += Time.deltaTime; //スタートしてからの秒数を格納
    Timer.GetComponent<Text>().text = countTime.ToString("F2"); //小数2桁にして表示
}

簡単に書くと上のように実装。

GetComponent()でエラーが出た時の参考URL:https://monaski.hatenablog.com/entry/2015/11/03/125957

ezgif-7-676c0b901692.gif

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?