unity自帶GUI之控制元件的焦點獲取
阿新 • • 發佈:2019-02-13
using UnityEngine;
using System.Collections;
public class SetConFocus : MonoBehaviour {
public string username = "username";
public string pwd = "a pwd";
public string pwds = "a pwds";
int count = 1;
private bool isopen = false;
void OnGUI()
{ /***********設定下一個控制元件的名字****************/
GUI.SetNextControlName("MyTextField");
GUI.Button(new Rect(10, 10, 100, 20), username);
GUI.SetNextControlName("TEXT2");
GUI.Button(new Rect(10, 40, 100, 20), pwd);
GUI.SetNextControlName("TEXT3");
GUI.Button(new Rect(10, 70, 100, 20), pwds);
/*************按一下button的時候,名字為MyTextField的控制元件獲得焦點*******************/
if (GUI.Button(new Rect(10, 100, 80, 20), "Move Focus"))
{
Debug.Log(count);
if (count % 3 == 0)
{
GUI.FocusControl("MyTextField");
isopen = true;
}
else if (count % 3 == 1)
{
GUI.FocusControl("TEXT2");
}
else
{
GUI.FocusControl("TEXT3");
}
count++;
}
}
void Update(){
/*********焦點在上面的時候,就響應一些事件************/
if (isopen)
{
if (Input.GetKeyDown(KeyCode.Return))
{
isopen = false;
Debug.Log("the t button is pressed");
Debug.Log("the MyTextField is pressed");
}
}
}
using System.Collections;
public class SetConFocus : MonoBehaviour {
public string username = "username";
public string pwd = "a pwd";
public string pwds = "a pwds";
int count = 1;
private bool isopen = false;
void OnGUI()
{ /***********設定下一個控制元件的名字****************/
GUI.SetNextControlName("MyTextField");
GUI.Button(new Rect(10, 10, 100, 20), username);
GUI.SetNextControlName("TEXT2");
GUI.Button(new Rect(10, 40, 100, 20), pwd);
GUI.SetNextControlName("TEXT3");
GUI.Button(new Rect(10, 70, 100, 20), pwds);
/*************按一下button的時候,名字為MyTextField的控制元件獲得焦點*******************/
if (GUI.Button(new Rect(10, 100, 80, 20), "Move Focus"))
{
Debug.Log(count);
if (count % 3 == 0)
{
GUI.FocusControl("MyTextField");
isopen = true;
}
else if (count % 3 == 1)
{
GUI.FocusControl("TEXT2");
}
else
{
GUI.FocusControl("TEXT3");
}
count++;
}
}
void Update(){
/*********焦點在上面的時候,就響應一些事件************/
if (isopen)
{
if (Input.GetKeyDown(KeyCode.Return))
{
isopen = false;
Debug.Log("the t button is pressed");
Debug.Log("the MyTextField is pressed");
}
}
}
}
效果圖: