IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Unity接入罗技G29方向盘,通过SDK获取按键信息 -> 正文阅读

[游戏开发]Unity接入罗技G29方向盘,通过SDK获取按键信息

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;

    // Use this for initialization
    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);
    }

    // Update is called once per frame
    void Update()
    {
        //All the test functions are called on the first device plugged in(index = 0)
        if (LogitechGSDK.LogiUpdate() && LogitechGSDK.LogiIsConnected(0))
        {
           
            //CONTROLLER PROPERTIES
            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";

            //CONTROLLER STATE
            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;
            }

            //Button status :

            buttonStatus = "Button pressed : \n\n";
            for (int i = 0; i < 128; i++)
            {
                if (rec.rgbButtons[i] == 128)
                {
                    buttonStatus += "Button " + i + " pressed\n";
                }

            }

            /* THIS AXIS ARE NEVER REPORTED BY LOGITECH CONTROLLERS 
             * 
             * actualState += "x-axis velocity :" + rec.lVX + "\n";
             * actualState += "y-axis velocity :" + rec.lVY + "\n";
             * actualState += "z-axis velocity :" + rec.lVZ + "\n";
             * actualState += "x-axis angular velocity :" + rec.lVRx + "\n";
             * actualState += "y-axis angular velocity :" + rec.lVRy + "\n";
             * actualState += "z-axis angular velocity :" + rec.lVRz + "\n";
             * actualState += "extra axes velocities 1 :" + rec.rglVSlider[0] + "\n";
             * actualState += "extra axes velocities 2 :" + rec.rglVSlider[1] + "\n";
             * actualState += "x-axis acceleration :" + rec.lAX + "\n";
             * actualState += "y-axis acceleration :" + rec.lAY + "\n";
             * actualState += "z-axis acceleration :" + rec.lAZ + "\n";
             * actualState += "x-axis angular acceleration :" + rec.lARx + "\n";
             * actualState += "y-axis angular acceleration :" + rec.lARy + "\n";
             * actualState += "z-axis angular acceleration :" + rec.lARz + "\n";
             * actualState += "extra axes accelerations 1 :" + rec.rglASlider[0] + "\n";
             * actualState += "extra axes accelerations 2 :" + rec.rglASlider[1] + "\n";
             * actualState += "x-axis force :" + rec.lFX + "\n";
             * actualState += "y-axis force :" + rec.lFY + "\n";
             * actualState += "z-axis force :" + rec.lFZ + "\n";
             * actualState += "x-axis torque :" + rec.lFRx + "\n";
             * actualState += "y-axis torque :" + rec.lFRy + "\n";
             * actualState += "z-axis torque :" + rec.lFRz + "\n";
             * actualState += "extra axes forces 1 :" + rec.rglFSlider[0] + "\n";
             * actualState += "extra axes forces 2 :" + rec.rglFSlider[1] + "\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

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-08-14 14:25:30  更:2021-08-14 14:28:32 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 17:27:02-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码