Unity自定义输入方式(通过可视化面板进行输入)

2023-11-06

去年某个时间段做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;
    // Start is called before the first frame update
    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++)
        {
            //此处为预制,也可以直接new一个空物体在添加组件,这里因为觉得太丑添加了个背景,然后做了个预制,虽然还是很丑
            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);
    }

    // Update is called once per frame
    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;
    // Start is called before the first frame update
    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++)
        {
            //此处为预制,也可以直接new一个空物体在添加组件,这里因为觉得太丑添加了个背景,然后做了个预制,虽然还是很丑
            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);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
[Serializable]
public class PersonData 
{
    /// <summary>
    /// 工号
    /// </summary>
    public int Id { get; set; }
    /// <summary>
    /// 姓名
    /// </summary>
    public string Name { get; set; }
    /// <summary>
    /// 年龄
    /// </summary>
    public int Age { get; set; }
    /// <summary>
    /// 职位
    /// </summary>
    public string Occupation { get; set; }
    /// <summary>
    /// 所在部门
    /// </summary>
    public string Department { get; set; }
    /// <summary>
    /// 个人荣誉
    /// </summary>
    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:最后效果,大致原理差不多,要更好点的就是完善下输入的资源以及美化面板!在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Unity自定义输入方式(通过可视化面板进行输入) 的相关文章

  • 注销租约抛出 InvalidOperationException

    我有一个使用插件的应用程序 我在另一个应用程序域中加载插件 我使用 RemoteHandle 类http www pocketsilicon com post Things That Make My Life Hell Part 1 App
  • 如何在 .NET Framework 2.0 中模拟“Func<(Of <(TResult>)>) 委托”?

    我尝试使用这个类代码项目文章 http www codeproject com KB threads AsyncVar aspx在 VB NET 和 NET Framework 2 0 中 除了这一行之外 所有内容似乎都可以编译Privat
  • 如何将非静态类成员“std::bind”绑定到 Win32 回调函数“WNDPROC”?

    我正在尝试将非静态类成员绑定到标准WNDPROC http msdn microsoft com en us library ms633573 aspx功能 我知道我可以通过将类成员设为静态来简单地做到这一点 但是 作为一名 C 11 ST
  • 计算 Richtextbox 中所有单词的最有效方法是什么?

    我正在编写一个文本编辑器 需要提供实时字数统计 现在我正在使用这个扩展方法 public static int WordCount this string s s s TrimEnd if String IsNullOrEmpty s re
  • 在 DataView 的 RowFilter 中选择 DISTINCT

    我试图根据与另一个表的关系缩小 DataView 中的行范围 我使用的 RowFilter 如下 dv new DataView myDS myTable id IN SELECT DISTINCT parentID FROM myOthe
  • C中的malloc内存分配方案

    我在 C 中尝试使用 malloc 发现 malloc 在分配了一些内存后浪费了一些空间 下面是我用来测试 malloc 的一段代码 include
  • 在 C 中匹配二进制模式

    我目前正在开发一个 C 程序 需要解析一些定制的数据结构 幸运的是我知道它们是如何构造的 但是我不确定如何在 C 中实现我的解析器 每个结构的长度都是 32 位 并且每个结构都可以通过其二进制签名来识别 举个例子 有两个我感兴趣的特定结构
  • 使用 LINQ2SQL 在 ASP.NET MVC 中的各种模型存储库之间共享数据上下文

    我的应用程序中有 2 个存储库 每个存储库都有自己的数据上下文对象 最终结果是我尝试将从一个存储库检索到的对象附加到从另一个存储库检索到的对象 这会导致异常 Use 构造函数注入将 DataContext 注入每个存储库 public cl
  • 为什么 Google 测试会出现段错误?

    我是 Google Test 的新手 正在尝试提供的示例 我的问题是 当我引入失败并设置GTEST BREAK ON FAILURE 1 或使用命令行选项 GTest 将出现段错误 我正在考虑这个例子 https code google c
  • 使用接口有什么好处?

    使用接口有什么用 我听说它用来代替多重继承 并且还可以用它来完成数据隐藏 还有其他优点吗 哪些地方使用了接口 程序员如何识别需要该接口 有什么区别explicit interface implementation and implicit
  • 在 C 中初始化变量

    我知道有时如果你不初始化int 如果打印整数 您将得到一个随机数 但将所有内容初始化为零似乎有点愚蠢 我问这个问题是因为我正在评论我的 C 项目 而且我对缩进非常直接 并且它可以完全编译 90 90 谢谢 Stackoverflow 但我想
  • 我可以使用 moq Mock 来模拟类而不是接口吗?

    正在经历https github com Moq moq4 wiki Quickstart https github com Moq moq4 wiki Quickstart 我看到它 Mock 一个接口 我的遗留代码中有一个没有接口的类
  • 如何在 32 位或 64 位配置中以编程方式运行任何 CPU .NET 可执行文件?

    我有一个可在 32 位和 64 位处理器上运行的 C 应用程序 我试图枚举给定系统上所有进程的模块 当尝试从 64 位应用程序枚举 32 位进程模块时 这会出现问题 Windows 或 NET 禁止它 我认为如果我可以从应用程序内部重新启动
  • 使用自定义堆的类似 malloc 的函数

    如果我希望使用自定义预分配堆构造类似 malloc 的功能 那么 C 中最好的方法是什么 我的具体问题是 我有一个可映射 类似内存 的设备 已将其放入我的地址空间中 但我需要获得一种更灵活的方式来使用该内存来存储将随着时间的推移分配和释放的
  • Azure 辅助角色“请求输入之一超出范围”的内部异常。

    我在辅助角色中调用 CloudTableClient CreateTableIfNotExist 方法 但收到一个异常 其中包含 请求输入之一超出范围 的内部异常 我做了一些研究 发现这是由于将表命名为非法表名引起的 但是 我尝试为我的表命
  • C# 中的合并运算符?

    我想我记得看到过类似的东西 三元运算符 http msdn microsoft com en us library ty67wk28 28VS 80 29 aspx在 C 中 它只有两部分 如果变量值不为空 则返回变量值 如果为空 则返回默
  • AES 128 CBC 蒙特卡罗测试

    我正在 AES 128 CBC 上执行 MCT 如中所述http csrc nist gov groups STM cavp documents aes AESAVS pdf http csrc nist gov groups STM ca
  • 使用管道时,如果子进程数量大于处理器数量,进程是否会被阻塞?

    当子进程数量很大时 我的程序停止运行 我不知道问题是什么 但我猜子进程在运行时以某种方式被阻止 下面是该程序的主要工作流程 void function int process num int i initial variables for
  • 将 MQTTNet 服务器与 MQTT.js 客户端结合使用

    我已经启动了一个 MQTT 服务器 就像this https github com chkr1011 MQTTnet tree master例子 该代码托管在 ASP Net Core 2 0 应用程序中 但我尝试过控制台应用程序 但没有成
  • 如何在非控制台应用程序中查看 cout 输出?

    输出到调试窗口似乎相当繁琐 我在哪里可以找到cout如果我正在编写非控制台信息 则输出 Like double i a b cout lt lt b lt lt endl I want to check out whether b is z

随机推荐