上周我收到了我的 Gear VR,发现 Oculus Home 中大多数更好的应用都需要游戏手柄。因此,我有了一个疯狂的想法,即尝试使用连接到我的 PC 的四个 Xbox 360 控制器中的一个,通过 WiFi 向 Gear Vr 发送命令,作为假游戏手柄。
现在我离真正的程序员还很远,我的大部分编码经验都来自于自学如何使用统一,但我已经取得了一些进展(尽管是一个 hacky)——我统一编写了一个脚本来读取我的输入游戏手柄并通过无线 ABD 终端发送到应用程序。
using UnityEngine;
using System;
using System.Collections;
using System.Diagnostics;
using XInputDotNetPure;
using System.IO;
public class Datagetter : MonoBehaviour {
public Process process;
public StreamWriter mySW;
public StreamReader mySR;
public bool left = false;
public bool right = false;
public bool up = false;
public bool down = false;
public bool start = false;
public bool xbtn = false;
// Use this for initialization
void Start () {
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WorkingDirectory = "C:\\Windows\\System32";
process.StartInfo.FileName = "cmd.exe";
// Redirects the standard input so that commands can be sent to the shell.
process.StartInfo.RedirectStandardInput = true;
// Runs the specified command and exits the shell immediately.
//process.StartInfo.Arguments = @"/c ""dir""";
process.Start();
mySW = process.StandardInput;
mySW.WriteLine ("cd /d C:\\Users\\user");
mySW.WriteLine ("adb tcpip 5555");/////////////
mySW.WriteLine ("adb connect 10.0.0.11");/////////////////
}
IEnumerator timer(){
yield return new WaitForSeconds (3f);
connect ();
}
public void connect(){
mySW.WriteLine ("adb connect 10.0.0.3");
}
// Update is called once per frame
void Update () {
if (GamePad.GetState (PlayerIndex.One).DPad.Left == ButtonState.Pressed) {
if(!left){
left = true;
mySW.WriteLine ("adb shell input keyevent 21");
mySW.Flush ();
}
}
if (GamePad.GetState (PlayerIndex.One).Buttons.X == ButtonState.Pressed) {
if (!right) {
xbtn = true;
mySW.WriteLine ("adb shell input keyevent 188");
mySW.Flush ();
}
}
if (GamePad.GetState (PlayerIndex.One).DPad.Right == ButtonState.Pressed) {
if (!right) {
right = true;
mySW.WriteLine ("adb shell input keyevent 22");
mySW.Flush ();
}
}
if (GamePad.GetState (PlayerIndex.One).DPad.Up == ButtonState.Pressed) {
if (!up) {
up = true;
mySW.WriteLine ("adb shell input keyevent 19");
mySW.Flush ();
}
// UnityEngine.Debug.Log (mySR.ReadLine());
}
if (GamePad.GetState (PlayerIndex.One).DPad.Down == ButtonState.Pressed) {
if (!down) {
down = true;
mySW.WriteLine ("adb shell input keyevent 20");
mySW.Flush ();
}
}
if (GamePad.GetState (PlayerIndex.One).Buttons.Start == ButtonState.Pressed) {
if (!start) {
start = true;
mySW.WriteLine ("control");
mySW.Flush ();
}
}
if (GamePad.GetState (PlayerIndex.One).DPad.Down == ButtonState.Released) {
if (down) {
down = false;
}
}
if (GamePad.GetState (PlayerIndex.One).DPad.Up == ButtonState.Released) {
if (up) {
up = false;
}
}
if (GamePad.GetState (PlayerIndex.One).DPad.Right == ButtonState.Released) {
if (right) {
right = false;
}
}
if (GamePad.GetState (PlayerIndex.One).DPad.Left == ButtonState.Released) {
if (left) {
left = false;
}
}
if (GamePad.GetState (PlayerIndex.One).Buttons.Start == ButtonState.Released) {
if (start) {
start = false;
}
}
}
}
使用此代码,我可以使用游戏手柄在非VR应用程序和系统菜单中移动,但由于某种原因,所有keyEvents都不能在gearVr中工作,到目前为止我发现的唯一例外是keyevent 3,它模拟了返回键。
我知道这段代码非常糟糕,一旦我掌握了基础知识,我会尝试正确地重写它,但现在我非常卡住。任何人都知道如何解决这个问题?对于那些不想用价格过高、不太舒服的游戏手柄替换当前游戏手柄的 gearVR 用户来说,这可能是一个非常有用的解决方案。