Unity3D_接入罗技G29方向盘

基于Unity版本 2018.4.11f1(点击下载

设置脚踏板

脚踏板的设置需要在Unity中添加虚拟轴,添加几个轴就把图中的“Size”改为 18 加上要添加的轴的数量。

离合

刹车

 

油门

 

Name:名字

Dead:设置为0,不然得到的数字不是 Sensitivity 设置的数值

Sensitivity:建议设置成0.5(该值为0.5时,若游戏开始运行,没有触碰过脚踏板,那么该数值为0;当触碰了一次离合踏板时(触碰任何一个脚踏板后,三个脚踏板都会出现非 0 的数值),离合板完全抬起的时候数值为0.5,完全踩下时数值为-0.5)

Type:按图中所示选择

Axis:按图中所示选择

设置方向盘

十字按键

 其他按键

通过以下脚本获取using System;using System.Collections;

using System.Collections.Generic;
using UnityEngine;

public class WiWi_GetInputG29 : MonoBehaviour
{
    // 离合值
    private float clutch;
    // 油门值
    private float throttle;
    // 刹车值
    private float brake;

    void Start()
    {
        // 没有这行代码,用方法二可能获取不到按键
        LogitechGSDK.LogiSteeringInitialize(false);
    }

    void Update()
    {
        // **如果该值在电脑插上方向盘之后始终为-1,那么需要将 inputManager 窗口中的第二个 Vertical 改成别的名字
        print(Input.GetAxis("Vertical"));
        // 获取方向盘
        if (Input.GetAxis("Horizontal") != 0)
        {
            print(Input.GetAxis("Horizontal"));
        }

        // 获取“脚踏板”(没有激活踏板时:值始终为 0(激活的意思就是游戏开始后踩一下)...激活之后:完全抬起是0.5,踩到底是-0.5,)
        clutch = Input.GetAxisRaw("Clutch");
        if (clutch < 0.5f && clutch != 0)
        {
            print("离合 的值为:" + clutch);
        }
        throttle = Input.GetAxisRaw("Throttle");
        if (throttle < 0.5f && throttle != 0)
        {
            print("油门 的值为:" + throttle);
        }
        brake = Input.GetAxisRaw("Brake");
        if (brake < 0.5f && brake != 0)
        {
            print("刹车 的值为:" + brake);
        }

        // 获取方向盘上的“十字按键”
        if (Input.GetAxisRaw("DPadX") == 0.5)
        {
            print("按下了十字按键  右");
        }
        if (Input.GetAxisRaw("DPadX") == -0.5)
        {
            print("按下了十字按键  左");
        }
        if (Input.GetAxisRaw("DPadY") == 0.5)
        {
            print("按下了十字按键  上");
        }
        if (Input.GetAxisRaw("DPadY") == -0.5)
        {
            print("按下了十字按键  下");
        }

        // 方向盘所有按键获取:
        // 方法一:
        // 该方法在unity中只能获取0-19的键位  20-24键位需要使用方法二获取 或者全部使用方法二 
// 方向盘上对应的虚拟粥名称可通过《监听输入》或《方法二》打印内容获取
if (Input.GetKeyDown(KeyCode.Joystick1Button0)) { Debug.Log("KeyCode.Joystick1Button0 方向盘 X 键"); } // 监听输入:存储所有的按键 var values = Enum.GetValues(typeof(KeyCode)); for (int x = 0; x < values.Length; x++) { if (Input.GetKeyDown((KeyCode)values.GetValue(x))) { // 打印当前按下的按键(按下方向盘上的按键,控制台会打印出对应的按键名称,然后使用 KeyCode.该名称 就可以获取按键状态) print(values.GetValue(x)); } }// 保证方向盘被正常识别,经测试,没有此行也不会出错,加上保险一点 print(LogitechGSDK.LogiUpdate()); // 方法二: if (true) { 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 键" + i); break; case 1: Debug.Log("KeyCode.Joystick1Button1 方向盘 □ 键" + i); break; case 2: Debug.Log("KeyCode.Joystick1Button2 方向盘 ○ 键" + i); break; case 3: Debug.Log("KeyCode.Joystick1Button3 方向盘 △ 键" + i); break; case 4: Debug.Log("KeyCode.Joystick1Button4 方向盘 右拨片 键" + i); break; case 5: Debug.Log("KeyCode.Joystick1Button5 方向盘 左拨片 键" + i); break; case 6: Debug.Log("KeyCode.Joystick1Button6 方向盘 R2 键" + i); break; case 7: Debug.Log("KeyCode.Joystick1Button7 方向盘 L2 键" + i); break; case 8: Debug.Log("KeyCode.Joystick1Button8 方向盘 SHARE 键" + i); break; case 9: Debug.Log("KeyCode.Joystick1Button9 方向盘 OPTION 键" + i); break; case 10: Debug.Log("KeyCode.Joystick1Butto10 方向盘 R3 键" + i); break; case 11: Debug.Log("KeyCode.Joystick1Button11 方向盘 L3 键" + i); break; case 12: Debug.Log("KeyCode.Joystick1Button12 挡位 1 挡" + i); break; case 13: Debug.Log("KeyCode.Joystick1Button13 挡位 2 挡" + i); break; case 14: Debug.Log("KeyCode.Joystick1Button14 挡位 3 挡" + i); break; case 15: Debug.Log("KeyCode.Joystick1Button15 挡位 4 挡" + i); break; case 16: Debug.Log("KeyCode.Joystick1Button16 挡位 5 挡" + i); break; case 17: Debug.Log("KeyCode.Joystick1Button17 挡位 6 挡" + i); break; case 18: Debug.Log("KeyCode.Joystick1Button18 挡位 R 挡(倒挡)" + i); break; case 19: Debug.Log("KeyCode.Joystick1Button19 方向盘 + 键" + i); break; case 20: Debug.Log("KeyCode.Joystick1Button20 方向盘 - 键" + i); break; case 21: Debug.Log("KeyCode.Joystick1Button21 方向盘 红色滚轮右滚 键" + i); break; case 22: Debug.Log("KeyCode.Joystick1Button22 方向盘 红色滚轮左滚 键" + i); break; case 23: Debug.Log("KeyCode.Joystick1Button23 方向盘 回车 键" + i); break; case 24: Debug.Log("KeyCode.Joystick1Button24 方向盘 特殊标志键 键" + i); break; } } } } } }

 

 

示例工程和LogitechSDK(点击下载

罗技G29方向盘驱动(点击下载

 

posted on 2020-06-02 15:35  考拉宝贝  阅读(2426)  评论(0编辑  收藏  举报

导航