LoginSignup
5
5

More than 5 years have passed since last update.

フォーカスを持ち続けるInputField

Last updated at Posted at 2015-10-16

以下のスクリプトを InputField に Add Component

using UnityEngine;
using UnityEngine.UI;

public class InputFieldFocus : MonoBehaviour {
    void Start() {
        InputField i = this.GetComponent<InputField>();
        i.ActivateInputField(); //InputFieldにフォーカスを持たせる
        i.onEndEdit.AddListener(
            delegate(string text) {
                if (!string.IsNullOrEmpty(text)) {
                    Debug.Log(text);    //textの送り先
                    i.text = "";
                }
                i.ActivateInputField(); //InputFieldにフォーカスを持たせる
            }
        );
        //4.6xではこの処理がないとEnterを押した後、InputFieldに改行が入ってしまう(Single Line設定でも)
        //5.2xでは要らないのでコメントアウト
//      i.onValidateInput += delegate(string _1, int _2, char c) { return c == '\n' ? '\0' : c; };
    }
}

この処理はGameビューに表示させるデバッグ用コンソールウィンドウ(デバッグコマンド入力ができるもの)で使うInputFieldとか、チャットウィンドウで使うInputFieldで使用。

シーン開始直後からフォーカスを持つので、すぐに文字入力できる。
Enterで入力を完了した後も、InputFieldに入力した文字をクリアして、すぐにフォーカスを持つので、続けて文字入力可能。
PC(Windows)でしか動作確認していないので、モバイルでの動作は不明。

5
5
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
5
5