Unity接入罗技G29方向盘,通过SDK获取按键信息
说明: ??最近需要做一个外设汽车驾驶相关的软件,采用的外设 罗技G29方向盘,项目中遇到的问题这里记录一下。
项目准备: ??1.下载罗技的SDK ??2.下载罗技游戏软件 ??3.Unity商店中导入Logitech Gaming SDK
遇到的问题: ??1.Unity商店中导入的SDK运行会报错,将项目准备中步骤一下载的SDK 路径:Lib\x64\LogitechSteeringWheelLib.lib 复制到Logitech SDK文件夹下 ??2.插件没有问题,运行并不能获取方向盘的按键信息,这个情况我的电脑重启了好多次,也补充了一些基础的运行环境,一直就是获取不到方向盘的按键信息,最后我重装了一下罗技游戏软件,重启运行发现可以用了。
最后: ??将插件中的LogitechSteeringWheel.cs脚本挂载到场景中运行:  相关方向盘和脚踏板的监听代码(我这没有挡位外设)
using UnityEngine;
using System.Collections;
using System.Text;
public class LogitechSteeringWheel : MonoBehaviour
{
LogitechGSDK.LogiControllerPropertiesData properties;
private string actualState;
private string activeForces;
private string propertiesEdit;
private string buttonStatus;
private string forcesLabel;
string[] activeForceAndEffect;
void Start()
{
activeForces = "";
propertiesEdit = "";
actualState = "";
buttonStatus = "";
forcesLabel = "Press the following keys to activate forces and effects on the steering wheel / gaming controller \n";
forcesLabel += "Spring force : S\n";
forcesLabel += "Constant force : C\n";
forcesLabel += "Damper force : D\n";
forcesLabel += "Side collision : Left or Right Arrow\n";
forcesLabel += "Front collision : Up arrow\n";
forcesLabel += "Dirt road effect : I\n";
forcesLabel += "Bumpy road effect : B\n";
forcesLabel += "Slippery road effect : L\n";
forcesLabel += "Surface effect : U\n";
forcesLabel += "Car Airborne effect : A\n";
forcesLabel += "Soft Stop Force : O\n";
forcesLabel += "Set example controller properties : PageUp\n";
forcesLabel += "Play Leds : P\n";
activeForceAndEffect = new string[9];
Debug.Log("SteeringInit:" + LogitechGSDK.LogiSteeringInitialize(false));
}
void OnApplicationQuit()
{
Debug.Log("SteeringShutdown:" + LogitechGSDK.LogiSteeringShutdown());
}
void OnGUI()
{
activeForces = GUI.TextArea(new Rect(10, 10, 180, 200), activeForces, 400);
propertiesEdit = GUI.TextArea(new Rect(200, 10, 200, 200), propertiesEdit, 400);
actualState = GUI.TextArea(new Rect(410, 10, 300, 200), actualState, 1000);
buttonStatus = GUI.TextArea(new Rect(720, 10, 300, 200), buttonStatus, 1000);
GUI.Label(new Rect(10, 400, 800, 400), forcesLabel);
}
void Update()
{
if (LogitechGSDK.LogiUpdate() && LogitechGSDK.LogiIsConnected(0))
{
StringBuilder deviceName = new StringBuilder(256);
LogitechGSDK.LogiGetFriendlyProductName(0, deviceName, 256);
propertiesEdit = "Current Controller : " + deviceName + "\n";
propertiesEdit += "Current controller properties : \n\n";
LogitechGSDK.LogiControllerPropertiesData actualProperties = new LogitechGSDK.LogiControllerPropertiesData();
LogitechGSDK.LogiGetCurrentControllerProperties(0, ref actualProperties);
propertiesEdit += "forceEnable = " + actualProperties.forceEnable + "\n";
propertiesEdit += "overallGain = " + actualProperties.overallGain + "\n";
propertiesEdit += "springGain = " + actualProperties.springGain + "\n";
propertiesEdit += "damperGain = " + actualProperties.damperGain + "\n";
propertiesEdit += "defaultSpringEnabled = " + actualProperties.defaultSpringEnabled + "\n";
propertiesEdit += "combinePedals = " + actualProperties.combinePedals + "\n";
propertiesEdit += "wheelRange = " + actualProperties.wheelRange + "\n";
propertiesEdit += "gameSettingsEnabled = " + actualProperties.gameSettingsEnabled + "\n";
propertiesEdit += "allowGameSettings = " + actualProperties.allowGameSettings + "\n";
actualState = "Steering wheel current state : \n\n";
LogitechGSDK.DIJOYSTATE2ENGINES rec;
rec = LogitechGSDK.LogiGetStateUnity(0);
actualState += "x-axis position :" + rec.lX + "\n";
actualState += "y-axis position :" + rec.lY + "\n";
actualState += "z-axis position :" + rec.lZ + "\n";
actualState += "x-axis rotation :" + rec.lRx + "\n";
actualState += "y-axis rotation :" + rec.lRy + "\n";
actualState += "z-axis rotation :" + rec.lRz + "\n";
actualState += "extra axes positions 1 :" + rec.rglSlider[0] + "\n";
actualState += "extra axes positions 2 :" + rec.rglSlider[1] + "\n";
switch (rec.rgdwPOV[0])
{
case (0): actualState += "POV : UP\n"; break;
case (4500): actualState += "POV : UP-RIGHT\n"; break;
case (9000): actualState += "POV : RIGHT\n"; break;
case (13500): actualState += "POV : DOWN-RIGHT\n"; break;
case (18000): actualState += "POV : DOWN\n"; break;
case (22500): actualState += "POV : DOWN-LEFT\n"; break;
case (27000): actualState += "POV : LEFT\n"; break;
case (31500): actualState += "POV : UP-LEFT\n"; break;
default: actualState += "POV : CENTER\n"; break;
}
buttonStatus = "Button pressed : \n\n";
for (int i = 0; i < 128; i++)
{
if (rec.rgbButtons[i] == 128)
{
buttonStatus += "Button " + i + " pressed\n";
}
}
int shifterTipe = LogitechGSDK.LogiGetShifterMode(0);
string shifterString = "";
if (shifterTipe == 1) shifterString = "Gated";
else if (shifterTipe == 0) shifterString = "Sequential";
else shifterString = "Unknown";
actualState += "\nSHIFTER MODE:" + shifterString;
}
else if (!LogitechGSDK.LogiIsConnected(0))
{
actualState = "PLEASE PLUG IN A STEERING WHEEL OR A FORCE FEEDBACK CONTROLLER";
}
else
{
actualState = "THIS WINDOW NEEDS TO BE IN FOREGROUND IN ORDER FOR THE SDK TO WORK PROPERLY";
}
}
}
参考: 链接1
|