当前位置:   article > 正文

UnityXR手柄按键输入_unity xr controller(action-based)

unity xr controller(action-based)

UnityXR 手柄按键输入

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using UnityEngine.UI;

public class JackXRUse : MonoBehaviour
{
    private int int_AR, int_BR, int_TR, int_GR, int_MR;

    private int int_AL, int_BL, int_TL, int_GL, int_ML;

    public Text Text_AR, Text_BR, Text_TR, Text_GR, Text_MR, Text_JSR_x, Text_JSR_y;
    public Image Image_JSR;

    public Text Text_AL, Text_BL, Text_TL, Text_GL, Text_ML, Text_JSL_x, Text_JSL_y;
    public Image Image_JSL;

    private bool bool_AR, bool_BR, bool_TR, bool_GR, bool_MR;
    private bool bool_AR_On, bool_BR_On, bool_TR_On, bool_GR_On, bool_MR_On;

    private bool bool_AL, bool_BL, bool_TL, bool_GL, bool_ML;
    private bool bool_AL_On, bool_BL_On, bool_TL_On, bool_GL_On, bool_ML_On;

    private void Update()
    {
        //列出所有输入设备,根据XR节点获取输入设备,必须先获取到输入设备,再获取对应输入设备的输入键值
        var rightDevice = new List<InputDevice>();
        var leftdevice = new List<InputDevice>();


        bool menuValueright, gripValueright, triggerValueright, primaryValueright, secondaryValueright;
        bool menuValueleft, gripValueleft, triggerValueleft, primaryValueleft, secondaryValueleft;
        Vector2 joystickright, joystickleft;

        InputDevices.GetDevicesAtXRNode(XRNode.RightHand, rightDevice);
        InputDevices.GetDevicesAtXRNode(XRNode.LeftHand, leftdevice);

        if (rightDevice.Count != 0 && leftdevice.Count != 0)
        {
            InputDevice rightcontroller = rightDevice[0];
            InputDevice leftcontroller = leftdevice[0];

            //右手A键
            rightcontroller.TryGetFeatureValue(CommonUsages.primaryButton, out primaryValueright);
            if(primaryValueright)
            {
                bool_AR = true;
            }
            else
            {
                bool_AR = false;
                bool_AR_On = true;
            }
            //右手B键
            rightcontroller.TryGetFeatureValue(CommonUsages.secondaryButton, out secondaryValueright);
            if (secondaryValueright)
            {
                bool_BR = true;
            }
            else
            {
                bool_BR = false;
                bool_BR_On = true;
            }
            //右手Trigger键
            rightcontroller.TryGetFeatureValue(CommonUsages.triggerButton, out triggerValueright);
            if (triggerValueright)
            {
                bool_TR = true;
            }
            else
            {
                bool_TR = false;
                bool_TR_On = true;
            }
            //右手Grip键
            rightcontroller.TryGetFeatureValue(CommonUsages.gripButton, out gripValueright);
            if (gripValueright)
            {
                bool_GR = true;
            }
            else
            {
                bool_GR = false;
                bool_GR_On = true;
            }
            //右手Menu键
            rightcontroller.TryGetFeatureValue(CommonUsages.menuButton, out menuValueright);
            if (menuValueright)
            {
                bool_MR = true;
            }
            else
            {
                bool_MR = false;
                bool_MR_On = true;
            }

            //右手摇杆
            rightcontroller.TryGetFeatureValue(CommonUsages.primary2DAxis, out joystickright);

            //显示摇杆的Vector2数据
            Image_JSR.transform.localPosition = joystickright;  
            Text_JSR_x.text = joystickright.x.ToString();
            Text_JSR_y.text = joystickright.y.ToString();

            //右手A键按下
            if (bool_AR && bool_AR_On)
            {
                bool_AR_On = !bool_AR_On;
                GetHandleRightADown();
            }
            //右手A键按住
            if (bool_AR)
            {
                GetHandleRightA();
            }
            //右手B键按下
            if (bool_BR && bool_BR_On)
            {
                bool_BR_On = !bool_BR_On;
                GetHandleRightBDown();
            }
            //右手B键按住
            if (bool_BR)
            {
                GetHandleRightB();
            }
            //右手Trigger键按下
            if (bool_TR && bool_TR_On)
            {
                bool_TR_On = !bool_TR_On;
                GetHandleRightTDown();
            }
            //右手Trigger键按住
            if (bool_TR)
            {
                GetHandleRightT();
            }
            //右手Grip键按下
            if (bool_GR && bool_GR_On)
            {
                bool_GR_On = !bool_GR_On;
                GetHandleRightGDown();
            }
            //右手Grip键按住
            if (bool_GR)
            {
                GetHandleRightG();
            }
            //右手Menu键按下
            if (bool_MR && bool_MR_On)
            {
                bool_MR_On = !bool_MR_On;
                GetHandleRightMDown();
            }
            //右手Menu键按住
            if (bool_MR)
            {
                GetHandleRightM();
            }
            //右手JoyStick
            if (joystickright.x > 0.5f)
            {

            }
            if (joystickright.x < -0.5f)
            {

            }
            if (joystickright.y > 0.5f)
            {

            }
            if (joystickright.y < -0.5f)
            {

            }

            //左手A键
            leftcontroller.TryGetFeatureValue(CommonUsages.primaryButton, out primaryValueleft);
            if (primaryValueleft)
            {
                bool_AL = true;
            }
            else
            {
                bool_AL = false;
                bool_AL_On = true;
            }
            //左手B键
            leftcontroller.TryGetFeatureValue(CommonUsages.secondaryButton, out secondaryValueleft);
            if (secondaryValueleft)
            {
                bool_BL = true;
            }
            else
            {
                bool_BL = false;
                bool_BL_On = true;
            }
            //左手Trigger键
            leftcontroller.TryGetFeatureValue(CommonUsages.triggerButton, out triggerValueleft);
            if (triggerValueleft)
            {
                bool_TL = true;
            }
            else
            {
                bool_TL = false;
                bool_TL_On = true;
            }
            //左手Grip键
            leftcontroller.TryGetFeatureValue(CommonUsages.gripButton, out gripValueleft);
            if (gripValueleft)
            {
                bool_GL = true;
            }
            else
            {
                bool_GL = false;
                bool_GL_On = true;
            }
            //左手Menu键
            leftcontroller.TryGetFeatureValue(CommonUsages.menuButton, out menuValueleft);
            if (menuValueleft)
            {
                bool_ML = true;
            }
            else
            {
                bool_ML = false;
                bool_ML_On = true;
            }

            //左手摇杆
            leftcontroller.TryGetFeatureValue(CommonUsages.primary2DAxis, out joystickleft);

            //显示摇杆的Vector2数据
            Image_JSL.transform.localPosition = joystickleft;
            Text_JSL_x.text = joystickleft.x.ToString();
            Text_JSL_y.text = joystickleft.y.ToString();

            //左手A键按下
            if (bool_AL && bool_AL_On)
            {
                bool_AL_On = !bool_AL_On;
                GetHandleLeftADown();
            }
            //左手A键按住
            if (bool_AL)
            {
                GetHandleLeftA();
            }
            //左手B键按下
            if (bool_BL && bool_BL_On)
            {
                bool_BL_On = !bool_BL_On;
                GetHandleLeftBDown();
            }
            //左手B键按住
            if (bool_BL)
            {
                GetHandleLeftB();
            }
            //左手Trigger键按下
            if (bool_TL && bool_TL_On)
            {
                bool_TL_On = !bool_TL_On;
                GetHandleLeftTDown();
            }
            //左手Trigger键按住
            if (bool_TL)
            {
                GetHandleLeftT();
            }
            //左手Grip键按下
            if (bool_GL && bool_GL_On)
            {
                bool_GL_On = !bool_GL_On;
                GetHandleLeftGDown();
            }
            //左手Grip键按住
            if (bool_GL)
            {
                GetHandleLeftG();
            }
            //左手Menu键按下
            if (bool_ML && bool_ML_On)
            {
                bool_ML_On = !bool_ML_On;
                GetHandleLeftMDown();
            }
            //左手Menu键按住
            if (bool_ML)
            {
                GetHandleLeftM();
            }
            //左手JoyStick
            if (joystickleft.x > 0.5f)
            {

            }
            if (joystickleft.x < -0.5f)
            {

            }
            if (joystickleft.y > 0.5f)
            {

            }
            if (joystickleft.y < -0.5f)
            {

            }
        }
    }
    //右手A键按下
    private void GetHandleRightADown()
    {
        int_AR++;
        Text_AR.text = int_AR.ToString();
    }
    //右手A键按住
    private void GetHandleRightA()
    {
        int_AR++;
        Text_AR.text = int_AR.ToString();
    }
    //右手B键按下
    private void GetHandleRightBDown()
    {
        int_BR++;
        Text_BR.text = int_BR.ToString();
    }
    //右手B键按住
    private void GetHandleRightB()
    {
        int_BR++;
        Text_BR.text = int_BR.ToString();
    }
    //右手T键按下
    private void GetHandleRightTDown()
    {
        int_TR++;
        Text_TR.text = int_TR.ToString();
    }
    //右手T键按住
    private void GetHandleRightT()
    {
        int_TR++;
        Text_TR.text = int_TR.ToString();
    }
    //右手G键按下
    private void GetHandleRightGDown()
    {
        int_GR++;
        Text_GR.text = int_GR.ToString();
    }
    //右手G键按住
    private void GetHandleRightG()
    {
        int_GR++;
        Text_GR.text = int_GR.ToString();
    }
    //右手M键按下
    private void GetHandleRightMDown()
    {
        int_MR++;
        Text_MR.text = int_MR.ToString();
    }
    //右手M键按住
    private void GetHandleRightM()
    {
        int_MR++;
        Text_MR.text = int_MR.ToString();
    }

    //左手A键按下
    private void GetHandleLeftADown()
    {
        int_AL++;
        Text_AL.text = int_AL.ToString();
    }
    //左手A键按住
    private void GetHandleLeftA()
    {
        int_AL++;
        Text_AL.text = int_AL.ToString();
    }
    //左手B键按下
    private void GetHandleLeftBDown()
    {
        int_BL++;
        Text_BL.text = int_BL.ToString();
    }
    //左手B键按住
    private void GetHandleLeftB()
    {
        int_BL++;
        Text_BL.text = int_BL.ToString();
    }
    //左手T键按下
    private void GetHandleLeftTDown()
    {
        int_TL++;
        Text_TL.text = int_TL.ToString();
    }
    //左手T键按住
    private void GetHandleLeftT()
    {
        int_TL++;
        Text_TL.text = int_TL.ToString();
    }
    //左手G键按下
    private void GetHandleLeftGDown()
    {
        int_GL++;
        Text_GL.text = int_GL.ToString();
    }
    //左手G键按住
    private void GetHandleLeftG()
    {
        int_GL++;
        Text_GL.text = int_GL.ToString();
    }
    //左手M键按下
    private void GetHandleLeftMDown()
    {
        int_ML++;
        Text_ML.text = int_ML.ToString();
    }
    //左手M键按住
    private void GetHandleLeftM()
    {
        int_ML++;
        Text_ML.text = int_ML.ToString();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/99990
推荐阅读
相关标签
  

闽ICP备14008679号