去年某个时间段做VR功能预研,利用Pico一体机做开发,当时通过Unity内置Inputfield组件实现外部输入,该组件貌似是调用系统内置输入方式,Pico一体机那个时候的输入方式简则是蛋疼得很,因为一调用该输入法就是双目模式,这就算了,更难受的是输入法贴到眼睛上了,根本不好使,偶尔还有穿透。当时就有了想法自定义一套输入方式,奈何一直没时间,最近遇到一个项目必须要用到自定义的,所以就简单的搞了一套将就用着。上代码先: 1:输入面板代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.UI.GridLayoutGroup;
[RequireComponent(typeof(GridLayoutGroup))]
public class PanelInput : MonoBehaviour
{
public delegate void DlAction(string input);
public static event DlAction e_InputAction;
public List<string> zimuli = new List<string>();
public Font toChange;
void Start()
{
GridLayoutGroup group= GetComponent<GridLayoutGroup>();
group.padding = new RectOffset(50, 50, 50, 50);
group.spacing = new Vector2(20,20);
group.constraint = Constraint.FixedRowCount;
group.constraintCount = 4;
for (char i = 'A'; i <= 'Z'; i++)
zimuli.Add(i.ToString());
for (int i = 0; i < zimuli.Count; i++)
{
GameObject item = Instantiate(Resources.Load<GameObject>("letterItem"),transform);
item.name = zimuli[i].ToString();
item.GetComponentInChildren<Text>().text = zimuli[i];
item.GetComponentInChildren<Text>().font = toChange;
item.GetComponentInChildren<Text>().color = Color.black;
item.AddComponent<Button>().onClick.AddListener(() => { OnClickBtn(item.name); }) ;
}
}
private void OnClickBtn(string name)
{
gameObject.SetActive(false);
e_InputAction(name);
}
void Update()
{
}
}
2:查询面板代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.UI.GridLayoutGroup;
[RequireComponent(typeof(GridLayoutGroup))]
public class PanelInput : MonoBehaviour
{
public delegate void DlAction(string input);
public static event DlAction e_InputAction;
public List<string> zimuli = new List<string>();
public Font toChange;
void Start()
{
GridLayoutGroup group= GetComponent<GridLayoutGroup>();
group.padding = new RectOffset(50, 50, 50, 50);
group.spacing = new Vector2(20,20);
group.constraint = Constraint.FixedRowCount;
group.constraintCount = 4;
for (char i = 'A'; i <= 'Z'; i++)
zimuli.Add(i.ToString());
for (int i = 0; i < zimuli.Count; i++)
{
GameObject item = Instantiate(Resources.Load<GameObject>("letterItem"),transform);
item.name = zimuli[i].ToString();
item.GetComponentInChildren<Text>().text = zimuli[i];
item.GetComponentInChildren<Text>().font = toChange;
item.GetComponentInChildren<Text>().color = Color.black;
item.AddComponent<Button>().onClick.AddListener(() => { OnClickBtn(item.name); }) ;
}
}
private void OnClickBtn(string name)
{
gameObject.SetActive(false);
e_InputAction(name);
}
void Update()
{
}
}
[Serializable]
public class PersonData
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Occupation { get; set; }
public string Department { get; set; }
public string Honor { get; set; }
public PersonData(int id ,string name ,int age,string occ,string det,string honnor)
{
Id = id;
Name = name;
Age = age;
Occupation = occ;
Department = det;
Honor = honnor;
}
}
3:场景面板设置:然后对应的节点挂上上面的两个脚本; 4:最后效果,大致原理差不多,要更好点的就是完善下输入的资源以及美化面板!
|