Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class XInput
- {
- public enum DPad
- {
- Left,
- Right,
- Up,
- Down
- }
- public enum Button
- {
- None,
- A,
- B,
- X,
- Y,
- Back,
- Start,
- Home,
- BumperLeft,
- BumperRight,
- StickClickLeft,
- StickClickRight,
- Max
- }
- public int index = 0;
- public Vector2 leftStick;
- public Vector2 rightStick;
- public float triggerLeft;
- public float triggerRight;
- public Vector2 dPad;
- public Vector3 oldDPad;
- private const float DEAD_ZONE = 0.33f;
- public bool GetButtonDown(Button button)
- {
- return Input.GetKeyDown(ButtonToKeyCode(button));
- }
- public bool GetButtonUp(Button button)
- {
- return Input.GetKeyUp(ButtonToKeyCode(button));
- }
- public bool GetButton(Button button)
- {
- return Input.GetKey(ButtonToKeyCode(button));
- }
- public KeyCode GetButtonForJoystick(int button)
- {
- if (index == 0)
- {
- return (KeyCode)((int)KeyCode.Joystick1Button0 + button);
- }
- else if (index == 1)
- {
- return (KeyCode)((int)KeyCode.Joystick2Button0 + button);
- }
- else if (index == 2)
- {
- return (KeyCode)((int)KeyCode.Joystick3Button0 + button);
- }
- else if (index == 3)
- {
- return (KeyCode)((int)KeyCode.Joystick4Button0 + button);
- }
- return KeyCode.None;
- }
- public KeyCode ButtonToKeyCode(Button button)
- {
- if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXWebPlayer)
- {
- switch (button)
- {
- case Button.A:
- return GetButtonForJoystick(16);
- case Button.B:
- return GetButtonForJoystick(17);
- case Button.X:
- return GetButtonForJoystick(18);
- case Button.Y:
- return GetButtonForJoystick(19);
- case Button.BumperLeft:
- return GetButtonForJoystick(13);
- case Button.BumperRight:
- return GetButtonForJoystick(14);
- case Button.Back:
- return GetButtonForJoystick(10);
- case Button.Start:
- return GetButtonForJoystick(9);
- case Button.StickClickLeft:
- return GetButtonForJoystick(11);
- case Button.StickClickRight:
- return GetButtonForJoystick(12);
- }
- }
- else if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsWebPlayer)
- {
- switch (button)
- {
- case Button.A:
- return GetButtonForJoystick(0);
- case Button.B:
- return GetButtonForJoystick(1);
- case Button.X:
- return GetButtonForJoystick(2);
- case Button.Y:
- return GetButtonForJoystick(3);
- case Button.BumperLeft:
- return GetButtonForJoystick(4);
- case Button.BumperRight:
- return GetButtonForJoystick(5);
- case Button.Back:
- return GetButtonForJoystick(6);
- case Button.Start:
- return GetButtonForJoystick(7);
- case Button.StickClickLeft:
- return GetButtonForJoystick(8);
- case Button.StickClickRight:
- return GetButtonForJoystick(9);
- }
- }
- return KeyCode.None;
- }
- public void Update()
- {
- if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor)
- {
- //Mac OSX
- triggerLeft = GetAxisRaw(5);
- triggerRight = GetAxisRaw(6);
- leftStick = GetAxisVec(1, 2);
- rightStick = GetAxisVec(3, 4);
- oldDPad = dPad;
- dPad.x = (Input.GetKeyDown(GetButtonForJoystick(8))?1.0f:0.0f) + (Input.GetKeyDown(GetButtonForJoystick(7))?-1.0f:0.0f);
- dPad.y = (Input.GetKeyDown(GetButtonForJoystick(6))?1.0f:0.0f) + (Input.GetKeyDown(GetButtonForJoystick(5))?-1.0f:0.0f);
- }
- else if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
- {
- //Windows
- float triggers = Input.GetAxis("XPCTriggers" + (index+1));
- if (triggers < -Mathf.Epsilon)
- triggerRight = -triggers;
- else if (triggers > Mathf.Epsilon)
- triggerLeft = triggers;
- else
- triggerLeft = triggerRight = 0.0f;
- leftStick = GetAxisVec(1, 2);
- rightStick = GetAxisVec(4, 5);
- //Debug.LogWarning("player: " + index + " leftStick: " + leftStick + " rightStick: " + rightStick);
- oldDPad = dPad;
- dPad = GetAxisVec(6, 7);
- }
- }
- float GetAxisRaw(int axis)
- {
- return Input.GetAxisRaw("Joy" + (index+1) + "Axis" + axis);
- }
- Vector2 GetAxisVecRaw(int axis1, int axis2)
- {
- return new Vector2(Input.GetAxisRaw("Joy" + (index+1) + "Axis" + axis1), Input.GetAxisRaw("Joy" + (index+1) + "Axis" + axis2));
- }
- Vector2 GetAxisVec(int axis1, int axis2)
- {
- Vector2 vec = GetAxisVecRaw(axis1, axis2);
- if (Mathf.Abs(vec.x) < DEAD_ZONE)
- {
- vec.x = 0.0f;
- }
- if (Mathf.Abs(vec.y) < DEAD_ZONE)
- {
- vec.y = 0.0f;
- }
- return vec;
- }
- public bool GetDPadDown(DPad dPadEnum)
- {
- switch (dPadEnum)
- {
- case DPad.Left:
- return (dPad.x < 0.0f && oldDPad.x == 0.0f);
- case DPad.Right:
- return (dPad.x > 0.0f && oldDPad.x == 0.0f);
- case DPad.Up:
- return (dPad.y > 0.0f && oldDPad.y == 0.0f);
- case DPad.Down:
- return (dPad.y < 0.0f && oldDPad.y == 0.0f);
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement