Search Unity

GamePad Control With XInput In JavaScript, A Simple How To.

Discussion in 'Scripting' started by FutureResident, Apr 23, 2014.

  1. FutureResident

    FutureResident

    Joined:
    Apr 22, 2014
    Posts:
    1
    I had a simple question of how use my game controller in Unity with JavaScript. I thought this problem would of already been solved, and a answer readily available, but the best I could find was people saying it should be possible, not any usable code. This is by no means a difficult job, but it's something simple and might save someone else the time it took me. A million thanks to speps and anyone else who crafted the XInputDotNetPure. https://github.com/speps/XInputDotNet
    While there is no problem with the C# version, I'm just a hobbyist and I like JavaScript.

    You can get the needed package at https://github.com/speps/XInputDotNet/releases

    Where it seems many people had problems was at this point, where in JavaScript you have to set a reference to the DLL in Mono as opposed to coding it in C#.
    To do this.. In Mono..
    Go to the Project menu
    Choose, Edit References...
    Select the tab labeled, .NET Assembly
    Open the Plugins directory
    Open the x86 or x86_64 directory, I think the 64bit is a finer vintage.
    Select XInputDotNetPure.dll and click, Add.
    click OK and your Done!!

    Use the prefix XInputDotNetPure to access the GamePad types, and your good to go. I don't know why, but I have to reset the reference to the XInputDotNetPure.dll every time I restart Mono. It's sort of annoying, I'm sure there is something I've missed.

    Here is the simplest possible JavaScripted version of the C# example that comes with XInputDotNet package. Only thing I changed was the formatting of the output to be a little clearer and literal. I think I could wrap this in to class if just so the coding doesn't looks so crowded because XInputDotNetPure is a long name it would function the same. Questions, Comments, did I screw something up?

    Code (csharp):
    1.  
    2. //The Displayed names and values are verbatim.
    3. //Think of this as an interactive script reference.
    4. //
    5. //
    6. //
    7. var playerIndexSet : boolean = false;
    8. var playerIndex : XInputDotNetPure.PlayerIndex;
    9. var state : XInputDotNetPure.GamePadState;
    10. var prevState : XInputDotNetPure.GamePadState;
    11. var rawState : XInputDotNetPure.GamePadState;
    12. function Update () {
    13.     if(!playerIndexSet || !prevState.IsConnected){
    14.         for(var i : int = 0; i <= 4; i++) {
    15.             var testPlayerIndex : XInputDotNetPure.PlayerIndex = XInputDotNetPure.PlayerIndex.One;
    16.             var testState : XInputDotNetPure.GamePadState = XInputDotNetPure.GamePad.GetState(testPlayerIndex);
    17.             if (testState.IsConnected){
    18.                 playerIndex = testPlayerIndex;
    19.                 playerIndexSet = true;
    20.             }
    21.         }
    22.     }
    23.     prevState = state;
    24.     rawState = state;
    25.     state = XInputDotNetPure.GamePad.GetState(playerIndex);
    26.     XInputDotNetPure.GamePad.SetVibration(playerIndex, state.Triggers.Left, state.Triggers.Right);
    27.  
    28. }
    29. function OnGUI() {
    30.     var text : String = " UnityScript GamePad Debug Tool v1";
    31.     text += "\n XInputDotNetPure.GamePadState availible states";
    32.     text += "\n boolean:    IsConnected  = " + state.IsConnected;
    33.     text += "\n int:            PacketNumber = " + state.PacketNumber;
    34.     text += "\n";
    35.     text += "\n float:      Triggers.   Left = " + state.Triggers.Left + "  Right = " + state.Triggers.Right;
    36.     text += "\n";
    37.     text += "\n String:     DPad.       Up = " + state.DPad.Up + "  Right = " + state.DPad.Right + "  Down = " + state.DPad.Down + "  Left = " + state.DPad.Left;
    38.     text += "\n";
    39.     text += "\n String:     Buttons.    Start = " + state.Buttons.Start + "     Back = " + state.Buttons.Back;
    40.     text += "\n";
    41.     text += "\n String:     Buttons.    LeftStick   = " + state.Buttons.LeftStick;
    42.     text += "\n                             RightStick = " + state.Buttons.RightStick;
    43.     text += "\n";
    44.     text += "\n String:     Buttons.    LeftShoulder   = " + state.Buttons.LeftShoulder;
    45.     text += "\n                             RightShoulder = " + state.Buttons.RightShoulder;
    46.     text += "\n";
    47.     text += "\n String:     Buttons.    X = " + state.Buttons.X + "     Y = " + state.Buttons.Y;
    48.     text += "\n                             A = " + state.Buttons.A + "     B = " + state.Buttons.B;
    49.     text += "\n";
    50.     text += "\n float:      ThumbStick.Left.    X = " + state.ThumbSticks.Left.X;
    51.     text += "\n                                         Y = "  + state.ThumbSticks.Left.Y;
    52.     text += "\n float:      ThumbStick.Right.   X = " + state.ThumbSticks.Right.X;
    53.     text += "\n                                         Y = " + state.ThumbSticks.Right.Y;
    54.     text += "\n XInputDotNetPure.GamePad functions";
    55.     text += "\n XInputDotNetPure.GamePad.SetVibration(String: playerIndex, float: LEFT normalized, RIGHT normalized)";
    56.    
    57.     GUI.Label(new Rect(0, 0, Screen.width, Screen.height), text);
    58. }
    59.  
     
    Last edited: Apr 23, 2014