1102 lines
36 KiB
C#
1102 lines
36 KiB
C#
using System;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
#if ENABLE_INPUT_SYSTEM
|
|
using UnityEngine.InputSystem;
|
|
#endif
|
|
#if MOBILE_INPUT
|
|
using UnityStandardAssets.CrossPlatformInput;
|
|
#endif
|
|
|
|
namespace Invector.vCharacterController
|
|
{
|
|
public class vInput : MonoBehaviour
|
|
{
|
|
public delegate void OnChangeInputType(InputDevice type);
|
|
public event OnChangeInputType onChangeInputType;
|
|
private static vInput _instance;
|
|
public static vInput instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
_instance = FindAnyObjectByType<vInput>();
|
|
if (_instance == null)
|
|
{
|
|
_instance = new GameObject("vInputType").AddComponent<vInput>();
|
|
return _instance;
|
|
}
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
public vHUDController hud;
|
|
|
|
#if ENABLE_INPUT_SYSTEM
|
|
private readonly System.Collections.Generic.HashSet<InputAction> enabledActions = new System.Collections.Generic.HashSet<InputAction>();
|
|
#endif
|
|
|
|
void Start()
|
|
{
|
|
if (hud == null) hud = vHUDController.instance;
|
|
|
|
#if UNITY_ANDROID || UNITY_IOS
|
|
// On touch platforms nothing reports Mobile until the player touches a control that
|
|
// routes through GenericInput (D-Pad buttons, camera drag). UI-only interactions like
|
|
// tapping an inventory slot don't go through that path, so without this the device stays
|
|
// stuck on the MouseKeyboard default until some other control happens to fire first.
|
|
inputDevice = InputDevice.Mobile;
|
|
#endif
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM
|
|
foreach (var action in enabledActions)
|
|
{
|
|
if (action != null) action.Disable();
|
|
}
|
|
enabledActions.Clear();
|
|
#endif
|
|
}
|
|
|
|
private InputDevice _inputType = InputDevice.MouseKeyboard;
|
|
[HideInInspector]
|
|
public InputDevice inputDevice
|
|
{
|
|
get { return _inputType; }
|
|
set
|
|
{
|
|
if (_inputType == value) return;
|
|
_inputType = value;
|
|
#if ENABLE_LEGACY_INPUT_MANAGER && !ENABLE_INPUT_SYSTEM
|
|
nextDeviceSwitchTime = Time.unscaledTime + deviceSwitchCooldown;
|
|
#endif
|
|
OnChangeInput();
|
|
}
|
|
}
|
|
|
|
// When both input backends are enabled, ReportInputAction already resolves the active
|
|
// device from the actual control that fired. The legacy polling heuristic below would
|
|
// fight it every frame, so it only runs when the Input System is not available.
|
|
#if ENABLE_LEGACY_INPUT_MANAGER && !ENABLE_INPUT_SYSTEM
|
|
[SerializeField]
|
|
private float joystickAxisDeadzone = 0.2f;
|
|
[SerializeField, Tooltip("Minimum seconds between control scheme switches.")]
|
|
private float deviceSwitchCooldown = 0.5f;
|
|
|
|
private float nextDeviceSwitchTime;
|
|
|
|
void Update()
|
|
{
|
|
if (Time.unscaledTime < nextDeviceSwitchTime) return;
|
|
|
|
switch (inputDevice)
|
|
{
|
|
case InputDevice.MouseKeyboard:
|
|
if (isJoystickInput())
|
|
{
|
|
inputDevice = InputDevice.Joystick;
|
|
|
|
if (hud != null)
|
|
{
|
|
hud.controllerInput = true;
|
|
hud.ShowText("Control scheme changed to Controller", 2f, 0.5f);
|
|
}
|
|
}
|
|
else if (isMobileInput())
|
|
{
|
|
inputDevice = InputDevice.Mobile;
|
|
if (hud != null)
|
|
{
|
|
hud.controllerInput = true;
|
|
hud.ShowText("Control scheme changed to Mobile", 2f, 0.5f);
|
|
}
|
|
}
|
|
break;
|
|
case InputDevice.Joystick:
|
|
if (isMouseKeyboard())
|
|
{
|
|
inputDevice = InputDevice.MouseKeyboard;
|
|
if (hud != null)
|
|
{
|
|
hud.controllerInput = false;
|
|
hud.ShowText("Control scheme changed to Keyboard/Mouse", 2f, 0.5f);
|
|
}
|
|
}
|
|
else if (isMobileInput())
|
|
{
|
|
inputDevice = InputDevice.Mobile;
|
|
if (hud != null)
|
|
{
|
|
hud.controllerInput = true;
|
|
hud.ShowText("Control scheme changed to Mobile", 2f, 0.5f);
|
|
}
|
|
}
|
|
break;
|
|
case InputDevice.Mobile:
|
|
if (isMouseKeyboard())
|
|
{
|
|
inputDevice = InputDevice.MouseKeyboard;
|
|
if (hud != null)
|
|
{
|
|
hud.controllerInput = false;
|
|
hud.ShowText("Control scheme changed to Keyboard/Mouse", 2f, 0.5f);
|
|
}
|
|
}
|
|
else if (isJoystickInput())
|
|
{
|
|
inputDevice = InputDevice.Joystick;
|
|
if (hud != null)
|
|
{
|
|
hud.controllerInput = true;
|
|
hud.ShowText("Control scheme changed to Controller", 2f, 0.5f);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
private bool isMobileInput()
|
|
{
|
|
#if ENABLE_LEGACY_INPUT_MANAGER && UNITY_EDITOR && UNITY_MOBILE
|
|
if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject() && Input.GetMouseButtonDown(0))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
#elif ENABLE_LEGACY_INPUT_MANAGER && MOBILE_INPUT
|
|
if ((EventSystem.current != null && EventSystem.current.IsPointerOverGameObject()) || (Input.touches.Length > 0))
|
|
return true;
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
private bool isMouseKeyboard()
|
|
{
|
|
#if !ENABLE_LEGACY_INPUT_MANAGER || ENABLE_INPUT_SYSTEM || MOBILE_INPUT
|
|
return false;
|
|
#else
|
|
// mouse buttons
|
|
if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2))
|
|
return true;
|
|
// printable keys: inputString is populated only by real keyboard keys (not gamepad
|
|
// buttons), so unlike Input.anyKey it can't false-positive when a controller button is pressed
|
|
if (!string.IsNullOrEmpty(Input.inputString))
|
|
return true;
|
|
// non-printable keys (arrows, modifiers): checked individually since KeyCode enum
|
|
// ranges overlap for gamepad buttons and there is no cheap bulk query for "keyboard only"
|
|
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift) ||
|
|
Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl) ||
|
|
Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt) ||
|
|
Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow) ||
|
|
Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow) ||
|
|
Input.GetKey(KeyCode.Tab) || Input.GetKey(KeyCode.CapsLock))
|
|
return true;
|
|
// mouse movement
|
|
if (Input.GetAxis("Mouse X") != 0.0f || Input.GetAxis("Mouse Y") != 0.0f)
|
|
return true;
|
|
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
private bool isJoystickInput()
|
|
{
|
|
#if ENABLE_LEGACY_INPUT_MANAGER && !ENABLE_INPUT_SYSTEM
|
|
// joystick buttons
|
|
if (Input.GetKey(KeyCode.Joystick1Button0) ||
|
|
Input.GetKey(KeyCode.Joystick1Button1) ||
|
|
Input.GetKey(KeyCode.Joystick1Button2) ||
|
|
Input.GetKey(KeyCode.Joystick1Button3) ||
|
|
Input.GetKey(KeyCode.Joystick1Button4) ||
|
|
Input.GetKey(KeyCode.Joystick1Button5) ||
|
|
Input.GetKey(KeyCode.Joystick1Button6) ||
|
|
Input.GetKey(KeyCode.Joystick1Button7) ||
|
|
Input.GetKey(KeyCode.Joystick1Button8) ||
|
|
Input.GetKey(KeyCode.Joystick1Button9) ||
|
|
Input.GetKey(KeyCode.Joystick1Button10) ||
|
|
Input.GetKey(KeyCode.Joystick1Button11) ||
|
|
Input.GetKey(KeyCode.Joystick1Button12) ||
|
|
Input.GetKey(KeyCode.Joystick1Button13) ||
|
|
Input.GetKey(KeyCode.Joystick1Button14) ||
|
|
Input.GetKey(KeyCode.Joystick1Button15) ||
|
|
Input.GetKey(KeyCode.Joystick1Button16) ||
|
|
Input.GetKey(KeyCode.Joystick1Button17) ||
|
|
Input.GetKey(KeyCode.Joystick1Button18) ||
|
|
Input.GetKey(KeyCode.Joystick1Button19))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
// joystick axis (deadzone filters analog stick drift/noise)
|
|
if (Mathf.Abs(Input.GetAxis("LeftAnalogHorizontal")) > joystickAxisDeadzone ||
|
|
Mathf.Abs(Input.GetAxis("LeftAnalogVertical")) > joystickAxisDeadzone ||
|
|
Mathf.Abs(Input.GetAxis("RightAnalogHorizontal")) > joystickAxisDeadzone ||
|
|
Mathf.Abs(Input.GetAxis("RightAnalogVertical")) > joystickAxisDeadzone ||
|
|
Mathf.Abs(Input.GetAxis("LT")) > joystickAxisDeadzone ||
|
|
Mathf.Abs(Input.GetAxis("RT")) > joystickAxisDeadzone ||
|
|
Mathf.Abs(Input.GetAxis("D-Pad Horizontal")) > joystickAxisDeadzone ||
|
|
Mathf.Abs(Input.GetAxis("D-Pad Vertical")) > joystickAxisDeadzone)
|
|
{
|
|
return true;
|
|
}
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
#if ENABLE_INPUT_SYSTEM
|
|
internal InputAction GetInputAction(InputActionReference reference)
|
|
{
|
|
var action = reference != null ? reference.action : null;
|
|
if (action != null && !action.enabled)
|
|
{
|
|
action.Enable();
|
|
enabledActions.Add(action);
|
|
}
|
|
return action;
|
|
}
|
|
|
|
internal void ReportInputAction(InputAction action, bool actuated)
|
|
{
|
|
if (!actuated || action == null || action.activeControl == null) return;
|
|
|
|
ReportInputDevice(action.activeControl.device);
|
|
}
|
|
|
|
internal void ReportInputDevice(UnityEngine.InputSystem.InputDevice device)
|
|
{
|
|
if (device == null) return;
|
|
|
|
if (device is Touchscreen)
|
|
SetReportedInputDevice(InputDevice.Mobile);
|
|
else if (device is Gamepad || device is UnityEngine.InputSystem.Joystick)
|
|
SetReportedInputDevice(InputDevice.Joystick);
|
|
else if (device is Keyboard || device is Mouse || device is Pointer)
|
|
SetReportedInputDevice(InputDevice.MouseKeyboard);
|
|
}
|
|
|
|
private void SetReportedInputDevice(InputDevice device)
|
|
{
|
|
if (inputDevice == device) return;
|
|
|
|
inputDevice = device;
|
|
if (hud == null) hud = vHUDController.instance;
|
|
if (hud == null) return;
|
|
|
|
switch (device)
|
|
{
|
|
case InputDevice.Joystick:
|
|
hud.ShowText("Control scheme changed to Controller", 2f, 0.5f);
|
|
break;
|
|
case InputDevice.Mobile:
|
|
hud.ShowText("Control scheme changed to Mobile", 2f, 0.5f);
|
|
break;
|
|
default:
|
|
hud.ShowText("Control scheme changed to Keyboard/Mouse", 2f, 0.5f);
|
|
break;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
void OnChangeInput()
|
|
{
|
|
if (hud != null)
|
|
{
|
|
hud.controllerInput = inputDevice != InputDevice.MouseKeyboard;
|
|
}
|
|
if (onChangeInputType != null)
|
|
{
|
|
onChangeInputType(inputDevice);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// INPUT TYPE - check in real time if you are using a joystick, mobile or mouse/keyboard
|
|
/// </summary>
|
|
[HideInInspector]
|
|
public enum InputDevice
|
|
{
|
|
MouseKeyboard,
|
|
Joystick,
|
|
Mobile
|
|
};
|
|
|
|
public enum GenericInputSource
|
|
{
|
|
LegacyInputManager,
|
|
InputSystem
|
|
}
|
|
|
|
public enum GenericInputValueComponent
|
|
{
|
|
Scalar,
|
|
X,
|
|
Y,
|
|
Magnitude
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class GenericInput
|
|
{
|
|
protected InputDevice inputDevice { get { return vInput.instance.inputDevice; } }
|
|
public bool useInput = true;
|
|
[SerializeField]
|
|
private GenericInputSource inputSource;
|
|
#if ENABLE_INPUT_SYSTEM
|
|
[SerializeField]
|
|
private InputActionReference inputAction;
|
|
|
|
public InputActionReference inputActionReference => inputAction;
|
|
#endif
|
|
[SerializeField]
|
|
private bool inputSystemAxis;
|
|
[SerializeField]
|
|
private bool inputSystemAxisInvert;
|
|
[SerializeField]
|
|
private GenericInputValueComponent inputSystemValueComponent;
|
|
[SerializeField, Range(0f, 1f)]
|
|
private float inputSystemRawThreshold = 0.1f;
|
|
|
|
/// <summary>
|
|
/// True when this input reads from an <see cref="InputAction"/> instead of the legacy Input Manager.
|
|
/// </summary>
|
|
public bool usesInputSystem
|
|
{
|
|
get
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM
|
|
return inputSource == GenericInputSource.InputSystem && inputAction != null && inputAction.action != null;
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
}
|
|
[SerializeField]
|
|
private bool isAxisInUse;
|
|
[SerializeField]
|
|
private bool isUnityInput;
|
|
[SerializeField]
|
|
public string keyboard;
|
|
[SerializeField]
|
|
public bool keyboardAxis;
|
|
[SerializeField]
|
|
public string joystick;
|
|
[SerializeField]
|
|
public bool joystickAxis;
|
|
[SerializeField]
|
|
public string mobile;
|
|
[SerializeField]
|
|
public bool mobileAxis;
|
|
|
|
[SerializeField]
|
|
public bool joystickAxisInvert;
|
|
[SerializeField]
|
|
public bool keyboardAxisInvert;
|
|
[SerializeField]
|
|
public bool mobileAxisInvert;
|
|
|
|
public float timeButtonWasPressed;
|
|
public float lastTimeTheButtonWasPressed;
|
|
public bool inButtomTimer;
|
|
private float multTapTimer;
|
|
private int multTapCounter;
|
|
|
|
public bool isAxis
|
|
{
|
|
get
|
|
{
|
|
if (usesInputSystem) return inputSystemAxis;
|
|
bool value = false;
|
|
switch (inputDevice)
|
|
{
|
|
case InputDevice.Joystick:
|
|
value = joystickAxis;
|
|
break;
|
|
case InputDevice.MouseKeyboard:
|
|
value = keyboardAxis;
|
|
break;
|
|
case InputDevice.Mobile:
|
|
value = mobileAxis;
|
|
break;
|
|
}
|
|
return value;
|
|
}
|
|
}
|
|
|
|
public bool isAxisInvert
|
|
{
|
|
get
|
|
{
|
|
if (usesInputSystem) return inputSystemAxisInvert;
|
|
bool value = false;
|
|
switch (inputDevice)
|
|
{
|
|
case InputDevice.Joystick:
|
|
value = joystickAxisInvert;
|
|
break;
|
|
case InputDevice.MouseKeyboard:
|
|
value = keyboardAxisInvert;
|
|
break;
|
|
case InputDevice.Mobile:
|
|
value = mobileAxisInvert;
|
|
break;
|
|
}
|
|
return value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initialise a new GenericInput
|
|
/// </summary>
|
|
/// <param name="keyboard"></param>
|
|
/// <param name="joystick"></param>
|
|
/// <param name="mobile"></param>
|
|
public GenericInput(string keyboard, string joystick, string mobile)
|
|
{
|
|
this.keyboard = keyboard;
|
|
this.joystick = joystick;
|
|
this.mobile = mobile;
|
|
|
|
GenerateKeyCodeHash();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initialise a new GenericInput
|
|
/// </summary>
|
|
/// <param name="keyboard"></param>
|
|
/// <param name="joystick"></param>
|
|
/// <param name="mobile"></param>
|
|
public GenericInput(string keyboard, bool keyboardAxis, string joystick, bool joystickAxis, string mobile, bool mobileAxis)
|
|
{
|
|
this.keyboard = keyboard;
|
|
this.keyboardAxis = keyboardAxis;
|
|
this.joystick = joystick;
|
|
this.joystickAxis = joystickAxis;
|
|
this.mobile = mobile;
|
|
this.mobileAxis = mobileAxis;
|
|
|
|
GenerateKeyCodeHash();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initialise a new GenericInput
|
|
/// </summary>
|
|
/// <param name="keyboard"></param>
|
|
/// <param name="joystick"></param>
|
|
/// <param name="mobile"></param>
|
|
public GenericInput(string keyboard, bool keyboardAxis, bool keyboardInvert, string joystick, bool joystickAxis, bool joystickInvert, string mobile, bool mobileAxis, bool mobileInvert)
|
|
{
|
|
this.keyboard = keyboard;
|
|
this.keyboardAxis = keyboardAxis;
|
|
this.keyboardAxisInvert = keyboardInvert;
|
|
this.joystick = joystick;
|
|
this.joystickAxis = joystickAxis;
|
|
this.joystickAxisInvert = joystickInvert;
|
|
this.mobile = mobile;
|
|
this.mobileAxis = mobileAxis;
|
|
this.mobileAxisInvert = mobileInvert;
|
|
|
|
GenerateKeyCodeHash();
|
|
}
|
|
/// <summary>
|
|
/// Button Name
|
|
/// </summary>
|
|
public string buttonName
|
|
{
|
|
get
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM
|
|
if (usesInputSystem) return inputAction.action.name;
|
|
#endif
|
|
if (vInput.instance != null)
|
|
{
|
|
if (vInput.instance.inputDevice == InputDevice.MouseKeyboard) return keyboard.ToString();
|
|
else if (vInput.instance.inputDevice == InputDevice.Joystick) return joystick;
|
|
else return mobile;
|
|
}
|
|
return string.Empty;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if button is a Key
|
|
/// </summary>
|
|
public bool isKey
|
|
{
|
|
get
|
|
{
|
|
if (usesInputSystem) return false;
|
|
if (vInput.instance != null && !isUnityInput)
|
|
{
|
|
if (string.IsNullOrEmpty(lastButtonName) || lastButtonName != buttonName)
|
|
{
|
|
m_isKey = System.Enum.IsDefined(typeof(KeyCode), buttonName);
|
|
lastButtonName = buttonName;
|
|
}
|
|
if (m_isKey)
|
|
return true;
|
|
isUnityInput = true;
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
string lastButtonName = "";
|
|
bool m_isKey = false;
|
|
|
|
|
|
/// <summary>
|
|
/// Get <see cref="KeyCode"/> value
|
|
/// </summary>
|
|
public KeyCode key
|
|
{
|
|
get
|
|
{
|
|
if (usesInputSystem) return KeyCode.None;
|
|
return (KeyCode)keyCodeHash[buttonName];
|
|
}
|
|
}
|
|
|
|
private static Hashtable keyCodeHash = new Hashtable();
|
|
|
|
static void GenerateKeyCodeHash()
|
|
{
|
|
// only want to generate this hash once
|
|
if (keyCodeHash.Count > 0)
|
|
return;
|
|
|
|
string[] names = Enum.GetNames(typeof(KeyCode));
|
|
foreach (string name in names)
|
|
{
|
|
keyCodeHash.Add(name, (KeyCode)Enum.Parse(typeof(KeyCode), name));
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get Button
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool GetButton()
|
|
{
|
|
if (!useInput) return false;
|
|
#if ENABLE_INPUT_SYSTEM
|
|
if (usesInputSystem)
|
|
{
|
|
if (isAxis) return GetAxisButton();
|
|
var action = GetInputSystemAction();
|
|
var value = (action != null && action.IsPressed()) || vMobileInputActions.GetButton(buttonName);
|
|
vInput.instance.ReportInputAction(action, value);
|
|
return value;
|
|
}
|
|
#endif
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName)) return false;
|
|
if (isAxis) return GetAxisButton();
|
|
|
|
// mobile
|
|
if (inputDevice == InputDevice.Mobile)
|
|
{
|
|
#if MOBILE_INPUT
|
|
return CrossPlatformInputManager.GetButton(this.buttonName);
|
|
#endif
|
|
return false;
|
|
}
|
|
// keyboard/mouse
|
|
else if (inputDevice == InputDevice.MouseKeyboard)
|
|
{
|
|
if (isKey)
|
|
{
|
|
if (Input.GetKey(key))
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
if (Input.GetButton(this.buttonName))
|
|
return true;
|
|
}
|
|
}
|
|
// joystick
|
|
else if (inputDevice == InputDevice.Joystick)
|
|
{
|
|
if (Input.GetButton(this.buttonName))
|
|
return true;
|
|
}
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get ButtonDown
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool GetButtonDown()
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM
|
|
if (!useInput) return false;
|
|
if (usesInputSystem)
|
|
{
|
|
if (isAxis) return GetAxisButtonDown();
|
|
var action = GetInputSystemAction();
|
|
var value = (action != null && action.WasPressedThisFrame()) || vMobileInputActions.GetButtonDown(buttonName);
|
|
vInput.instance.ReportInputAction(action, value);
|
|
return value;
|
|
}
|
|
#endif
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName)) return false;
|
|
if (isAxis) return GetAxisButtonDown();
|
|
// mobile
|
|
if (inputDevice == InputDevice.Mobile)
|
|
{
|
|
#if MOBILE_INPUT
|
|
return CrossPlatformInputManager.GetButtonDown(this.buttonName);
|
|
#endif
|
|
return false;
|
|
}
|
|
// keyboard/mouse
|
|
else if (inputDevice == InputDevice.MouseKeyboard)
|
|
{
|
|
if (isKey)
|
|
{
|
|
if (Input.GetKeyDown(key))
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
if (Input.GetButtonDown(this.buttonName))
|
|
return true;
|
|
}
|
|
}
|
|
// joystick
|
|
else if (inputDevice == InputDevice.Joystick)
|
|
{
|
|
if (Input.GetButtonDown(this.buttonName))
|
|
return true;
|
|
}
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Button Up
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool GetButtonUp()
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM
|
|
if (!useInput) return false;
|
|
if (usesInputSystem)
|
|
{
|
|
if (isAxis) return GetAxisButtonUp();
|
|
var action = GetInputSystemAction();
|
|
var value = (action != null && action.WasReleasedThisFrame()) || vMobileInputActions.GetButtonUp(buttonName);
|
|
vInput.instance.ReportInputAction(action, value);
|
|
return value;
|
|
}
|
|
#endif
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName)) return false;
|
|
if (isAxis) return GetAxisButtonUp();
|
|
|
|
// mobile
|
|
if (inputDevice == InputDevice.Mobile)
|
|
{
|
|
#if MOBILE_INPUT
|
|
return CrossPlatformInputManager.GetButtonUp(this.buttonName);
|
|
#endif
|
|
return false;
|
|
}
|
|
// keyboard/mouse
|
|
else if (inputDevice == InputDevice.MouseKeyboard)
|
|
{
|
|
if (isKey)
|
|
{
|
|
if (Input.GetKeyUp(key))
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
if (Input.GetButtonUp(this.buttonName))
|
|
return true;
|
|
}
|
|
}
|
|
// joystick
|
|
else if (inputDevice == InputDevice.Joystick)
|
|
{
|
|
if (Input.GetButtonUp(this.buttonName))
|
|
return true;
|
|
}
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Axis
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public float GetAxis()
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM
|
|
if (!useInput) return 0f;
|
|
if (usesInputSystem) return ReadInputSystemAxis(false);
|
|
#endif
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName) || isKey) return 0;
|
|
|
|
// mobile
|
|
if (inputDevice == InputDevice.Mobile)
|
|
{
|
|
#if MOBILE_INPUT
|
|
return CrossPlatformInputManager.GetAxis(this.buttonName);
|
|
#endif
|
|
}
|
|
// keyboard/mouse
|
|
else if (inputDevice == InputDevice.MouseKeyboard)
|
|
{
|
|
return Input.GetAxis(this.buttonName);
|
|
}
|
|
// joystick
|
|
else if (inputDevice == InputDevice.Joystick)
|
|
{
|
|
return Input.GetAxis(this.buttonName);
|
|
}
|
|
#endif
|
|
return 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Axis Raw
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public float GetAxisRaw()
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM
|
|
if (!useInput) return 0f;
|
|
if (usesInputSystem) return ReadInputSystemAxis(true);
|
|
#endif
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
if (string.IsNullOrEmpty(buttonName) || !IsButtonAvailable(this.buttonName) || isKey) return 0;
|
|
|
|
// mobile
|
|
if (inputDevice == InputDevice.Mobile)
|
|
{
|
|
#if MOBILE_INPUT
|
|
return CrossPlatformInputManager.GetAxisRaw(this.buttonName);
|
|
#endif
|
|
}
|
|
// keyboard/mouse
|
|
else if (inputDevice == InputDevice.MouseKeyboard)
|
|
{
|
|
return Input.GetAxisRaw(this.buttonName);
|
|
}
|
|
// joystick
|
|
else if (inputDevice == InputDevice.Joystick)
|
|
{
|
|
return Input.GetAxisRaw(this.buttonName);
|
|
}
|
|
#endif
|
|
return 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Double Button Down Check if button is pressed Within the defined time
|
|
/// </summary>
|
|
/// <param name="inputTime"></param>
|
|
/// <returns></returns>
|
|
public bool GetDoubleButtonDown(float inputTime = 1)
|
|
{
|
|
if (!IsAvailable()) return false;
|
|
|
|
|
|
if (multTapCounter == 0 && GetButtonDown())
|
|
{
|
|
multTapTimer = Time.time;
|
|
multTapCounter = 1;
|
|
return false;
|
|
}
|
|
else if (multTapCounter == 1 && GetButtonDown())
|
|
{
|
|
var time = multTapTimer + inputTime;
|
|
var valid = (Time.time < time);
|
|
multTapTimer = 0;
|
|
multTapCounter = 0;
|
|
return valid;
|
|
}
|
|
else if (multTapCounter == 1 && multTapTimer + inputTime < Time.time)
|
|
{
|
|
multTapTimer = 0;
|
|
multTapCounter = 0;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Buttom Timer Check if a button is pressed for defined time
|
|
/// </summary>
|
|
/// <param name="inputTime"> time to check button press</param>
|
|
/// <returns></returns>
|
|
public bool GetButtonTimer(float inputTime = 2)
|
|
{
|
|
if (!IsAvailable()) return false;
|
|
if (GetButtonDown() && !inButtomTimer)
|
|
{
|
|
lastTimeTheButtonWasPressed = Time.time + 0.1f;
|
|
timeButtonWasPressed = Time.time;
|
|
inButtomTimer = true;
|
|
}
|
|
if (inButtomTimer)
|
|
{
|
|
var time = timeButtonWasPressed + inputTime;
|
|
var valid = (time - Time.time <= 0);
|
|
|
|
if (!GetButton() || lastTimeTheButtonWasPressed < Time.time)
|
|
{
|
|
inButtomTimer = false;
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
lastTimeTheButtonWasPressed = Time.time + 0.1f;
|
|
}
|
|
if (valid)
|
|
{
|
|
inButtomTimer = false;
|
|
}
|
|
return valid;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Buttom Timer Check if a button is pressed for defined time
|
|
/// </summary>
|
|
/// <param name="inputTime"> time to check button press</param>
|
|
/// <returns></returns>
|
|
public bool GetButtonTimer(ref float currentTimer, float inputTime = 2)
|
|
{
|
|
if (!IsAvailable()) return false;
|
|
if (GetButtonDown() && !inButtomTimer)
|
|
{
|
|
lastTimeTheButtonWasPressed = Time.time + 0.1f;
|
|
timeButtonWasPressed = Time.time;
|
|
inButtomTimer = true;
|
|
}
|
|
if (inButtomTimer)
|
|
{
|
|
var time = timeButtonWasPressed + inputTime;
|
|
currentTimer = time - Time.time;
|
|
var valid = (time - Time.time <= 0);
|
|
|
|
if (!GetButton() || lastTimeTheButtonWasPressed < Time.time)
|
|
{
|
|
inButtomTimer = false;
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
lastTimeTheButtonWasPressed = Time.time + 0.1f;
|
|
}
|
|
if (valid)
|
|
{
|
|
inButtomTimer = false;
|
|
}
|
|
return valid;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Buttom Timer Check if a button is pressed for defined time
|
|
/// </summary>
|
|
/// <param name="inputTime"> time to check button press</param>
|
|
/// <returns></returns>
|
|
public bool GetButtonTimer(ref float currentTimer, ref bool upAfterPressed, float inputTime = 2)
|
|
{
|
|
if (!IsAvailable()) return false;
|
|
if (GetButtonDown())
|
|
{
|
|
lastTimeTheButtonWasPressed = Time.time + 0.1f;
|
|
timeButtonWasPressed = Time.time;
|
|
inButtomTimer = true;
|
|
}
|
|
if (inButtomTimer)
|
|
{
|
|
var time = timeButtonWasPressed + inputTime;
|
|
currentTimer = (inputTime - (time - Time.time)) / inputTime;
|
|
var valid = (time - Time.time <= 0);
|
|
|
|
if (!GetButton() || lastTimeTheButtonWasPressed < Time.time)
|
|
{
|
|
inButtomTimer = false;
|
|
upAfterPressed = true;
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
upAfterPressed = false;
|
|
lastTimeTheButtonWasPressed = Time.time + 0.1f;
|
|
}
|
|
if (valid)
|
|
{
|
|
inButtomTimer = false;
|
|
}
|
|
return valid;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Axis like a button
|
|
/// </summary>
|
|
/// <param name="value">Value to check need to be diferent 0</param>
|
|
/// <returns></returns>
|
|
public bool GetAxisButton(float value = 0.5f)
|
|
{
|
|
if (!IsAvailable()) return false;
|
|
if (!usesInputSystem && isAxisInvert) value *= -1f;
|
|
if (value > 0)
|
|
{
|
|
return GetAxisRaw() >= value;
|
|
}
|
|
else if (value < 0)
|
|
{
|
|
return GetAxisRaw() <= value;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Axis like a buttonDown
|
|
/// </summary>
|
|
/// <param name="value">Value to check need to be diferent 0</param>
|
|
/// <returns></returns>
|
|
public bool GetAxisButtonDown(float value = 0.5f)
|
|
{
|
|
if (!IsAvailable()) return false;
|
|
if (!usesInputSystem && isAxisInvert) value *= -1f;
|
|
if (value > 0)
|
|
{
|
|
if (!isAxisInUse && GetAxisRaw() >= value)
|
|
{
|
|
isAxisInUse = true;
|
|
return true;
|
|
}
|
|
else if (isAxisInUse && GetAxisRaw() == 0)
|
|
{
|
|
isAxisInUse = false;
|
|
}
|
|
}
|
|
else if (value < 0)
|
|
{
|
|
if (!isAxisInUse && GetAxisRaw() <= value)
|
|
{
|
|
isAxisInUse = true;
|
|
return true;
|
|
}
|
|
else if (isAxisInUse && GetAxisRaw() == 0)
|
|
{
|
|
isAxisInUse = false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Axis like a buttonUp
|
|
/// Check if Axis is zero after press
|
|
/// <returns></returns>
|
|
public bool GetAxisButtonUp()
|
|
{
|
|
if (!IsAvailable()) return false;
|
|
if (isAxisInUse && GetAxisRaw() == 0)
|
|
{
|
|
isAxisInUse = false;
|
|
return true;
|
|
}
|
|
else if (!isAxisInUse && GetAxisRaw() != 0)
|
|
{
|
|
isAxisInUse = true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private bool IsAvailable()
|
|
{
|
|
if (!useInput) return false;
|
|
#if ENABLE_INPUT_SYSTEM
|
|
if (usesInputSystem) return GetInputSystemAction() != null;
|
|
#endif
|
|
return !string.IsNullOrEmpty(buttonName) && IsButtonAvailable(buttonName);
|
|
}
|
|
|
|
#if ENABLE_INPUT_SYSTEM
|
|
private InputAction GetInputSystemAction()
|
|
{
|
|
return vInput.instance.GetInputAction(inputAction);
|
|
}
|
|
|
|
private float ReadInputSystemAxis(bool raw)
|
|
{
|
|
var action = GetInputSystemAction();
|
|
if (action == null) return 0f;
|
|
|
|
float value;
|
|
var readsVector = inputSystemValueComponent != GenericInputValueComponent.Scalar || action.expectedControlType == "Vector2";
|
|
if (!readsVector)
|
|
{
|
|
value = action.ReadValue<float>();
|
|
}
|
|
else
|
|
{
|
|
var vector = action.ReadValue<Vector2>();
|
|
switch (inputSystemValueComponent)
|
|
{
|
|
case GenericInputValueComponent.Y: value = vector.y; break;
|
|
case GenericInputValueComponent.Magnitude: value = vector.magnitude; break;
|
|
default: value = vector.x; break;
|
|
}
|
|
}
|
|
|
|
if (inputSystemAxisInvert) value *= -1f;
|
|
vInput.instance.ReportInputAction(action, Mathf.Abs(value) > inputSystemRawThreshold);
|
|
if (!raw) return value;
|
|
return value > inputSystemRawThreshold ? 1f : value < -inputSystemRawThreshold ? -1f : 0f;
|
|
}
|
|
#endif
|
|
|
|
bool IsButtonAvailable(string btnName)
|
|
{
|
|
if (!useInput) return false;
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
try
|
|
{
|
|
if (isKey) return true;
|
|
Input.GetButton(buttonName);
|
|
return true;
|
|
}
|
|
catch (System.Exception exc)
|
|
{
|
|
if (isUnityInput)
|
|
{
|
|
isUnityInput = false;
|
|
return isKey;
|
|
}
|
|
Debug.LogWarning($" Failure to try access Unity Input or KeyCode : {buttonName} \n {exc.Message}");
|
|
return false;
|
|
}
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
}
|
|
}
|