在输入用户名密码的时候,我们经常喜欢按下tab键来切换输入框,但是unity里的UGUI没有直接的脚本用,所以只能自己写一个了,
首先新建一个脚本,命名为InputNavigator.cs
内容如下:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InputNavigator : MonoBehaviour
{
private EventSystem es;
public InputField[] IfArray;
public int index = 0;
void Start() {
es = EventSystem.current;
es.SetSelectedGameObject(IfArray[index].gameObject,new BaseEventData(es));
}
void Update() {
if (Input.GetKeyDown(KeyCode.Tab)) {
index++;
if (index >= IfArray.Length) {
index = 0;
}
es.SetSelectedGameObject(IfArray[index].gameObject, new BaseEventData(es));
}
}
}
然后创建几个输入框:
为每一个输入框加上一个脚本:GetIndex.cs
using UnityEngine;
using UnityEngine.EventSystems;
public class GetIndex : MonoBehaviour,ISelectHandler {
public int index;
public void OnSelect(BaseEventData eventData)
{
this.transform.parent.GetComponent().index = index;
}
}
然后按照顺序将index赋值:0 1 2 3 4…….
到此为止,我们的功能就做完了 ,大家可以运行程序,试试看啦!!!!!!
本文介绍如何在Unity中使用UGUI实现通过Tab键在多个输入框间切换的功能。作者提供了一个名为InputNavigator的脚本,用于管理输入框间的切换,并配合GetIndex脚本完成输入框的选择。
3856

被折叠的 条评论
为什么被折叠?



