Unity3d 如何在单击';选项卡';unity 3d中的按钮
我希望使用tab按钮聚焦输入字段。我找到了一些用于聚焦输入字段的代码,但那一个不满足我的要求。在我的应用程序中,一个页面的设计就像水平和垂直格式中输入字段的数量一样。在我的页面中有3行输入字段,每行有3个输入字段。我的要求是,当用户单击tab按钮时,聚焦下一个正在工作的输入字段,但当到达行的末尾时,如何聚焦下一行包含的输入字段。请提出任何意见。非常感谢。你可以在下面找到我的页面设计示例 这是我尝试过的代码Unity3d 如何在单击';选项卡';unity 3d中的按钮,unity3d,Unity3d,我希望使用tab按钮聚焦输入字段。我找到了一些用于聚焦输入字段的代码,但那一个不满足我的要求。在我的应用程序中,一个页面的设计就像水平和垂直格式中输入字段的数量一样。在我的页面中有3行输入字段,每行有3个输入字段。我的要求是,当用户单击tab按钮时,聚焦下一个正在工作的输入字段,但当到达行的末尾时,如何聚焦下一行包含的输入字段。请提出任何意见。非常感谢。你可以在下面找到我的页面设计示例 这是我尝试过的代码 Selectable next = system.currentSelectedGameO
Selectable next = system.currentSelectedGameObject.GetComponent<Selectable().FindSelectableOnRight();
if (next != null)
{
InputField inputfield = next.GetComponent<InputField>();
if (inputfield != null)
{
inputfield.OnPointerClick(new PointerEventData(system));
system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
}
}
Selectable next=system.currentSelectedGameObject.GetComponent我已经用NGUI实现了一个类似的函数,如果使用UGUI,您可以进行一些修改。这个想法是通过公共变量手动设置nextInput的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIInputTab : MonoBehaviour {
UIInput thisInput;
public GameObject nextInput;
void Start () {
thisInput = transform.GetComponent<UIInput>();
}
void Update () {
if (thisInput.isSelected)
{
if (nextInput != null && Input.GetKeyDown(KeyCode.Tab))
{
UICamera.selectedObject = nextInput;
}
}
}
}
结果:
我已经用NGUI实现了类似的功能,如果使用UGUI,您可以进行一些修改。这个想法是通过公共变量手动设置nextInput的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIInputTab : MonoBehaviour {
UIInput thisInput;
public GameObject nextInput;
void Start () {
thisInput = transform.GetComponent<UIInput>();
}
void Update () {
if (thisInput.isSelected)
{
if (nextInput != null && Input.GetKeyDown(KeyCode.Tab))
{
UICamera.selectedObject = nextInput;
}
}
}
}
结果:
这是我的解决方案,非常类似
public class TabToNextController : MonoBehaviour {
public InputField nextField;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (GetComponent<InputField> ().isFocused && Input.GetKeyDown (KeyCode.Tab)) {
nextField.ActivateInputField ();
}
}
公共类TabToNextController:monobhavior{
公共输入字段nextField;
//用于初始化
无效开始(){
}
//每帧调用一次更新
无效更新(){
if(GetComponent().isFocused&&Input.GetKeyDown(KeyCode.Tab)){
nextField.ActivateInputField();
}
}
只需将此脚本添加到要从中设置选项卡的项目中。然后在GUI下拉菜单中,将要设置选项卡的输入字段作为“nextField”。这是我的解决方案,非常类似
public class TabToNextController : MonoBehaviour {
public InputField nextField;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (GetComponent<InputField> ().isFocused && Input.GetKeyDown (KeyCode.Tab)) {
nextField.ActivateInputField ();
}
}
公共类TabToNextController:monobhavior{
公共输入字段nextField;
//用于初始化
无效开始(){
}
//每帧调用一次更新
无效更新(){
if(GetComponent().isFocused&&Input.GetKeyDown(KeyCode.Tab)){
nextField.ActivateInputField();
}
}
只需将此脚本添加到要从中设置选项卡的项目中,然后在GUI下拉菜单中,将要设置选项卡的输入字段作为“nextField”.我最近遇到了这个问题,一想到没有选项卡功能,我就发疯了。我以前从未注意到它。到目前为止,一个简单的解决方案似乎是使用UI元素从中继承的可选
类
我用最常见的类型(对我来说)测试了这一点——输入字段、按钮和下拉列表
- 它会按预期的那样将每个标签都粘贴到它们上
- 使用空格键单击按钮
- 使用空格键打开下拉列表
如果希望完整的web样式控件(如箭头键)调用下拉列表,则需要编写该代码
public class TabToNextController : MonoBehaviour, IUpdateSelectedHandler
{
public Selectable nextField;
public void OnUpdateSelected(BaseEventData data)
{
if (Input.GetKeyDown(KeyCode.Tab))
nextField.Select();
}
}
干杯我最近遇到了这个问题,想到没有选项卡功能让我发疯。我以前从来没有注意到过。到目前为止,一个简单的解决方案似乎是使用UI元素继承自的可选
类
我用最常见的类型(对我来说)测试了这一点——输入字段、按钮和下拉列表
- 它会按预期的那样将每个标签都粘贴到它们上
- 使用空格键单击按钮
- 使用空格键打开下拉列表
如果希望完整的web样式控件(如箭头键)调用下拉列表,则需要编写该代码
public class TabToNextController : MonoBehaviour, IUpdateSelectedHandler
{
public Selectable nextField;
public void OnUpdateSelected(BaseEventData data)
{
if (Input.GetKeyDown(KeyCode.Tab))
nextField.Select();
}
}
干杯这对我来说很有用:
public Selectable next; //if you want the cursor to start at a specific field
//you can set this in the inspector to any input field
private EventSystem system;
private int tabIndex; //to keep track of cursor
void Start()
{
system = EventSystem.current;// EventSystemManager.currentSystem;
//if you don't want to set next in the inspector, you can do it here
//next = Selectable.allSelectables[0];
tabIndex = 0;
next.Select(); //make sure to do this!
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
if (tabIndex >= Selectable.allSelectables.Count)
{
next = Selectable.allSelectables[0];
tabIndex = 0;
}
else
{
//if nothing is selected then system.currentSelectedGameObject
//will throw a nullPointer Exception
next = system.currentSelectedGameObject.GetComponent<Selectable>()
.FindSelectableOnDown();
tabIndex++;
}
Debug.Log(next);
if (next != null)
{
InputField inputfield = next.GetComponent<InputField>();
if (inputfield != null)
inputfield.OnPointerClick(new PointerEventData(system)); //if it's an input field, also set the text caret
system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
}
}
}
public selected next;//如果希望光标从特定字段开始
//您可以在inspector中将其设置为任何输入字段
私人事件系统;
private int tabIndex;//用于跟踪游标
void Start()
{
system=EventSystem.current;//EventSystemManager.currentSystem;
//如果不想在inspector中设置下一个,可以在此处设置
//下一步=可选择。所有可选择项[0];
tabIndex=0;
next.Select();//确保执行此操作!
}
无效更新()
{
if(Input.GetKeyDown(KeyCode.Tab))
{
if(tabIndex>=Selectable.allSelectables.Count)
{
下一步=可选择。所有可选择项[0];
tabIndex=0;
}
其他的
{
//如果未选择任何内容,则system.currentSelectedGameObject
//将引发nullPointer异常
next=system.currentSelectedGameObject.GetComponent()
.FindSelectableOnDown();
tabIndex++;
}
Debug.Log(下一步);
如果(下一步!=null)
{
InputField InputField=next.GetComponent();
如果(输入字段!=null)
OnPointerClick(newpointereventdata(system));//如果是输入字段,还可以设置文本插入符号
system.SetSelectedGameObject(next.gameObject,新BaseEventData(system));
}
}
}
这就是我的工作原理:
public Selectable next; //if you want the cursor to start at a specific field
//you can set this in the inspector to any input field
private EventSystem system;
private int tabIndex; //to keep track of cursor
void Start()
{
system = EventSystem.current;// EventSystemManager.currentSystem;
//if you don't want to set next in the inspector, you can do it here
//next = Selectable.allSelectables[0];
tabIndex = 0;
next.Select(); //make sure to do this!
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
if (tabIndex >= Selectable.allSelectables.Count)
{
next = Selectable.allSelectables[0];
tabIndex = 0;
}
else
{
//if nothing is selected then system.currentSelectedGameObject
//will throw a nullPointer Exception
next = system.currentSelectedGameObject.GetComponent<Selectable>()
.FindSelectableOnDown();
tabIndex++;
}
Debug.Log(next);
if (next != null)
{
InputField inputfield = next.GetComponent<InputField>();
if (inputfield != null)
inputfield.OnPointerClick(new PointerEventData(system)); //if it's an input field, also set the text caret
system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
}
}
}
public selected next;//如果希望光标从特定字段开始
//您可以在inspector中将其设置为任何输入字段
私人事件系统;
private int tabIndex;//用于跟踪游标
void Start()
{
system=EventSystem.current;//EventSystemManager.currentSystem;
//如果不想在inspector中设置下一个,可以在此处设置
//下一步=可选择。所有可选择项[0];
tabIndex=0;
next.Select();//确保执行此操作!
}
无效更新()
{
if(Input.GetKeyDown(KeyCode.Tab))
{
if(tabIndex>=Selectable.allSelectables.Count)
{
下一步=可选择。所有可选择项[0];
tabIndex=0;
}
其他的
{
//如果未选择任何内容,则system.currentSelectedGameObject
//w