ThSchm

InputUtility

Mar 3rd, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.48 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class InputUtility : MonoBehaviour
  5. {
  6.     private static InputUtility m_instance;
  7.     private static InputUtility Instance { get { return m_instance ?? ( m_instance = new GameObject( "InputUtility" ).AddComponent<InputUtility>() ); } }
  8.  
  9.     #region Controller
  10.     private bool m_Button_A_down;
  11.     private bool m_Button_B_down;
  12.     private bool m_Button_X_down;
  13.     private bool m_Button_Y_down;
  14.     private bool m_Button_Back_down;
  15.  
  16.     private float m_LeftX;
  17.     private float m_LeftY;
  18.     #endregion
  19.  
  20.     #region Keyboard
  21.     // Axis
  22.     private float m_horizontal;
  23.     private float m_vertical;
  24.     private bool m_space_Down;
  25.     #endregion
  26.  
  27.     #region Mouse
  28.     private float m_mouseX;
  29.     private float m_mouseY;
  30.     private float m_mouseScrollWheel;
  31.  
  32.     private bool m_mouseLeftDown;
  33.     private bool m_mouseRightDown;
  34.     private bool m_mouseMiddleDown;
  35.     #endregion
  36.  
  37.     void Awake()
  38.     {
  39.         if ( m_instance == null )
  40.             m_instance = this;
  41.         else
  42.         {
  43.             Debug.LogError( "Multiple input utility scripts found! Make sure to have only ONE input utility script in your scene!" );
  44.             Destroy( this );
  45.             return;
  46.         }
  47.     }
  48.  
  49.     // Use this for initialization
  50.     void Start()
  51.     {
  52.         TickManager.Instance.lateFixedUpdate += new System.Action( LateFixedUpdate );
  53.     }
  54.  
  55.     // Update is called once per frame
  56.     void Update()
  57.     {
  58.         #region Controller
  59.         if ( !m_Button_A_down )
  60.             m_Button_A_down = InputExtension.GetButtonDown( GamePadButton.A, GamePadIndex.One );
  61.  
  62.         if ( !m_Button_B_down )
  63.             m_Button_B_down = InputExtension.GetButtonDown( GamePadButton.B, GamePadIndex.One );
  64.  
  65.         if ( !m_Button_X_down )
  66.             m_Button_X_down = InputExtension.GetButtonDown( GamePadButton.X, GamePadIndex.One );
  67.  
  68.         if ( !m_Button_Y_down )
  69.             m_Button_Y_down = InputExtension.GetButtonDown( GamePadButton.Y, GamePadIndex.One );
  70.  
  71.         if ( !m_Button_Back_down )
  72.             m_Button_Back_down = InputExtension.GetButtonDown( GamePadButton.Back, GamePadIndex.One );
  73.  
  74.         // Axes
  75.         m_LeftX = InputExtension.GetAxis( GamePadAxis.LeftX, GamePadIndex.One );
  76.         m_LeftY = InputExtension.GetAxis( GamePadAxis.LeftY, GamePadIndex.One );
  77.  
  78.         #endregion
  79.  
  80.         #region Keyboard
  81.         // Key Down Events
  82.         if ( !m_space_Down )
  83.             m_space_Down = Input.GetKeyDown( KeyCode.Space );
  84.  
  85.         // Axes
  86.         m_horizontal = Input.GetAxis( "Horizontal" );
  87.         m_vertical = Input.GetAxis( "Vertical" );
  88.         #endregion
  89.  
  90.         #region Mouse
  91.         // Key down events
  92.         if ( !m_mouseLeftDown )
  93.             m_mouseLeftDown = Input.GetMouseButtonDown( 0 );
  94.         if ( !m_mouseRightDown )
  95.             m_mouseRightDown = Input.GetMouseButtonDown( 1 );
  96.         if ( !m_mouseMiddleDown )
  97.             m_mouseMiddleDown = Input.GetMouseButtonDown( 2 );
  98.  
  99.         // Axis
  100.         m_mouseX = Input.GetAxis( "Mouse X" );
  101.         m_mouseY = Input.GetAxis( "Mouse Y" );
  102.         m_mouseScrollWheel = Input.GetAxis( "Mouse ScrollWheel" );
  103.         #endregion
  104.     }
  105.  
  106.     void LateFixedUpdate()
  107.     {
  108.         #region Controller
  109.         m_Button_A_down = false;
  110.         m_Button_B_down = false;
  111.         m_Button_X_down = false;
  112.         m_Button_Y_down = false;
  113.         m_Button_Back_down = false;
  114.         #endregion
  115.  
  116.         #region Keyboard
  117.         m_space_Down = false;
  118.         #endregion
  119.  
  120.         #region Mouse
  121.         m_mouseLeftDown = false;
  122.         m_mouseRightDown = false;
  123.         m_mouseMiddleDown = false;
  124.         #endregion
  125.     }
  126.     void OnDestroy()
  127.     {
  128.         InputExtension.ApplyForceFeedback( XInputDotNetPure.PlayerIndex.One, 0.0f, 0.0f );
  129.     }
  130.  
  131.     public static bool GetFixedButtonDown( GamePadButton button )
  132.     {
  133.         switch ( button )
  134.         {
  135.             case GamePadButton.A:
  136.                 return Instance.m_Button_A_down;
  137.             case GamePadButton.B:
  138.                 return Instance.m_Button_B_down;
  139.             case GamePadButton.X:
  140.                 return Instance.m_Button_X_down;
  141.             case GamePadButton.Y:
  142.                 return Instance.m_Button_Y_down;
  143.             case GamePadButton.Back:
  144.                 return Instance.m_Button_Back_down;
  145.             default:
  146.                 throw new System.NotImplementedException();
  147.         }
  148.     }
  149.     public static bool GetFixedKeyDown( KeyCode keyCode )
  150.     {
  151.         switch ( keyCode )
  152.         {
  153.             case KeyCode.Space:
  154.                 return Instance.m_space_Down;
  155.             default:
  156.                 throw new System.NotImplementedException();
  157.         }
  158.     }
  159.     public static bool GetFixedButtonUp( GamePadButton button ) { throw new System.NotImplementedException(); }
  160.     public static bool GetFixedButton( GamePadButton button ) { throw new System.NotImplementedException(); }
  161.  
  162.     public enum MouseButton
  163.     {
  164.         Left,
  165.         Right,
  166.         Middle
  167.     }
  168.  
  169.     public static bool GetFixedMouseButtonDown( MouseButton button )
  170.     {
  171.         switch ( button )
  172.         {
  173.             case MouseButton.Left:
  174.                 return Instance.m_mouseLeftDown;
  175.             case MouseButton.Right:
  176.                 return Instance.m_mouseRightDown;
  177.             case MouseButton.Middle:
  178.                 return Instance.m_mouseMiddleDown;
  179.             default:
  180.                 throw new System.IndexOutOfRangeException();
  181.         }
  182.     }
  183.  
  184.     public enum Axis
  185.     {
  186.         HorizontalInput,
  187.         VerticalInput,
  188.         MouseScrollWheel,
  189.         MouseX,
  190.         MouseY
  191.     }
  192.  
  193.     public static float GetFixedAxis( Axis axis )
  194.     {
  195.         float controllerInput, keyboardInput;
  196.  
  197.         switch ( axis )
  198.         {
  199.             case Axis.HorizontalInput:
  200.                 controllerInput = Instance.m_LeftX;
  201.                 keyboardInput = Instance.m_horizontal;
  202.                 return ( Mathf.Abs( controllerInput ) > Mathf.Abs( keyboardInput ) ) ? controllerInput : keyboardInput;
  203.             case Axis.VerticalInput:
  204.                 controllerInput = Instance.m_LeftY;
  205.                 keyboardInput = Instance.m_vertical;
  206.                 return ( Mathf.Abs( controllerInput ) > Mathf.Abs( keyboardInput ) ) ? controllerInput : keyboardInput;
  207.             case Axis.MouseScrollWheel:
  208.                 return Instance.m_mouseScrollWheel;
  209.             case Axis.MouseX:
  210.                 return Instance.m_mouseX;
  211.             case Axis.MouseY:
  212.                 return Instance.m_mouseY;
  213.  
  214.         }
  215.  
  216.         return 0;
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment