1、創建InputField
creat->UI->InputField
2、基本屬性界面
可以再content type屬性下設置InputField的文本類型(圖片中無顯示)
3、文本監聽``
創建腳本(代碼如下)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class n : MonoBehaviour
{
public InputField inputField;
// Start is called before the first frame update
void Start()
{
//添加監聽事件
transform.GetComponent<InputField>().onValueChange.AddListener(Change);
transform.GetComponent<InputField>().onEndEdit.AddListener(End);
}
void Change(string str)
{
Debug.Log("正在輸入:"+str);
}
void End(string str)
{
Debug.Log("輸入結果爲"+str);
}
}
注意:當輸入框爲密碼框時需要使用監聽事件來獲取文本內容,如果用.text會獲得********。
小白筆記 歡迎指點