Unity3D: 獲取輸入/控件焦點


簡單來講,就是

  1. 用GUI.SetNextControlName為該行代碼的下一句控件設置名字
  2. GUI.FocusControl來把焦點設置到某控件上,這里將用到上一步設置的名字

用GUI.GetNameOfFocusedControl來獲得焦點控件的名字。

示例代碼1:

GUI.SetNextControlName("Text1");
text1 = GUILayout.TextField(text1);
GUI.SetNextControlName("Text2");
text2 = GUILayout.TextField(text2);
if (GUI.GetNameOfFocusedControl() == string.Empty) {
    // Set focus to control Text1.
    GUI.FocusControl("Text1");
}

 

官方示例代碼:

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    public string username = "username";
    public string pwd = "a pwd";
    void OnGUI() {
        GUI.SetNextControlName("MyTextField");
        // This control will be named MyTextField.
        username = GUI.TextField(new Rect(10, 10, 100, 20), username);
        pwd = GUI.TextField(new Rect(10, 40, 100, 20), pwd);
        if (GUI.Button(new Rect(10, 70, 80, 20), "Move Focus"))
            GUI.FocusControl("MyTextField");
        
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM