Unity3D 罗技G29开发笔记

这些博客可能对你有帮助:
https://blog.csdn.net/m0_38143858/article/details/79855923
https://blog.csdn.net/baozaodedianxiaoer/article/details/80134658
https://blog.csdn.net/wuyf88/article/details/68942196


一、准备工作

1、在罗技官网下载最新的罗技游戏软件。https://www.logitechg.com/en-us/partnerdeveloperlab/sdk-resource-list/steering-wheel-sdk.html
2、在assetstore中下载Logitech Gaming SDK,导入 Logitech SDK 。把官网下载的LogitechSteeringWheelEnginesWrapper.dll替换Unity3D中的。

二、使用官方Demo,熟悉各种输入

直接把Script Sample中的LogitechSteeringWheel.cs挂载空物体中,运行。

三、 注意项

(1)初始化连接问题
  • 使用LogitechGSDK.cs脚本需要先初始化,LogiSteeringInitialize();
  • 要在代码中调用来自罗技SDK的任何函数,请导入LogitechGSDK.cs脚本并静态调用函数。
  • 函数LogiSteeringInitialize()会试图获得你的应用程序/游戏主窗口句柄。它可能会失败,因为当你调用该函数时的主窗口可能不会在最前面的界面。
  • 如果LogiSteeringInitialize()返回false,初始化将会接受任何尝试下一个LogiUpdate()函数的调用。当窗口在最前界面时和SDK可以初始化,LogiUpdate()将返回true。
  • 【但我曾经出现过一个状况是:在Start()里LogiSteeringInitialize()返回是false,然后就算当前窗口已经是最前界面了,Update()里LogiUpdate()还是一直返回false,后来试着在Start()里加一个循环,一直循环LogiSteeringInitialize()直到返回true就完美解决问题了。】
(2)获取不到方向盘旋转的数据(结构体DIJOYSTATE2ENGINES中的lX)
  • 如果连接成功(函数LogiSteeringInitialize和LogiUpdate,返回true),但只能获取方向盘的按钮,获取不了方向盘旋转的数据可能是线没插紧的原因。
(3)方向盘、脚踏板、档位器:当没有触碰到的时候,好像是不会收到数据?我判定不到连接状态。

我用了下LogiIsDeviceConnected()去获取方向盘、脚踏板、档位器的状态,但只有LOGI_DEVICE_TYPE_WHEEL返回true,其他(LOGI_DEVICE_TYPE_JOYSTICK、LOGI_DEVICE_TYPE_GAMEPAD、LOGI_DEVICE_TYPE_OTHER)都是返回false,不知道为什么获取不到连接状态,如果知道的,望告知 QAQ

四、使用流程图

在这里插入图片描述

五、模板

public class LogitechControllerController : MonoBehaviour {
	private static LogitechControllerController instance = null;

    public static LogitechControllerController GetInstance()
    {
        if (instance == null)
        {
           instance = new GameObject("LogitechControllerCtrl").AddComponent<LogitechControllerController>();
			//1、初始化,看需求初始化。
			bool flag = false;
            while (flag == false)
            {
            	//Debug.Log("SteeringInit:" + LogitechGSDK.LogiLedInit());
				//Debug.Log("SteeringInit2:" + LogitechGSDK.LogiSteeringInitialize(false));
                flag = LogitechGSDK.LogiSteeringInitialize(false);
                Debug.Log("SteeringInit:" + flag);
            }
	 	}
        return instance;
    }
	
	//2、更新
	private void Update()
    {
            if (LogitechGSDK.LogiUpdate() && LogitechGSDK.LogiIsConnected(0))
            {
            	//。。。。。。
            }
    }
    
	//3、退出前
	void OnApplicationQuit()
    {
        Debug.Log("SteeringShutdown:" + LogitechGSDK.LogiSteeringShutdown());
    }
}

六、记录

  1. 方向盘:lX
    不触碰(初始):0,触碰后左扭到尽:-32768,右扭到尽:32767
  2. 油门: lY
    不触碰(初始):0,不踩:32767,踩尽:-32768
  3. 刹车: lRz
    不触碰(初始):0,不踩:32767,踩尽:-32768

七、官方文档中较常用的一些函数

【因为我不肯定机翻的误差多大,但大部分都是能看懂的,不过最好还是一边官方文档一边我这文哈哈哈】
1、LogiSteeringInitialize 主窗口初始化

  • bool LogiSteeringInitialize(bool ignoreXInputControllers);
    ignoreXInputControllers:忽略X输入控制器

  • 【只允许一个用户在任何给定时间来控制,如果两个应用程序同时初始化SDK设备,只有第一个获取成功,第二个应用程序获取失败。】

  • 函数LogiSteeringInitialize()会试图获得你的应用程序/游戏主窗口句柄。它可能会失败,因为当你调用该函数时的主窗口可能不会在最前面的界面。

  • 如果LogiSteeringInitialize()返回false,初始化将会接受任何尝试下一个LogiUpdate()函数的调用。当窗口在最前界面时和SDK可以初始化,LogiUpdate()将返回true。

2、获取设备数据前,要判断设备是否连接和更新【LogiUpdate()和LogiIsConnected(0)】

  • (1)LogiUpdate 主窗口设备更新
    Bool LogiUpdate()
    该LogiUpdate()查找主窗口处理程序,如果已经发现力和控制器 保持连接最新的。它被称为应用程序的每一帧。
    返回false,可能是LogiSteeringInitialize()没有被调用/已无法找到主窗口句柄。

  • (2)LogiIsConnected 设备是否已连接
    Bool LogiIsConnected(const int index)
    该LogiIsConnected()函数检查游戏控制器是否连接在指定索引处
    Index:游戏控制器的索引。0对应连接的第一个游戏控制器。1对应第二个游戏控制器。

  • (3)LogiGetStateUnity 获取设备状态
    DIJOYSTATE2ENGINES LogiGetStateUnity(int index)
    LogiGetStateUnity()返回结构体DIJOYSTATE2控制器的状态。
    Index:游戏控制器的索引。0对应连接的第一个游戏控制器。1对应第二个游戏控制器。

  • (4)LogiGetFriendlyProductName 获取设备名
    Bool LogiGetFriendlyProductName(int index, StringBuilder buffer, int bufferSize);
    Index:游戏控制器的索引。0对应连接的第一个游戏控制器。1对应第二个游戏控制器。
    Buffer:设备名
    bufferSize:buffer长度

  • (5)DIJOYSTATE2结构体 包含设备的在坐标系中的位置信息,和POVs和按钮设备信息。
    【大部分方向盘、脚踏、排挡杆数据可在这里获取】

   public struct DIJOYSTATE2ENGINES
    {
        public int lX;                     /* x-axis position   【方向盘】    */
        public int lY;                     /* y-axis position   【油门】       */
        public int lZ;                     /* z-axis position              */
        public int lRx;                    /* x-axis rotation              */
        public int lRy;                    /* y-axis rotation              */
        public int lRz;                    /* z-axis rotation    【刹车】     */
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
        public int[] rglSlider;              /* extra axes positions  【rglSlider[0]离合】 */
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public uint[] rgdwPOV;                          /* POV directions               */
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
        public byte[] rgbButtons;                     /* 128 buttons                  */
        public int lVX;                    /* x-axis velocity              */
        public int lVY;                    /* y-axis velocity              */
        public int lVZ;                    /* z-axis velocity              */
        public int lVRx;                   /* x-axis angular velocity      */
        public int lVRy;                   /* y-axis angular velocity      */
        public int lVRz;                   /* z-axis angular velocity      */
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
        public int[] rglVSlider;             /* extra axes velocities        */
        public int lAX;                    /* x-axis acceleration          */
        public int lAY;                    /* y-axis acceleration          */
        public int lAZ;                    /* z-axis acceleration          */
        public int lARx;                   /* x-axis angular acceleration  */
        public int lARy;                   /* y-axis angular acceleration  */

        public int lARz;                   /* z-axis angular acceleration  */
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
        public int[] rglASlider;             /* extra axes accelerations     */
        public int lFX;                    /* x-axis force                 */
        public int lFY;                    /* y-axis force                 */
        public int lFZ;                    /* z-axis force                 */
        public int lFRx;                   /* x-axis torque                */
        public int lFRy;                   /* y-axis torque                */
        public int lFRz;                   /* z-axis torque                */
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
        public int[] rglFSlider;                        /* extra axes forces            */
};
//例子:		可在void Update()中写,获取方向盘数据。
if (LogitechGSDK.LogiUpdate() && LogitechGSDK.LogiIsConnected(0))
{
            LogitechGSDK.DIJOYSTATE2ENGINES wheel;
            wheel = LogitechGSDK.LogiGetStateUnity(0);
            Debug.Log(wheel.lX);
}

3、LogiButton 方向盘按键

  • (1)bool LogiButtonTriggered(int index, int buttonNbr); 按键检测
  • (2)bool LogiButtonIsPressed(int index, int buttonNbr); 按键释放
  • (3)bool LogiButtonReleased(int index, int buttonNbr); 按键按下
    buttonNbr:想要检测的按钮号码
    【Triggered按键按下第一帧返回true,Pressed按键按下期间返回true,Released按键松开第一帧返回true】
//例子:    
//可在void Update()中写,获取按键信息
 if (LogitechGSDK.LogiUpdate() && LogitechGSDK.LogiIsConnected(0))
{
if (LogitechGSDK.LogiButtonTriggered(0, 0))
                Debug.Log("KeyCode.Joystick1Button0  方向盘 X 键");
            if (LogitechGSDK.LogiButtonTriggered(0, 1))
                Debug.Log("KeyCode.Joystick1Button1  方向盘 □ 键");
}


//或:
//(这种方法是根据参考官方的演示脚本中写的。
//如果只需要监听几个按钮,推荐上面的方法,且作用类似LogiButtonIsPressed,只要按下,wheel.rgbButtons[i]就返回true)
//但最推荐就是写一个脚本,把所有输入信息包装一下,用事件的形式触发。
if (LogitechGSDK.LogiUpdate() && LogitechGSDK.LogiIsConnected(0))
{
      LogitechGSDK.DIJOYSTATE2ENGINES wheel;
      wheel = LogitechGSDK.LogiGetStateUnity(0);
      for (int i = 0; i < 128; i++)
      {
           if (wheel.rgbButtons[i] == 128)
           {
               switch (i)
               {
                  case 0:
                     Debug.Log("KeyCode.Joystick1Button0  方向盘 X 键");
                     break;
                  case 1:
                     Debug.Log("KeyCode.Joystick1Button1  方向盘 □ 键");
                     break;
					//。。。。。。。。。。
			   }
           }
      }
}

4、LogiControllerPropertiesData 控制器属性

public struct LogiControllerPropertiesData
{
        public bool forceEnable;	使能力
        public int overallGain; 整体
        public int springGain; 弹簧力
        public int damperGain; 阻尼力
        public bool defaultSpringEnabled;默认弹簧力状态
        public int defaultSpringGain; 默认弹簧力的大小
        public bool combinePedals; 
        public int wheelRange; 轮范围
        public bool gameSettingsEnabled;
        public bool allowGameSettings;
}

5、LogiIsPlaying 力反馈是否发挥作用
bool LogiIsPlaying(const int index, const int forceType)检查是否有某个力正在发挥作用。

Force types:
    public const int LOGI_FORCE_NONE = -1;
    public const int LOGI_FORCE_SPRING = 0;
    public const int LOGI_FORCE_CONSTANT = 1;
    public const int LOGI_FORCE_DAMPER = 2;
    public const int LOGI_FORCE_SIDE_COLLISION = 3;
    public const int LOGI_FORCE_FRONTAL_COLLISION = 4;
    public const int LOGI_FORCE_DIRT_ROAD = 5;
    public const int LOGI_FORCE_BUMPY_ROAD = 6;
    public const int LOGI_FORCE_SLIPPERY_ROAD = 7;
    public const int LOGI_FORCE_SURFACE_EFFECT = 8;
    public const int LOGI_NUMBER_FORCE_EFFECTS = 9;
    public const int LOGI_FORCE_SOFTSTOP = 10;
    public const int LOGI_FORCE_CAR_AIRBORNE = 11;

6、SpringForce 弹簧力

  • (1)bool LogiPlaySpringForce(int index, int offsetPercentage, int saturationPercentage, int coefficientPercentage); 产生弹簧力
    offsetPercentage:指定弹簧力作用的中心,[-100,100],指定0为弹簧的中心位置。超出范围的任何值弹簧都会静止不动。
    saturationPercentage:指定弹力效果饱和的水平。经过中心位置一定偏差后,饱和水平保持不变。[0,100],超出范围的任何值弹簧都会静止不动。
    coefficientPercentage:表示指定相对于从条件中心偏转的量的效果强度增加的斜率。值越高,到达饱和水平越快。[-100,100]。有效范围之外的任何值都将被静默地夹紧。
  • (2)bool LogiStopSpringForce(int index); 终止弹簧力
  • (3)例子:
    LogitechGSDK.LogiPlaySpringForce(0, 50, 50, 50);
    LogitechGSDK.LogiStopSpringForce(0);

7、ConstantForce 恒力

  • (1)bool LogiPlayConstantForce(int index, int magnitudePercentage); 产生恒力
    magnitudePercentage:指定恒力作用的大小。负值与力的方向相反。[-100,100],超出范围的任何值弹簧都会静止不动。
  • (2)bool LogiStopConstantForce(int index); 终止恒力

8、DamperForce 阻尼力

  • (1)bool LogiPlayDamperForce(int index, int coefficientPercentage); 产生阻尼力
    coefficientPercentage:效果强度增加的斜率和中心位置处的条件有关。值越高,到达饱和水平越快。[-100,100],有效范围之外的任何值都将被静默地夹紧。-100模拟了一个非常光滑的效果。+100使车轮/操纵杆很难移动,模拟车在停车或在泥泞中行驶。
  • (2)bool LogiStopDamperForce(int index); 终止阻尼力

9、LogiPlaySideCollisionForce 侧向碰撞力

  • bool LogiPlaySideCollisionForce(int index, int magnitudePercentage);
    magnitudePercentage:侧面碰撞力的大小。[-100,100],超出范围的任何值将不起作用。

10、LogiPlayFrontalCollisionForce 正向碰撞力

  • bool LogiPlayFrontalCollisionForce(int index, int magnitudePercentage);
    magnitudePercentage:正向碰撞力的大小。[0,100],超出范围的任何值将不起作用。

11、DirtRoadEffect土路效果

  • (1)bool LogiPlayDirtRoadEffect(int index, int magnitudePercentage);开启土路效果
    magnitudePercentage:指土路影响的程度。[0,100],超出范围的任何值将不起作用。
  • (2)bool LogiStopDirtRoadEffect(int index); 终止土路效果

12、BumpyRoadEffect 颠簸路面效果

  • (1)bool LogiPlayBumpyRoadEffect(int index, int magnitudePercentage); 开启颠簸路面效果
    magnitudePercentage:指颠簸路面影响的程度。[0,100],超出范围的任何值将不起作用。
  • (2)bool LogiStopBumpyRoadEffect(int index);关闭颠簸路面效果

13、SlipperyRoadEffect 平滑路面效果

  • (1)bool LogiPlaySlipperyRoadEffect(int index, int magnitudePercentage); 开启平滑路面效果
    magnitudePercentage:指平滑路面影响的程度。[0,100],超出范围的任何值将不起作用。
  • (2)bool LogiStopSlipperyRoadEffect(int index);终止平滑路面效果

14、SurfaceEffect 路面响应效果

  • (1)bool LogiPlaySurfaceEffect(int index, int type, int magnitudePercentage, int period); 开启路面响应效果
//Periodic types  for surface effect
    public const int LOGI_PERIODICTYPE_NONE = -1;
    public const int LOGI_PERIODICTYPE_SINE = 0;
    public const int LOGI_PERIODICTYPE_SQUARE = 1;
    public const int LOGI_PERIODICTYPE_TRIANGLE = 2;

magnitudePercentage:指路面影响的程度。[0,100],超出范围的任何值将不起作用。
period:表示周期力作用的期限,值是毫秒为测量单位。

  • (2)bool LogiStopSurfaceEffect(int index); 关闭路面响应效果
  • (3) 例子:LogitechGSDK.LogiPlaySurfaceEffect(0, LogitechGSDK.LOGI_PERIODICTYPE_SQUARE, 50, 1000);

15、CarAirborne 模拟汽车在路面上腾空的效果

  • (1)bool LogiPlayCarAirborne(int index); 模拟汽车在路面上腾空的效果
  • (2)bool LogiStopCarAirborne(int index);终止汽车在路面上腾空的效果

16、SoftstopForce 软停力

  • (1)bool LogiPlaySoftstopForce(int index, int usableRangePercentage); 开启软停力
    usableRangePercentage:表示软停力效果影响deadband的百分比。
  • (2)bool LogiStopSoftstopForce(int index); 终止软停力

17、LogiPlayLeds 开启方向盘的LED灯

  • bool LogiPlayLeds(int index, float currentRPM, float rpmFirstLedTurnsOn, float rpmRedLine);

最后我做了一个辣鸡插件,如何使用在test里,供大家参考吧。
功能如下:

监控了方向盘 + 脚踏板 + 档位器中的如下数据

  • 方向盘:
    15 个按钮的触发事件(包括:刚点击触发一次,维持点击一直触发,松开时触发一次)
    7 个按钮(pov)的触发事件(点击按钮时一直触发)
    旋转红色旋钮的触发事件(每旋转一档旋钮时触发一次)
    转动方向盘的数据事件
  • 脚踏板:
    离合、刹车、油门的数据事件
  • 档位器:
    7个档位的触发事件(包括:刚进入新档位时触发一次,维持档位一直触发,离开档位触发一次)
using System;
using System.Collections;
using UnityEngine;



namespace Sakura
{
    /// <summary>
    /// Logitech输入设备
    /// </summary>
    public enum LogitechSteeringWheelButtonType
    {
        /// <summary>
        /// 方向盘 X 键(按钮1)
        /// </summary>
        x,

        /// <summary>
        /// 方向盘 □ 键(按钮2)
        /// </summary>
        rect,

        /// <summary>
        /// 方向盘 ○ 键(按钮3)
        /// </summary>
        circle,

        /// <summary>
        /// 方向盘 △ 键(按钮4)
        /// </summary>
        triangle,

        /// <summary>
        /// 方向盘 右拨片 键(按钮5)
        /// </summary>
        rightDialSlice,

        /// <summary>
        /// 方向盘 左拨片 键(按钮6)
        /// </summary>
        leftDialSlice,

        /// <summary>
        /// 方向盘 R2 键(按钮7)
        /// </summary>
        R2,

        /// <summary>
        /// 方向盘 L2 键(按钮8)
        /// </summary>
        L2,
        
        /// <summary>
        /// 方向盘 SHARE 键(按钮9)
        /// </summary>
        share,

        /// <summary>
        /// 方向盘 OPTION 键(按钮10)
        /// </summary>
        option,

        /// <summary>
        /// 方向盘 R3 键(按钮11)
        /// </summary>
        R3,

        /// <summary>
        /// 方向盘 L3 键(按钮12)
        /// </summary>
        L3,

        /// <summary>
        /// 方向盘 + 键(按钮20)
        /// </summary>
        plus,

        /// <summary>
        /// 方向盘 - 键(按钮21)
        /// </summary>
        subtract,

        /// <summary>
        /// 方向盘 回车 键(按钮24)
        /// </summary>
        enter,

        /// <summary>
        ///  方向盘 logo 键(按钮25)
        /// </summary>
        logoBtn
        
    }


    public enum CursorType
    {
        /// <summary>
        ///  Up 
        ///  rec.rgdwPOV[0] = 0
        /// </summary>
        up,

        /// <summary>
        ///  UP-RIGHT
        ///  rec.rgdwPOV[0] = 4500
        /// </summary>
        up_right,

        /// <summary>
        ///  right
        ///  rec.rgdwPOV[0] = 9000
        /// </summary>
        right,

        /// <summary>
        ///  DOWN-RIGHT
        ///  rec.rgdwPOV[0] = 13500
        /// </summary>
        down_right,

        /// <summary>
        ///  down
        ///  rec.rgdwPOV[0] = 18000
        /// </summary>
        down,

        /// <summary>
        ///  DOWN-LEFT
        ///  rec.rgdwPOV[0] = 22500
        /// </summary>
        down_left,

        /// <summary>
        ///  left
        ///  rec.rgdwPOV[0] = 27000
        /// </summary>
        left,

        /// <summary>
        ///  UP-LEFT
        ///  rec.rgdwPOV[0] = 31500
        /// </summary>
        up_left
    }


    public class LogitechController : MonoBehaviour
    {
        public GearShifter gearShifter;
        public Pedals pedals;
        public SteeringWheel steeringWheel;
        

        private LogitechGSDK.DIJOYSTATE2ENGINES rec;

        /// <summary>
        /// 方向盘 
        /// </summary>
        public class SteeringWheel
        {
            /// <summary>
            /// 刚点击按钮时触发一次
            /// LogitechWheelButtonType:按钮类型
            /// </summary>
            public Action<LogitechSteeringWheelButtonType> OnTriggered;
            /// <summary>
            /// 维持点击按钮时一直触发
            /// LogitechWheelButtonType:按钮类型
            /// </summary>
            public Action<LogitechSteeringWheelButtonType> OnPressed;
            /// <summary>
            /// 松开按钮时触发一次
            /// LogitechWheelButtonType:按钮类型
            /// </summary>
            public Action<LogitechSteeringWheelButtonType> OnReleased;

            /// <summary>
            /// 点击按钮时触发一次,按住按钮一直触发
            /// CursorType:方向类型
            /// </summary>
            public Action<CursorType> OnCursor;

            /// <summary>
            /// 每旋转一档旋钮时触发一次
            /// bool:返回true,顺时针旋转;返回flase,逆时针旋转
            /// </summary>
            public Action<bool> OnKnob;

            /// <summary>
            /// 转动方向盘触发
            /// float:不触碰方向盘,值一直为0,触碰后左扭到尽:-32768,右扭到尽为32767
            /// </summary>
            public Action<float> OnSteeringWheel;

        }

        /// <summary>
        /// 脚踏板
        /// </summary>
        public class Pedals
        {
            /// <summary>
            /// 离合
            /// float:不触碰脚踏板,值一直为0,触碰后踩尽:-32768,松开为32767
            /// </summary>
            public Action<float> Clutch;
            /// <summary>
            /// 刹车
            /// float:不触碰脚踏板,值一直为0,触碰后踩尽:-32768,松开为32767
            /// </summary>
            public Action<float> Brake;
            /// <summary>
            /// 油门
            /// float:不触碰脚踏板,值一直为0,触碰后踩尽:-32768,松开为32767
            /// </summary>
            public Action<float> Throttle;
        }

        /// <summary>
        /// 档位器
        /// </summary>
        public class GearShifter
        {
            /// <summary>
            /// 刚进入新档位时触发一次
            /// int:档位数(0:R档,1:1档,2:2档,以此类推)
            /// </summary>
            public Action<int> OnTriggered;
            /// <summary>
            /// 维持档位时一直触发
            /// int:档位数(0:R档,1:1档,2:2档,以此类推)
            /// </summary>
            public Action<int> OnPressed;
            /// <summary>
            /// 离开档位时触发一次
            /// int:档位数(0:R档,1:1档,2:2档,以此类推)
            /// </summary>
            public Action<int> OnReleased;
            
        }


        private void Awake()
        {
            gearShifter = new GearShifter();
            pedals = new Pedals();
            steeringWheel = new SteeringWheel();

            #region test
            //gearShifter.OnTriggered += (index) => { Debug.Log(index + " OnTriggered"); };
            //gearShifter.OnPressed += (index) => { Debug.Log(index + " OnPressed"); };
            //gearShifter.OnReleased += (index) => { Debug.Log(index + "OnReleased "); };
            //pedals.Clutch += (xx) => { Debug.Log(xx + "Clutch"); };
            //pedals.Brake += (xx) => { Debug.Log(xx + "Brake"); };
            //pedals.Throttle += (xx) => { Debug.Log(xx + "Throttle"); };

            //steeringWheel.OnTriggered += (index) => { Debug.Log(index + " OnTriggered"); };
            //steeringWheel.OnPressed += (index) => { Debug.Log(index + " OnPressed"); };
            //steeringWheel.OnReleased += (index) => { Debug.Log(index + "OnReleased "); };
            //steeringWheel.OnCursor += (inx) => { Debug.Log(inx + "OnReleased "); };
            //steeringWheel.OnKnob += (inx) => { Debug.Log(inx + "OnReleased "); };
            //steeringWheel.OnSteeringWheel += (inx) => { Debug.Log(inx + "  "); };
            #endregion


            bool x = false;
            while (x == false)
            {
                x = LogitechGSDK.LogiSteeringInitialize(false);
                Debug.Log("SteeringInit:" + x);
            }
           // 
        }
        

        private void Update()
        {
           
            if (LogitechGSDK.LogiUpdate() && LogitechGSDK.LogiIsConnected(0))
            {
                #region 档位器

                //挡位 1 挡
                if (LogitechGSDK.LogiButtonTriggered(0, 12))
                {
                    if(gearShifter.OnTriggered != null)      gearShifter.OnTriggered(1);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 12))
                {
                    if (gearShifter.OnPressed != null) gearShifter.OnPressed(1);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 12))
                {
                    if (gearShifter.OnReleased != null) gearShifter.OnReleased(1);
                }

                //挡位 2 挡
                if (LogitechGSDK.LogiButtonTriggered(0, 13))
                {
                    if (gearShifter.OnTriggered != null) gearShifter.OnTriggered(2);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 13))
                {
                    if (gearShifter.OnPressed != null) gearShifter.OnPressed(2);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 13))
                {
                    if (gearShifter.OnReleased != null) gearShifter.OnReleased(2);
                }

                //挡位 3 挡
                if (LogitechGSDK.LogiButtonTriggered(0, 14))
                {
                    if (gearShifter.OnTriggered != null) gearShifter.OnTriggered(3);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 14))
                {
                    if (gearShifter.OnPressed != null) gearShifter.OnPressed(3);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 14))
                {
                    if (gearShifter.OnReleased != null) gearShifter.OnReleased(3);
                }

                //挡位 4 挡
                if (LogitechGSDK.LogiButtonTriggered(0, 15))
                {
                    if (gearShifter.OnTriggered != null) gearShifter.OnTriggered(4);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 15))
                {
                    if (gearShifter.OnPressed != null) gearShifter.OnPressed(4);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 15))
                {
                    if (gearShifter.OnReleased != null) gearShifter.OnReleased(4);
                }

                //挡位 5 挡
                if (LogitechGSDK.LogiButtonTriggered(0, 16))
                {
                    if (gearShifter.OnTriggered != null) gearShifter.OnTriggered(5);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 16))
                {
                    if (gearShifter.OnPressed != null) gearShifter.OnPressed(5);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 16))
                {
                    if (gearShifter.OnReleased != null) gearShifter.OnReleased(5);
                }
                
                //挡位 6 挡
                if (LogitechGSDK.LogiButtonTriggered(0, 17))
                {
                    if (gearShifter.OnTriggered != null) gearShifter.OnTriggered(6);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 17))
                {
                    if (gearShifter.OnPressed != null) gearShifter.OnPressed(6);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 17))
                {
                    if (gearShifter.OnReleased != null) gearShifter.OnReleased(6);
                }

                //挡位 R 挡
                if (LogitechGSDK.LogiButtonTriggered(0, 18))
                {
                    if (gearShifter.OnTriggered != null) gearShifter.OnTriggered(0);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 18))
                {
                    if (gearShifter.OnPressed != null) gearShifter.OnPressed(0);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 18))
                {
                    if (gearShifter.OnReleased != null) gearShifter.OnReleased(0);
                }

                #endregion

                #region 脚踏板

                rec = LogitechGSDK.LogiGetStateUnity(0);

                //离合
                if (pedals.Clutch != null) pedals.Clutch(rec.rglSlider[0]);
                //刹车
                if (pedals.Brake != null) pedals.Brake(rec.lRz);
                //油门
                if (pedals.Throttle != null) pedals.Throttle(rec.lY);

                #endregion

                #region 方向盘
                rec = LogitechGSDK.LogiGetStateUnity(0);
                if (steeringWheel.OnSteeringWheel != null) steeringWheel.OnSteeringWheel(rec.lX);

                //方向盘 X 键
                if (LogitechGSDK.LogiButtonTriggered(0, 0))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.x);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 0))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.x);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 0))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.x);
                }
                //方向盘 □ 键
                if (LogitechGSDK.LogiButtonTriggered(0, 1))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.rect);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 1))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.rect);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 1))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.rect);
                }
                //方向盘 ○ 键
                if (LogitechGSDK.LogiButtonTriggered(0, 2))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.circle);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 2))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.circle);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 2))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.circle);
                }
                //方向盘 △ 键
                if (LogitechGSDK.LogiButtonTriggered(0, 3))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.triangle);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 3))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.triangle);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 3))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.triangle);
                }
                //方向盘 右拨片 键
                if (LogitechGSDK.LogiButtonTriggered(0, 4))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.rightDialSlice);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 4))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.rightDialSlice);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 4))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.rightDialSlice);
                }
                //方向盘 左拨片 键
                if (LogitechGSDK.LogiButtonTriggered(0, 5))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.leftDialSlice);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 5))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.leftDialSlice);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 5))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.leftDialSlice);
                }
                //方向盘 R2 键
                if (LogitechGSDK.LogiButtonTriggered(0, 6))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.R2);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 6))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.R2);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 6))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.R2);
                }
                //方向盘 L2 键
                if (LogitechGSDK.LogiButtonTriggered(0, 7))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.L2);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 7))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.L2);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 7))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.L2);
                }
                //方向盘 SHARE 键
                if (LogitechGSDK.LogiButtonTriggered(0, 8))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.share);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 8))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.share);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 8))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.share);
                }
                //方向盘 OPTION 键
                if (LogitechGSDK.LogiButtonTriggered(0, 9))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.option);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 9))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.option);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 9))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.option);
                }
                //方向盘 R3 键
                if (LogitechGSDK.LogiButtonTriggered(0, 10))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.R3);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 10))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.R3);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 10))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.R3);
                }
                //方向盘 L3 键
                if (LogitechGSDK.LogiButtonTriggered(0, 11))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.L3);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 11))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.L3);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 11))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.L3);
                }
                //方向盘 + 键
                if (LogitechGSDK.LogiButtonTriggered(0, 19))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.plus);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 19))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.plus);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 19))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.plus);
                }
                //方向盘 - 键
                if (LogitechGSDK.LogiButtonTriggered(0, 20))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.subtract);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 20))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.subtract);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 20))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.subtract);
                }
                //方向盘 回车 键
                if (LogitechGSDK.LogiButtonTriggered(0, 23))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.enter);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 23))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.enter);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 23))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.enter);
                }
                //方向盘 logo 键
                if (LogitechGSDK.LogiButtonTriggered(0, 24))
                {
                    if (steeringWheel.OnTriggered != null) steeringWheel.OnTriggered(LogitechSteeringWheelButtonType.logoBtn);
                }
                else if (LogitechGSDK.LogiButtonIsPressed(0, 24))
                {
                    if (steeringWheel.OnPressed != null) steeringWheel.OnPressed(LogitechSteeringWheelButtonType.logoBtn);
                }
                else if (LogitechGSDK.LogiButtonReleased(0, 24))
                {
                    if (steeringWheel.OnReleased != null) steeringWheel.OnReleased(LogitechSteeringWheelButtonType.logoBtn);
                }
                //方向盘 红色滚轮右滚 键
                if (LogitechGSDK.LogiButtonTriggered(0, 21))
                {
                    if (steeringWheel.OnKnob != null) steeringWheel.OnKnob(true);
                }
                //方向盘 红色滚轮左滚 键
                if (LogitechGSDK.LogiButtonTriggered(0, 22))
                {
                    if (steeringWheel.OnKnob != null) steeringWheel.OnKnob(false);
                }

                switch (rec.rgdwPOV[0])
                {
                    case (0):      //Up
                        if (steeringWheel.OnCursor != null) steeringWheel.OnCursor(CursorType.up);
                        break;
                    case (4500):   // UP-RIGHT
                        if (steeringWheel.OnCursor != null) steeringWheel.OnCursor(CursorType.up_left);
                        break;
                    case (9000):   //right
                        if (steeringWheel.OnCursor != null) steeringWheel.OnCursor(CursorType.right);
                        break;
                    case (13500):  //DOWN-RIGHT
                        if (steeringWheel.OnCursor != null) steeringWheel.OnCursor(CursorType.down_right);
                        break;
                    case (18000):  //down
                        if (steeringWheel.OnCursor != null) steeringWheel.OnCursor(CursorType.down);
                        break;
                    case (22500):  //DOWN-LEFT
                        if (steeringWheel.OnCursor != null) steeringWheel.OnCursor(CursorType.down_left);
                        break;
                    case (27000):  //left
                        if (steeringWheel.OnCursor != null) steeringWheel.OnCursor(CursorType.left);
                        break;
                    case (31500):  //UP-LEFT
                        if (steeringWheel.OnCursor != null) steeringWheel.OnCursor(CursorType.up_left);
                        break;
                }
                
                #endregion

            }


        }

        void OnApplicationQuit()
        {
            Debug.Log("SteeringShutdown:" + LogitechGSDK.LogiSteeringShutdown());
        }

    }


}
  • 16
    点赞
  • 104
    收藏
    觉得还不错? 一键收藏
  • 35
    评论
要使用Unity和罗技G29方向盘制作一个简单的模拟驾驶,您可以按照以下步骤进行操作: 1. 连接罗技G29方向盘:将您的罗技G29方向盘连接到您的电脑上,并确保驱动程序已正确安装。 2. 创建车辆模型:您可以在Unity中创建一个适合驾驶模拟的车辆模型。可以从Unity Asset Store或其他资源库中获取现成的车辆模型,或者自己创建模型。 3. 导入Realistic Car Controller(RCC)资源:从Unity Asset Store下载并导入RCC包。RCC是一个专门用来制作真实驾驶模拟的资源,其中包含了一系列预设和脚本。 4. 设置车辆控制器:将RCC脚本附加到您的车辆模型上,并根据您的需要进行调整。您可以设置转向灵敏度、加速度、刹车和倒挡等参数,以实现更真实的驾驶体验。 5. 连接罗技G29方向盘输入:使用Unity的输入管理系统,将罗技G29方向盘的输入映射到车辆控制器上。您可以使用Unity的Input Manager或RCC包中的输入映射器来完成此操作。 6. 添加场景和环境:在Unity中创建一个逼真的驾驶场景,并添加适当的道路、交通标志和其他环境要素。您还可以为模拟驾驶体验增加天气效果、时间变化和音效等。 7. 调试和优化:在完成制作之后,进行测试和调试,确保罗技G29方向盘与车辆控制器的交互正常。您还可以通过优化代码、调整物理特性和提升图形效果来改善驾驶模拟的体验。 通过以上步骤,您就可以使用Unity和罗技G29方向盘制作一个简单但逼真的模拟驾驶体验。当然,根据您的需求也可以进行更多的定制和细节处理。
评论 35
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值