LoginSignup
5
6

More than 5 years have passed since last update.

Unityメモ1 モバイル キーボードから文字列を入力する(Android)

Last updated at Posted at 2014-06-23

編集可能なGUI要素をタップしたとき自動表示される場合

InputNameTextField.cs
public class InputNameTextField : MonoBehaviour {

    const int MAX_LENGTH = 4;
    string inputtedName = "Name";

    void OnGUI()
    {
        this.inputtedName = GUI.TextField(new Rect(Screen.width / 2 - 50, Screen.height * 1 / 3, 100, 20), this.inputtedName, MAX_LENGTH);
    }
}

手動でキーボードを表示させて入力する場合

InputName.cs
public class InputName.cs: MonoBehaviour {

    TouchScreenKeyboard keyboard;
    string inputtedName = "Name";
  
    // アタッチしているGameObjectに子オブジェクトとしてGUITextを作成している
    Transform nameText; 

    void Start () 
    {
        // GUITextを見つける
        this.nameText = gameObject.transform.Find("NameText");

        // キーボードを表示する
        this.keyboard = TouchScreenKeyboard.Open(this.inputtedName, TouchScreenKeyboardType.Default);
    }

    void Update()
    {
        if (this.keyboard.done)  // キーボードが閉じた時
        {
            this.inputtedName = this.keyboard.text;
            this.nameText.guiText.text = this.inputtedName;
        }
    }
}
5
6
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
6