Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class InputUtility : MonoBehaviour
- {
- private static InputUtility m_instance;
- private static InputUtility Instance { get { return m_instance ?? ( m_instance = new GameObject( "InputUtility" ).AddComponent<InputUtility>() ); } }
- #region Controller
- private bool m_Button_A_down;
- private bool m_Button_B_down;
- private bool m_Button_X_down;
- private bool m_Button_Y_down;
- private bool m_Button_Back_down;
- private float m_LeftX;
- private float m_LeftY;
- #endregion
- #region Keyboard
- // Axis
- private float m_horizontal;
- private float m_vertical;
- private bool m_space_Down;
- #endregion
- #region Mouse
- private float m_mouseX;
- private float m_mouseY;
- private float m_mouseScrollWheel;
- private bool m_mouseLeftDown;
- private bool m_mouseRightDown;
- private bool m_mouseMiddleDown;
- #endregion
- void Awake()
- {
- if ( m_instance == null )
- m_instance = this;
- else
- {
- Debug.LogError( "Multiple input utility scripts found! Make sure to have only ONE input utility script in your scene!" );
- Destroy( this );
- return;
- }
- }
- // Use this for initialization
- void Start()
- {
- TickManager.Instance.lateFixedUpdate += new System.Action( LateFixedUpdate );
- }
- // Update is called once per frame
- void Update()
- {
- #region Controller
- if ( !m_Button_A_down )
- m_Button_A_down = InputExtension.GetButtonDown( GamePadButton.A, GamePadIndex.One );
- if ( !m_Button_B_down )
- m_Button_B_down = InputExtension.GetButtonDown( GamePadButton.B, GamePadIndex.One );
- if ( !m_Button_X_down )
- m_Button_X_down = InputExtension.GetButtonDown( GamePadButton.X, GamePadIndex.One );
- if ( !m_Button_Y_down )
- m_Button_Y_down = InputExtension.GetButtonDown( GamePadButton.Y, GamePadIndex.One );
- if ( !m_Button_Back_down )
- m_Button_Back_down = InputExtension.GetButtonDown( GamePadButton.Back, GamePadIndex.One );
- // Axes
- m_LeftX = InputExtension.GetAxis( GamePadAxis.LeftX, GamePadIndex.One );
- m_LeftY = InputExtension.GetAxis( GamePadAxis.LeftY, GamePadIndex.One );
- #endregion
- #region Keyboard
- // Key Down Events
- if ( !m_space_Down )
- m_space_Down = Input.GetKeyDown( KeyCode.Space );
- // Axes
- m_horizontal = Input.GetAxis( "Horizontal" );
- m_vertical = Input.GetAxis( "Vertical" );
- #endregion
- #region Mouse
- // Key down events
- if ( !m_mouseLeftDown )
- m_mouseLeftDown = Input.GetMouseButtonDown( 0 );
- if ( !m_mouseRightDown )
- m_mouseRightDown = Input.GetMouseButtonDown( 1 );
- if ( !m_mouseMiddleDown )
- m_mouseMiddleDown = Input.GetMouseButtonDown( 2 );
- // Axis
- m_mouseX = Input.GetAxis( "Mouse X" );
- m_mouseY = Input.GetAxis( "Mouse Y" );
- m_mouseScrollWheel = Input.GetAxis( "Mouse ScrollWheel" );
- #endregion
- }
- void LateFixedUpdate()
- {
- #region Controller
- m_Button_A_down = false;
- m_Button_B_down = false;
- m_Button_X_down = false;
- m_Button_Y_down = false;
- m_Button_Back_down = false;
- #endregion
- #region Keyboard
- m_space_Down = false;
- #endregion
- #region Mouse
- m_mouseLeftDown = false;
- m_mouseRightDown = false;
- m_mouseMiddleDown = false;
- #endregion
- }
- void OnDestroy()
- {
- InputExtension.ApplyForceFeedback( XInputDotNetPure.PlayerIndex.One, 0.0f, 0.0f );
- }
- public static bool GetFixedButtonDown( GamePadButton button )
- {
- switch ( button )
- {
- case GamePadButton.A:
- return Instance.m_Button_A_down;
- case GamePadButton.B:
- return Instance.m_Button_B_down;
- case GamePadButton.X:
- return Instance.m_Button_X_down;
- case GamePadButton.Y:
- return Instance.m_Button_Y_down;
- case GamePadButton.Back:
- return Instance.m_Button_Back_down;
- default:
- throw new System.NotImplementedException();
- }
- }
- public static bool GetFixedKeyDown( KeyCode keyCode )
- {
- switch ( keyCode )
- {
- case KeyCode.Space:
- return Instance.m_space_Down;
- default:
- throw new System.NotImplementedException();
- }
- }
- public static bool GetFixedButtonUp( GamePadButton button ) { throw new System.NotImplementedException(); }
- public static bool GetFixedButton( GamePadButton button ) { throw new System.NotImplementedException(); }
- public enum MouseButton
- {
- Left,
- Right,
- Middle
- }
- public static bool GetFixedMouseButtonDown( MouseButton button )
- {
- switch ( button )
- {
- case MouseButton.Left:
- return Instance.m_mouseLeftDown;
- case MouseButton.Right:
- return Instance.m_mouseRightDown;
- case MouseButton.Middle:
- return Instance.m_mouseMiddleDown;
- default:
- throw new System.IndexOutOfRangeException();
- }
- }
- public enum Axis
- {
- HorizontalInput,
- VerticalInput,
- MouseScrollWheel,
- MouseX,
- MouseY
- }
- public static float GetFixedAxis( Axis axis )
- {
- float controllerInput, keyboardInput;
- switch ( axis )
- {
- case Axis.HorizontalInput:
- controllerInput = Instance.m_LeftX;
- keyboardInput = Instance.m_horizontal;
- return ( Mathf.Abs( controllerInput ) > Mathf.Abs( keyboardInput ) ) ? controllerInput : keyboardInput;
- case Axis.VerticalInput:
- controllerInput = Instance.m_LeftY;
- keyboardInput = Instance.m_vertical;
- return ( Mathf.Abs( controllerInput ) > Mathf.Abs( keyboardInput ) ) ? controllerInput : keyboardInput;
- case Axis.MouseScrollWheel:
- return Instance.m_mouseScrollWheel;
- case Axis.MouseX:
- return Instance.m_mouseX;
- case Axis.MouseY:
- return Instance.m_mouseY;
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment