Advertisement
Guest User

Untitled

a guest
Jan 12th, 2011
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1. using UnityEngine;
  2. using XInputDotNetPure; // Required in C#
  3.  
  4. public class XInputTest : MonoBehaviour
  5. {
  6.     bool playerIndexSet = false;
  7.     PlayerIndex playerIndex;
  8.     GamePadState state;
  9.     GamePadState prevState;
  10.    
  11.     GameObject textObject;
  12.     GameObject cubeObject;
  13.     float cubeAngle = 0.0f;
  14.  
  15.     // Use this for initialization
  16.     void Start()
  17.     {
  18.         // No need to initialize anything for the plugin
  19.        
  20.         // However, I need that for the purpose of the demo
  21.         textObject = GameObject.Find("GUI Text");
  22.         cubeObject = GameObject.Find("Cube");
  23.     }
  24.  
  25.     // Update is called once per frame
  26.     void Update()
  27.     {
  28.         // Find a PlayerIndex, for a single player game
  29.         if (!playerIndexSet || !prevState.IsConnected)
  30.         {
  31.             for (int i = 0; i < 4; ++i)
  32.             {
  33.                 PlayerIndex testPlayerIndex = (PlayerIndex)i;
  34.                 GamePadState testState = GamePad.GetState(testPlayerIndex);
  35.                 if (testState.IsConnected)
  36.                 {
  37.                     Debug.Log(string.Format("GamePad found {0}", testPlayerIndex));
  38.                     playerIndex = testPlayerIndex;
  39.                     playerIndexSet = true;
  40.                 }
  41.             }
  42.         }
  43.  
  44.         state = GamePad.GetState(playerIndex);
  45.  
  46.         string text = "Use left stick to turn the cube\n";
  47.         text += string.Format("IsConnected {0} Packet #{1}\n", state.IsConnected, state.PacketNumber);
  48.         text += string.Format("\tTriggers {0} {1}\n", state.Triggers.Left, state.Triggers.Right);
  49.         text += string.Format("\tD-Pad {0} {1} {2} {3}\n", state.DPad.Up, state.DPad.Right, state.DPad.Down, state.DPad.Left);
  50.         text += string.Format("\tButtons Start {0} Back {1}\n", state.Buttons.Start, state.Buttons.Back);
  51.         text += string.Format("\tButtons LeftStick {0} RightStick {1} LeftShoulder {2} RightShoulder {3}\n", state.Buttons.LeftStick, state.Buttons.RightStick, state.Buttons.LeftShoulder, state.Buttons.RightShoulder);
  52.         text += string.Format("\tButtons A {0} B {1} X {2} Y {3}\n", state.Buttons.A, state.Buttons.B, state.Buttons.X, state.Buttons.Y);
  53.         text += string.Format("\tSticks Left {0} {1} Right {2} {3}\n", state.ThumbSticks.Left.X, state.ThumbSticks.Left.Y, state.ThumbSticks.Right.X, state.ThumbSticks.Right.Y);
  54.         GamePad.SetVibration(playerIndex, state.Triggers.Left, state.Triggers.Right);
  55.        
  56.         // Display the state information
  57.         textObject.guiText.text = text;
  58.        
  59.         // Make the cube turn
  60.         cubeAngle += state.ThumbSticks.Left.X * 25.0f * Time.deltaTime;
  61.         cubeObject.transform.localRotation = Quaternion.Euler(0.0f, cubeAngle, 0.0f);
  62.        
  63.         prevState = state;
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement