补充(25/5/19):在后续开发中是有发现steamVR里是有带的虚拟键盘,相关的调用可以看案例里面的demo。所以其实也可以不需要使用我的,但如果想作为定制化或使用其他都VR开发,就可以参考一下我的这个博文
由于steamVR没有提供键盘输入的内容,我看了很多,结果发现都挺麻烦的,索性直接自己手搓了一个如图:
其实也简单,首先就是用按钮一个个把UI做出来,然后写对应的一些脚本就好
感兴趣的可以看我后面写的代码(下载链接也在后面)。
下面是一些使用说明:
首先要在场景中设置好WorldKeyboard的物体
重要:然后在需要点击输入的InputField 中添加Event Trigger点击事件,然后按照如下添加事件调用
代码(比较简单,就不注释啦):
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WorldKeyboard : MonoBehaviour
{
private Transform Key1;
private Transform Key2;
private Transform Keys;
private InputField nowInputField;
private InputField currentInputField;
private bool isLock = false;
private void Awake()
{
Key1 = transform.Find("Key1");
Key2 = transform.Find("Key2");
Keys = transform.Find("Keys");
}
public void OnNowInputField(InputField inputField)
{
nowInputField = inputField;
}
public void OnCapsLockDown()
{
isLock = !isLock;
Key1.gameObject.SetActive(isLock);
Key2.gameObject.SetActive(!isLock);
}
public void OnEnterDown()
{
gameObject.SetActive(false);
nowInputField.DeactivateInputField();
nowInputField = null;
}
public void OnBackspaceDown()
{
if (nowInputField != null)
{
nowInputField.text = RemoveLastCharacter(nowInputField.text);
}
}
public string RemoveLastCharacter(string input)
{
if (string.IsNullOrEmpty(input))
{
return input; // 如果字符串为空或为null,直接返回
}
return input.Substring(0, input.Length - 1); // 删除最后一个字符
}
public void OnkeyDown(string keyStr)
{
if(nowInputField != null)
{
nowInputField.text += keyStr;
}
}
}
以下是链接分享:
我用夸克网盘分享了「Virtual Keyboard」,点击链接即可保存。打开「夸克APP」,无需下载在线播放视频,畅享原画5倍速,支持电视投屏。
链接:https://pan.quark.cn/s/e5ec871cc16b
提取码:gzhW