Update
This commit is contained in:
@@ -1,99 +0,0 @@
|
||||
/// ---------------------------------------------
|
||||
/// Ultimate Character Controller
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
|
||||
namespace Opsive.UltimateCharacterController.Input
|
||||
{
|
||||
/// <summary>
|
||||
/// The ActiveInputEvent class allows the object to receive input callbacks while the object is active.
|
||||
/// </summary>
|
||||
public class ActiveInputEvent
|
||||
{
|
||||
// Specifies any inputs that should be received while the object is active.
|
||||
public enum Type
|
||||
{
|
||||
ButtonDown, // The object will receive input when the specified button is down.
|
||||
ButtonUp, // The object will receive input when the specified button is up.
|
||||
DoublePress, // The object will receive input when the specified input is pressed twice.
|
||||
LongPress, // The object will receive input when the specified button has been pressed for more the than the specified duration.
|
||||
Axis // The object will receive input for the axis.
|
||||
}
|
||||
|
||||
private Type m_InputType;
|
||||
private string m_InputName;
|
||||
private float m_LongPressDuration;
|
||||
private bool m_WaitForLongPressRelease;
|
||||
private string m_EventName;
|
||||
|
||||
public string EventName { get { return m_EventName; } }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the ActiveInputEvent object to the specified values.
|
||||
/// </summary>
|
||||
/// <param name="inputType">Specifies how the event should be triggered.</param>
|
||||
/// <param name="inputName">The button name that will trigger the event.</param>
|
||||
/// <param name="eventName">The event to execute.</param>
|
||||
public void Initialize(Type inputType, string inputName, string eventName)
|
||||
{
|
||||
Initialize(inputType, inputName, 0, false, eventName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the ActiveInputEvent object to the specified values.
|
||||
/// </summary>
|
||||
/// <param name="inputType">Specifies how the event should be triggered.</param>
|
||||
/// <param name="inputName">The button name that will trigger the event.</param>
|
||||
/// <param name="longPressDuration">Specifies how long the button should be pressed until the event is executed. Only used with a Type of LongPress.</param>
|
||||
/// <param name="waitForLongPressRelease">Should the long press wait to be activated until the button has been released?</param>
|
||||
/// <param name="eventName">The event to execute.</param>
|
||||
public void Initialize(Type inputType, string inputName, float longPressDuration, bool waitForLongPressRelease, string eventName)
|
||||
{
|
||||
m_InputType = inputType;
|
||||
m_InputName = inputName;
|
||||
m_LongPressDuration = longPressDuration;
|
||||
m_WaitForLongPressRelease = waitForLongPressRelease;
|
||||
m_EventName = eventName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the button at the specified type has been triggered.
|
||||
/// </summary>
|
||||
/// <param name="playerInput">A reference to the input component.</param>
|
||||
/// <returns>True if the button at the specified type has been triggered.</returns>
|
||||
public bool HasButtonEvent(PlayerInput playerInput)
|
||||
{
|
||||
if ((m_InputType == Type.ButtonDown && playerInput.GetButtonDown(m_InputName)) ||
|
||||
(m_InputType == Type.ButtonUp && playerInput.GetButtonUp(m_InputName)) ||
|
||||
(m_InputType == Type.DoublePress && playerInput.GetDoublePress(m_InputName)) ||
|
||||
(m_InputType == Type.LongPress && playerInput.GetLongPress(m_InputName, m_LongPressDuration, m_WaitForLongPressRelease))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the AxisInputMap is using an axis.
|
||||
/// </summary>
|
||||
/// <param name="playerInput">A reference to the input component.</param>
|
||||
/// <returns></returns>
|
||||
public bool HasAxisEvent(PlayerInput playerInput)
|
||||
{
|
||||
if (m_InputType == Type.Axis) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the axis value which maps to the input at the specified name.
|
||||
/// </summary>
|
||||
/// <param name="playerInput">A reference to the input component.</param>
|
||||
/// <returns>The axis value which maps to the input at the specified name.</returns>
|
||||
public float GetAxisValue(PlayerInput playerInput)
|
||||
{
|
||||
return playerInput.GetAxisRaw(m_InputName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2c26920fb4c54e1408549832618573f4
|
||||
timeCreated: 1499456690
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,53 +0,0 @@
|
||||
/// ---------------------------------------------
|
||||
/// Ultimate Character Controller
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
|
||||
namespace Opsive.UltimateCharacterController.Input
|
||||
{
|
||||
/// <summary>
|
||||
/// The base class for both mobile and standalone (keyboard/mouse and controller) input. This base class exists so UnityInput doesn't need to know if it
|
||||
/// is working with mobile controls or standalone controls.
|
||||
/// </summary>
|
||||
public abstract class InputBase
|
||||
{
|
||||
/// <summary>
|
||||
/// The type of button action to check against.
|
||||
/// </summary>
|
||||
public enum ButtonAction { GetButton, GetButtonDown, GetButtonUp }
|
||||
|
||||
protected PlayerInput m_PlayerInput;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the UnityInputBase.
|
||||
/// </summary>
|
||||
/// <param name="unityInput">A reference to the PlayerInput component.</param>
|
||||
public void Initialize(PlayerInput playerInput)
|
||||
{
|
||||
m_PlayerInput = playerInput;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if the button is true with the specified ButtonAction.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the button.</param>
|
||||
/// <param name="action">The type of action to check.</param>
|
||||
/// <returns>The status of the action.</returns>
|
||||
public abstract bool GetButton(string name, ButtonAction action);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the axis of the specified button.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the axis.</param>
|
||||
/// <returns>The axis value.</returns>
|
||||
public abstract float GetAxis(string name);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the raw axis of the specified button.
|
||||
/// </summary>
|
||||
/// <param name="axisName">The name of the axis.</param>
|
||||
/// <returns>The raw axis value.</returns>
|
||||
public abstract float GetAxisRaw(string axisName);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 558cf5f9fd072484e9e1f15a01a0b366
|
||||
timeCreated: 1481260746
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,512 +0,0 @@
|
||||
/// ---------------------------------------------
|
||||
/// Ultimate Character Controller
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
|
||||
namespace Opsive.UltimateCharacterController.Input
|
||||
{
|
||||
using Opsive.Shared.Events;
|
||||
using Opsive.Shared.Game;
|
||||
using Opsive.UltimateCharacterController.Events;
|
||||
using Opsive.UltimateCharacterController.Utility;
|
||||
using Opsive.UltimateCharacterController.StateSystem;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract class to expose a common interface for any input implementation.
|
||||
/// </summary>
|
||||
public abstract class PlayerInput : StateBehavior
|
||||
{
|
||||
/// <summary>
|
||||
/// Specifies how to set the look vector.
|
||||
/// </summary>
|
||||
public enum LookVectorMode
|
||||
{
|
||||
Smoothed, // A smoothing will be applied to the look vector.
|
||||
UnitySmoothed, // The smoothed Unity input values will be used.
|
||||
Raw, // The raw input values will be used.
|
||||
Manual // The look vector is assigned manually. This is useful for VR head movement.
|
||||
}
|
||||
|
||||
[Tooltip("The name of the horizontal camera input mapping.")]
|
||||
[SerializeField] protected string m_HorizontalLookInputName = "Mouse X";
|
||||
[Tooltip("The name of the vertical camera input mapping.")]
|
||||
[SerializeField] protected string m_VerticalLookInputName = "Mouse Y";
|
||||
[Tooltip("Specifies how the look vector is assigned.")]
|
||||
[SerializeField] protected LookVectorMode m_LookVectorMode = LookVectorMode.Smoothed;
|
||||
[Tooltip("If using look smoothing, specifies how sensitive the mouse is. The higher the value the more sensitive.")]
|
||||
[SerializeField] protected Vector2 m_LookSensitivity = new Vector2(2f, 2f);
|
||||
[Tooltip("If using look smoothing, specifies a multiplier to apply to the LookSensitivity value.")]
|
||||
[SerializeField] protected float m_LookSensitivityMultiplier = 1;
|
||||
[Tooltip("If using look smoothing, the amount of history to store of previous look values.")]
|
||||
[SerializeField] protected int m_SmoothLookSteps = 20;
|
||||
[Tooltip("If using look smoothing, specifies how much weight each element should have on the total smoothed value (range 0-1).")]
|
||||
[SerializeField] protected float m_SmoothLookWeight = 0.5f;
|
||||
[Tooltip("If using look smoothing, specifies an exponent to give a smoother feel with smaller inputs.")]
|
||||
[SerializeField] protected float m_SmoothExponent = 1.05f;
|
||||
[Tooltip("If using look smoothing, specifies a maximum acceleration value of the smoothed look value (0 to disable).")]
|
||||
[SerializeField] protected float m_LookAccelerationThreshold = 0.4f;
|
||||
[Tooltip("The rate (in seconds) the component checks to determine if a controller is connected.")]
|
||||
[SerializeField] protected float m_ControllerConnectedCheckRate = 1f;
|
||||
[Tooltip("The state that should be activated when a controller is connected.")]
|
||||
[SerializeField] protected string m_ConnectedControllerState = "ConnectedController";
|
||||
[Tooltip("Unity event invoked when the gameplay input is enabled or disabled.")]
|
||||
[SerializeField] protected UnityBoolEvent m_EnableGamplayInputEvent;
|
||||
|
||||
public string HorizontalLookInputName { get { return m_HorizontalLookInputName; } set { m_HorizontalLookInputName = value; } }
|
||||
public string VerticalLookInputName { get { return m_VerticalLookInputName; } set { m_VerticalLookInputName = value; } }
|
||||
public LookVectorMode LookMode { get { return m_LookVectorMode; }
|
||||
set
|
||||
{
|
||||
m_LookVectorMode = value;
|
||||
if (m_LookVectorMode == LookVectorMode.Smoothed && m_SmoothLookBuffer == null) {
|
||||
m_SmoothLookBuffer = new Vector2[m_SmoothLookSteps];
|
||||
}
|
||||
}
|
||||
}
|
||||
public Vector2 LookSensitivity { get { return m_LookSensitivity; } set { m_LookSensitivity = value; } }
|
||||
public float LookSensitivityMultiplier { get { return m_LookSensitivityMultiplier; } set { m_LookSensitivityMultiplier = value; } }
|
||||
public int SmoothLookSteps { get { return m_SmoothLookSteps; } set { m_SmoothLookSteps = value; } }
|
||||
public float SmoothLookWeight { get { return m_SmoothLookWeight; } set { m_SmoothLookWeight = value; } }
|
||||
public float SmoothExponent { get { return m_SmoothExponent; } set { m_SmoothExponent = value; } }
|
||||
public float LookAccelerationThreshold { get { return m_LookAccelerationThreshold; } set { m_LookAccelerationThreshold = value; } }
|
||||
public float ControllerConnectedCheckRate { get { return m_ControllerConnectedCheckRate; } set { m_ControllerConnectedCheckRate = value; } }
|
||||
public string ConnectedControllerState { get { return m_ConnectedControllerState; } set { m_ConnectedControllerState = value; } }
|
||||
public UnityBoolEvent EnableGameplayInputEvent { get { return m_EnableGamplayInputEvent; } set { m_EnableGamplayInputEvent = value; } }
|
||||
|
||||
private Vector2[] m_SmoothLookBuffer;
|
||||
private int m_SmoothLookBufferIndex;
|
||||
private int m_SmoothLookBufferCount;
|
||||
protected Vector2 m_RawLookVector;
|
||||
protected Vector2 m_CurrentLookVector;
|
||||
private float m_TimeScale = 1;
|
||||
private bool m_ControllerConnected;
|
||||
private Dictionary<string, float> m_ButtonDownTime;
|
||||
private Dictionary<string, float> m_ButtonUpTime;
|
||||
private ScheduledEventBase m_ControllerCheckEvent;
|
||||
private bool m_AllowInput = true;
|
||||
private bool m_Death = false;
|
||||
|
||||
public Vector2 RawLookVector { set { m_RawLookVector = value; } }
|
||||
public Vector2 CurrentLookVector { set { m_CurrentLookVector = value; } }
|
||||
public bool ControllerConnected { get { return m_ControllerConnected; } }
|
||||
|
||||
protected virtual bool CanCheckForController { get { return true; } }
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the default values.
|
||||
/// </summary>
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
|
||||
if (m_LookVectorMode == LookVectorMode.Smoothed) {
|
||||
m_SmoothLookBuffer = new Vector2[m_SmoothLookSteps];
|
||||
}
|
||||
|
||||
EventHandler.RegisterEvent<float>(gameObject, "OnCharacterChangeTimeScale", ChangeTimeScale);
|
||||
EventHandler.RegisterEvent<bool>(gameObject, "OnEnableGameplayInput", EnableGameplayInput);
|
||||
EventHandler.RegisterEvent<Vector3, Vector3, GameObject>(gameObject, "OnDeath", OnDeath);
|
||||
EventHandler.RegisterEvent(gameObject, "OnRespawn", OnRespawn);
|
||||
|
||||
CheckForController();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the button is being pressed.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the button.</param>
|
||||
/// <returns>True of the button is being pressed.</returns>
|
||||
public bool GetButton(string name)
|
||||
{
|
||||
return GetButtonInternal(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Internal method which returns true if the button is being pressed.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the button.</param>
|
||||
/// <returns>True of the button is being pressed.</returns>
|
||||
protected virtual bool GetButtonInternal(string name) { return false; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the button was pressed this frame.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the button.</param>
|
||||
/// <returns>True if the button is pressed this frame.</returns>
|
||||
public bool GetButtonDown(string name) { return GetButtonDownInternal(name); }
|
||||
|
||||
/// <summary>
|
||||
/// Internal method which returns true if the button was pressed this frame.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the button.</param>
|
||||
/// <returns>True if the button is pressed this frame.</returns>
|
||||
protected virtual bool GetButtonDownInternal(string name) { return false; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the button is up.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the button.</param>
|
||||
/// <returns>True if the button is up.</returns>
|
||||
public bool GetButtonUp(string name) { return GetButtonUpInternal(name); }
|
||||
|
||||
/// <summary>
|
||||
/// Internal method which returns true if the button is up.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the button.</param>
|
||||
/// <returns>True if the button is up.</returns>
|
||||
protected virtual bool GetButtonUpInternal(string name) { return false; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if a double press occurred (double click or double tap).
|
||||
/// </summary>
|
||||
/// <param name="name">The button name to check for a double press.</param>
|
||||
/// <returns>True if a double press occurred (double click or double tap).</returns>
|
||||
public bool GetDoublePress(string name)
|
||||
{
|
||||
if (GetButtonDown(name)) {
|
||||
if (m_ButtonDownTime == null) {
|
||||
m_ButtonDownTime = new Dictionary<string, float>();
|
||||
}
|
||||
var time = -1f;
|
||||
if (m_ButtonDownTime.TryGetValue(name, out time)) {
|
||||
if (time != Time.unscaledTime && time + 0.2f > Time.unscaledTime) {
|
||||
return true;
|
||||
}
|
||||
m_ButtonDownTime[name] = Time.unscaledTime;
|
||||
} else {
|
||||
m_ButtonDownTime.Add(name, Time.unscaledTime);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Internal method which returns true if a double press occurred (double click or double tap).
|
||||
/// </summary>
|
||||
/// <param name="name">The button name to check for a double press.</param>
|
||||
/// <returns>True if a double press occurred (double click or double tap).</returns>
|
||||
protected virtual bool GetDoublePressInternal(string name) { return false; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if a tap occurred.
|
||||
/// </summary>
|
||||
/// <param name="name">The button name to check for a tap.</param>
|
||||
/// <returns>True if a tap occurred.</returns>
|
||||
public bool GetTap(string name)
|
||||
{
|
||||
var time = -1f;
|
||||
if (GetButton(name)) {
|
||||
if (m_ButtonDownTime == null) {
|
||||
m_ButtonDownTime = new Dictionary<string, float>();
|
||||
}
|
||||
if (!m_ButtonDownTime.ContainsKey(name)) {
|
||||
m_ButtonDownTime.Add(name, Time.unscaledTime);
|
||||
}
|
||||
} else if (m_ButtonDownTime != null && m_ButtonDownTime.TryGetValue(name, out time)) {
|
||||
m_ButtonDownTime.Remove(name);
|
||||
if (time != Time.unscaledTime && time + 0.2f > Time.unscaledTime) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if a long press occurred.
|
||||
/// </summary>
|
||||
/// <param name="name">The button name to check for a long press.</param>
|
||||
/// <param name="duration">The duration of a long press.</param>
|
||||
/// <param name="waitForRelease">Indicates if the long press should occur after the button has been released (true) or after the duration (false).</param>
|
||||
/// <returns>True if a long press occurred.</returns>
|
||||
public bool GetLongPress(string name, float duration, bool waitForRelease)
|
||||
{
|
||||
// Button down and up times won't be allocated unless double or long press inputs are used.
|
||||
if (m_ButtonDownTime == null) {
|
||||
m_ButtonDownTime = new Dictionary<string, float>();
|
||||
m_ButtonUpTime = new Dictionary<string, float>();
|
||||
}
|
||||
|
||||
if (GetButtonInternal(name)) {
|
||||
var downTime = -1f;
|
||||
if (m_ButtonDownTime.TryGetValue(name, out downTime)) {
|
||||
// Only set the down time if the up time is greater than the down time. This will prevent the current time from being set every tick.
|
||||
var upTime = -1f;
|
||||
m_ButtonUpTime.TryGetValue(name, out upTime);
|
||||
if (upTime > downTime) {
|
||||
m_ButtonDownTime[name] = downTime = Time.unscaledTime;
|
||||
}
|
||||
// Return true as soon as the button has been pressed for the duration.
|
||||
if (!waitForRelease) {
|
||||
return downTime + duration <= Time.unscaledTime;
|
||||
}
|
||||
} else {
|
||||
m_ButtonDownTime.Add(name, Time.unscaledTime);
|
||||
}
|
||||
} else {
|
||||
var upTime = -1f;
|
||||
if (m_ButtonUpTime.TryGetValue(name, out upTime)) {
|
||||
// Only set the up time if the down time is greater than the up time. This will prevent the current time from being set every tick.
|
||||
var downTime = -1f;
|
||||
m_ButtonDownTime.TryGetValue(name, out downTime);
|
||||
if (downTime > upTime) {
|
||||
m_ButtonUpTime[name] = upTime = Time.unscaledTime;
|
||||
if (waitForRelease) {
|
||||
return downTime + duration <= Time.unscaledTime;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
m_ButtonUpTime.Add(name, Time.unscaledTime);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the value of the axis with the specified name.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the axis.</param>
|
||||
/// <returns>The value of the axis.</returns>
|
||||
public float GetAxis(string name) { return GetAxisInternal(name); }
|
||||
|
||||
/// <summary>
|
||||
/// Internal method which returns the value of the axis with the specified name.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the axis.</param>
|
||||
/// <returns>The value of the axis.</returns>
|
||||
protected virtual float GetAxisInternal(string name) { return 0; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the value of the raw axis with the specified name.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the axis.</param>
|
||||
/// <returns>The value of the raw axis.</returns>
|
||||
public float GetAxisRaw(string name) { return GetAxisRawInternal(name); }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the value of the raw axis with the specified name.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the axis.</param>
|
||||
/// <returns>The value of the raw axis.</returns>
|
||||
protected virtual float GetAxisRawInternal(string name) { return 0; }
|
||||
|
||||
/// <summary>
|
||||
/// Is a controller connected?
|
||||
/// </summary>
|
||||
/// <returns>True if a controller is connected.</returns>
|
||||
public bool IsControllerConnected() { return m_ControllerConnected; }
|
||||
|
||||
/// <summary>
|
||||
/// Is the cursor visible?
|
||||
/// </summary>
|
||||
/// <returns>True if the cursor is visible.</returns>
|
||||
public bool IsCursorVisible()
|
||||
{
|
||||
return Cursor.visible;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the position of the mouse.
|
||||
/// </summary>
|
||||
/// <returns>The mouse position.</returns>
|
||||
public virtual Vector2 GetMousePosition() { return Input.mousePosition; }
|
||||
|
||||
/// <summary>
|
||||
/// Determines if a controller is connected.
|
||||
/// </summary>
|
||||
private void CheckForController()
|
||||
{
|
||||
if (!CanCheckForController) {
|
||||
return;
|
||||
}
|
||||
|
||||
var controllerConencted = Input.GetJoystickNames().Length > 0;
|
||||
if (m_ControllerConnected != controllerConencted) {
|
||||
m_ControllerConnected = controllerConencted;
|
||||
if (!string.IsNullOrEmpty(m_ConnectedControllerState)) {
|
||||
StateManager.SetState(gameObject, m_ConnectedControllerState, m_ControllerConnected);
|
||||
}
|
||||
EventHandler.ExecuteEvent<bool>(gameObject, "OnInputControllerConnected", m_ControllerConnected);
|
||||
}
|
||||
|
||||
// Schedule the controller check event if the rate is positive.
|
||||
// UnityEngine.Input.GetJoystickNames generates garbage so limit the amount of time the controller is checked.
|
||||
if (m_ControllerConnectedCheckRate > 0 && (m_ControllerCheckEvent == null || !m_ControllerCheckEvent.Active)) {
|
||||
m_ControllerCheckEvent = Scheduler.Schedule(m_ControllerConnectedCheckRate, CheckForController);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the look smoothing buffer to the current look vector.
|
||||
/// </summary>
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (!Application.isFocused) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_RawLookVector.x = GetAxisRaw(m_HorizontalLookInputName);
|
||||
m_RawLookVector.y = GetAxisRaw(m_VerticalLookInputName);
|
||||
|
||||
if (m_LookVectorMode == LookVectorMode.Smoothed) {
|
||||
// Set the current input to the look buffer.
|
||||
m_SmoothLookBuffer[m_SmoothLookBufferIndex].x = m_RawLookVector.x;
|
||||
m_SmoothLookBuffer[m_SmoothLookBufferIndex].y = m_RawLookVector.y;
|
||||
if (m_SmoothLookBufferCount < m_SmoothLookBufferIndex + 1) {
|
||||
m_SmoothLookBufferCount = m_SmoothLookBufferIndex + 1;
|
||||
}
|
||||
|
||||
// Calculate the input smoothing value. The more recent the input value occurred the higher the influence it has on the final smoothing value.
|
||||
var weight = 1f;
|
||||
var average = Vector2.zero;
|
||||
var averageTotal = 0f;
|
||||
var deltaTime = m_TimeScale * TimeUtility.FramerateDeltaTime;
|
||||
for (int i = 0; i < m_SmoothLookBufferCount; ++i) {
|
||||
var index = m_SmoothLookBufferIndex - i;
|
||||
if (index < 0) { index = m_SmoothLookBufferCount + m_SmoothLookBufferIndex - i; }
|
||||
average += m_SmoothLookBuffer[index] * weight;
|
||||
averageTotal += weight;
|
||||
// The deltaTime will be 0 if Unity just started to play after stepping through the editor.
|
||||
if (deltaTime > 0) {
|
||||
weight *= (m_SmoothLookWeight / deltaTime);
|
||||
}
|
||||
}
|
||||
m_SmoothLookBufferIndex = (m_SmoothLookBufferIndex + 1) % m_SmoothLookBuffer.Length;
|
||||
|
||||
// Store the averaged input value.
|
||||
averageTotal = Mathf.Max(1, averageTotal);
|
||||
m_CurrentLookVector = average / averageTotal;
|
||||
|
||||
// Apply any look acceleration. The delta time will be zero on the very first frame.
|
||||
var lookAcceleration = 0f;
|
||||
if (m_LookAccelerationThreshold > 0 && deltaTime != 0) {
|
||||
var accX = Mathf.Abs(m_CurrentLookVector.x);
|
||||
var accY = Mathf.Abs(m_CurrentLookVector.y);
|
||||
lookAcceleration = Mathf.Sqrt((accX * accX) + (accY * accY)) / deltaTime;
|
||||
if (lookAcceleration > m_LookAccelerationThreshold) {
|
||||
lookAcceleration = m_LookAccelerationThreshold;
|
||||
}
|
||||
}
|
||||
|
||||
// Determine the final value.
|
||||
m_CurrentLookVector.x *= ((m_LookSensitivity.x * m_LookSensitivityMultiplier) + lookAcceleration) * TimeUtility.FramerateDeltaTime;
|
||||
m_CurrentLookVector.y *= ((m_LookSensitivity.y * m_LookSensitivityMultiplier) + lookAcceleration) * TimeUtility.FramerateDeltaTime;
|
||||
|
||||
m_CurrentLookVector.x = Mathf.Sign(m_CurrentLookVector.x) * Mathf.Pow(Mathf.Abs(m_CurrentLookVector.x), m_SmoothExponent);
|
||||
m_CurrentLookVector.y = Mathf.Sign(m_CurrentLookVector.y) * Mathf.Pow(Mathf.Abs(m_CurrentLookVector.y), m_SmoothExponent);
|
||||
} else if (m_LookVectorMode == LookVectorMode.UnitySmoothed) {
|
||||
m_CurrentLookVector.x = GetAxis(m_HorizontalLookInputName);
|
||||
m_CurrentLookVector.y = GetAxis(m_VerticalLookInputName);
|
||||
} else if (m_LookVectorMode == LookVectorMode.Raw) {
|
||||
m_CurrentLookVector = m_RawLookVector;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the look vector. Will apply smoothing if specified otherwise will return the GetAxis value.
|
||||
/// </summary>
|
||||
/// <param name="smoothed">Should the smoothing value be returned? If false the raw look vector will be returned.</param>
|
||||
/// <returns>The current look vector.</returns>
|
||||
public virtual Vector2 GetLookVector(bool smoothed)
|
||||
{
|
||||
if (smoothed) {
|
||||
return m_CurrentLookVector;
|
||||
}
|
||||
return m_RawLookVector;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the pointer is over a UI element.
|
||||
/// </summary>
|
||||
/// <returns>True if the pointer is over a UI element.</returns>
|
||||
public virtual bool IsPointerOverUI()
|
||||
{
|
||||
if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The character's local timescale has changed.
|
||||
/// </summary>
|
||||
/// <param name="timeScale">The new timescale.</param>
|
||||
private void ChangeTimeScale(float timeScale)
|
||||
{
|
||||
m_TimeScale = timeScale;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enables or disables gameplay input. An example of when it will not be enabled is when there is a fullscreen UI over the main camera.
|
||||
/// </summary>
|
||||
/// <param name="enable">True if the input is enabled.</param>
|
||||
protected virtual void EnableGameplayInput(bool enable)
|
||||
{
|
||||
m_AllowInput = enable;
|
||||
enabled = m_AllowInput && !m_Death;
|
||||
if (enabled && !Application.isFocused) {
|
||||
OnApplicationFocus(true);
|
||||
} else if (!enabled) {
|
||||
m_RawLookVector = m_CurrentLookVector = Vector3.zero;
|
||||
}
|
||||
|
||||
if (m_EnableGamplayInputEvent != null) {
|
||||
m_EnableGamplayInputEvent.Invoke(enable);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The character has died. Disable the component.
|
||||
/// </summary>
|
||||
/// <param name="position">The position of the force.</param>
|
||||
/// <param name="force">The amount of force which killed the character.</param>
|
||||
/// <param name="attacker">The GameObject that killed the character.</param>
|
||||
private void OnDeath(Vector3 position, Vector3 force, GameObject attacker)
|
||||
{
|
||||
m_Death = true;
|
||||
enabled = m_AllowInput && !m_Death;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The character has respawned. Enable the component.
|
||||
/// </summary>
|
||||
private void OnRespawn()
|
||||
{
|
||||
m_Death = false;
|
||||
enabled = m_AllowInput && !m_Death;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Does the game have focus?
|
||||
/// </summary>
|
||||
/// <param name="hasFocus">True if the game has focus.</param>
|
||||
protected virtual void OnApplicationFocus(bool hasFocus)
|
||||
{
|
||||
if (Application.isFocused) {
|
||||
CheckForController();
|
||||
} else {
|
||||
m_CurrentLookVector = Vector3.zero;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The GameObject has been destroyed.
|
||||
/// </summary>
|
||||
private void OnDestroy()
|
||||
{
|
||||
EventHandler.UnregisterEvent<float>(gameObject, "OnCharacterChangeTimeScale", ChangeTimeScale);
|
||||
EventHandler.UnregisterEvent<bool>(gameObject, "OnEnableGameplayInput", EnableGameplayInput);
|
||||
EventHandler.UnregisterEvent<Vector3, Vector3, GameObject>(gameObject, "OnDeath", OnDeath);
|
||||
EventHandler.UnregisterEvent(gameObject, "OnRespawn", OnRespawn);
|
||||
if (m_ControllerCheckEvent != null) {
|
||||
Scheduler.Cancel(m_ControllerCheckEvent);
|
||||
m_ControllerCheckEvent = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9e87457eb498fb847bcb1126b0928d14
|
||||
timeCreated: 1481260746
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,90 +0,0 @@
|
||||
/// ---------------------------------------------
|
||||
/// Ultimate Character Controller
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
|
||||
namespace Opsive.UltimateCharacterController.Input
|
||||
{
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Uses Unity's input system to detect input related actions.
|
||||
/// </summary>
|
||||
public class StandaloneInput : InputBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns if the button is true with the specified ButtonAction.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the button.</param>
|
||||
/// <param name="action">The type of action to check.</param>
|
||||
/// <returns>The status of the action.</returns>
|
||||
public override bool GetButton(string name, ButtonAction action)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
try {
|
||||
switch (action) {
|
||||
case ButtonAction.GetButton:
|
||||
return UnityEngine.Input.GetButton(name);
|
||||
case ButtonAction.GetButtonDown:
|
||||
return UnityEngine.Input.GetButtonDown(name);
|
||||
case ButtonAction.GetButtonUp:
|
||||
return UnityEngine.Input.GetButtonUp(name);
|
||||
}
|
||||
}
|
||||
catch (System.Exception /*e*/) {
|
||||
Debug.LogError("Button \"" + name + "\" is not setup. Please create a button mapping within the Unity Input Manager.");
|
||||
}
|
||||
#else
|
||||
switch (action) {
|
||||
case ButtonAction.GetButton:
|
||||
return UnityEngine.Input.GetButton(name);
|
||||
case ButtonAction.GetButtonDown:
|
||||
return UnityEngine.Input.GetButtonDown(name);
|
||||
case ButtonAction.GetButtonUp:
|
||||
return UnityEngine.Input.GetButtonUp(name);
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the value of the axis with the specified name.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the axis.</param>
|
||||
/// <returns>The value of the axis.</returns>
|
||||
public override float GetAxis(string name)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
try {
|
||||
return UnityEngine.Input.GetAxis(name);
|
||||
}
|
||||
catch (UnityException /*e*/) {
|
||||
Debug.LogError("Axis \"" + name + "\" is not setup. Please create an axis mapping within the Unity Input Manager.");
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
return UnityEngine.Input.GetAxis(name);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the value of theraw axis with the specified name.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the axis.</param>
|
||||
/// <returns>The value of the raw axis.</returns>
|
||||
public override float GetAxisRaw(string name)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
try {
|
||||
return UnityEngine.Input.GetAxisRaw(name);
|
||||
} catch (UnityException /*e*/) {
|
||||
Debug.LogError("Axis \"" + name + "\" is not setup. Please create an axis mapping within the Unity Input Manager.");
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
return UnityEngine.Input.GetAxisRaw(name);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c5840c34b732de4fb0671615042063a
|
||||
timeCreated: 1481260746
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,305 +0,0 @@
|
||||
/// ---------------------------------------------
|
||||
/// Ultimate Character Controller
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
|
||||
namespace Opsive.UltimateCharacterController.Input
|
||||
{
|
||||
using Opsive.UltimateCharacterController.Input.VirtualControls;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Acts as a common base class for input using the Unity Input Manager. Works with keyboard/mouse, controller, and mobile input.
|
||||
/// </summary>
|
||||
public class UnityInput : PlayerInput
|
||||
{
|
||||
/// <summary>
|
||||
/// Specifies if any input type should be forced.
|
||||
/// </summary>
|
||||
public enum ForceInputType { None, Standalone, Virtual }
|
||||
|
||||
[Tooltip("Specifies if any input type should be forced.")]
|
||||
[SerializeField] protected ForceInputType m_ForceInput;
|
||||
[Tooltip("Should the cursor be disabled?")]
|
||||
[SerializeField] protected bool m_DisableCursor = true;
|
||||
[Tooltip("Should the cursor be enabled when the escape key is pressed?")]
|
||||
[SerializeField] protected bool m_EnableCursorWithEscape = true;
|
||||
[Tooltip("If the cursor is enabled with escape should the look vector be prevented from updating?")]
|
||||
[SerializeField] protected bool m_PreventLookVectorChanges = true;
|
||||
[Tooltip("The joystick is considered up when the raw value is less than the specified threshold.")]
|
||||
[Range(0, 1)] [SerializeField] protected float m_JoystickUpThreshold = 1;
|
||||
|
||||
public bool DisableCursor { get { return m_DisableCursor; }
|
||||
set
|
||||
{
|
||||
if (m_Input is VirtualInput) {
|
||||
m_DisableCursor = false;
|
||||
}
|
||||
m_DisableCursor = value;
|
||||
if (m_DisableCursor && Cursor.visible) {
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
} else if (!m_DisableCursor && !Cursor.visible) {
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
public bool EnableCursorWithEscape { get { return m_EnableCursorWithEscape; } set { m_EnableCursorWithEscape = value; } }
|
||||
public bool PreventLookMovementWithEscape { get { return m_PreventLookVectorChanges; } set { m_PreventLookVectorChanges = value; } }
|
||||
public float JoystickUpThreshold { get { return m_JoystickUpThreshold; } set { m_JoystickUpThreshold = value; } }
|
||||
|
||||
private InputBase m_Input;
|
||||
private bool m_UseVirtualInput;
|
||||
private HashSet<string> m_JoystickDownSet;
|
||||
private HashSet<string> m_ToAddJoystickDownSet;
|
||||
private HashSet<string> m_ToRemoveJoystickDownSet;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the default values.
|
||||
/// </summary>
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
|
||||
m_UseVirtualInput = m_ForceInput == ForceInputType.Virtual;
|
||||
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID || UNITY_WP_8_1 || UNITY_BLACKBERRY)
|
||||
if (m_ForceInput != ForceInputType.Standalone) {
|
||||
m_UseVirtualInput = true;
|
||||
}
|
||||
#endif
|
||||
if (m_UseVirtualInput) {
|
||||
m_Input = new VirtualInput();
|
||||
// The cursor must be enabled for virtual controls to allow the drag events to occur.
|
||||
m_DisableCursor = false;
|
||||
} else {
|
||||
m_Input = new StandaloneInput();
|
||||
}
|
||||
m_Input.Initialize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The component has been enabled.
|
||||
/// </summary>
|
||||
private void OnEnable()
|
||||
{
|
||||
if (!m_UseVirtualInput && m_DisableCursor) {
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Associates the VirtualControlsManager with the VirtualInput object.
|
||||
/// </summary>
|
||||
/// <param name="virtualControlsManager">The VirtualControlsManager to associate with the VirtualInput object.</param>
|
||||
/// <returns>True if the virtual controls were registered.</returns>
|
||||
public bool RegisterVirtualControlsManager(VirtualControlsManager virtualControlsManager)
|
||||
{
|
||||
if (m_Input is VirtualInput) {
|
||||
(m_Input as VirtualInput).RegisterVirtualControlsManager(virtualControlsManager);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the VirtualControlsManager association.
|
||||
/// </summary>
|
||||
public void UnegisterVirtualControlsManager()
|
||||
{
|
||||
if (m_Input is VirtualInput) {
|
||||
(m_Input as VirtualInput).UnregisterVirtualControlsManager();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the joystick and cursor state values.
|
||||
/// </summary>
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (m_UseVirtualInput) {
|
||||
return;
|
||||
}
|
||||
// The joystick is no longer down after the axis is 0.
|
||||
if (IsControllerConnected()) {
|
||||
// GetButtonUp/Down doesn't immediately add the button name to the set to prevent the GetButtonUp/Down from returning false
|
||||
// if it is called twice within the same frame. Add it after the frame has ended.
|
||||
if (m_JoystickDownSet != null) {
|
||||
foreach (var item in m_JoystickDownSet) {
|
||||
if (Mathf.Abs(m_Input.GetAxisRaw(item)) < m_JoystickUpThreshold) {
|
||||
m_ToRemoveJoystickDownSet.Add(item);
|
||||
}
|
||||
}
|
||||
foreach (var item in m_ToRemoveJoystickDownSet) {
|
||||
m_JoystickDownSet.Remove(item);
|
||||
}
|
||||
m_ToRemoveJoystickDownSet.Clear();
|
||||
}
|
||||
if (m_ToAddJoystickDownSet != null && m_ToAddJoystickDownSet.Count > 0) {
|
||||
if (m_JoystickDownSet == null) {
|
||||
m_JoystickDownSet = new HashSet<string>();
|
||||
m_ToRemoveJoystickDownSet = new HashSet<string>();
|
||||
}
|
||||
foreach (var item in m_ToAddJoystickDownSet) {
|
||||
m_JoystickDownSet.Add(item);
|
||||
}
|
||||
m_ToAddJoystickDownSet.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
// Enable the cursor if the escape key is pressed. Disable the cursor if it is visbile but should be disabled upon press.
|
||||
if (m_EnableCursorWithEscape && UnityEngine.Input.GetKeyDown(KeyCode.Escape)) {
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
if (m_PreventLookVectorChanges) {
|
||||
OnApplicationFocus(false);
|
||||
}
|
||||
} else if (Cursor.visible && m_DisableCursor && !IsPointerOverUI() && (UnityEngine.Input.GetKeyDown(KeyCode.Mouse0) || UnityEngine.Input.GetKeyDown(KeyCode.Mouse1))) {
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
if (m_PreventLookVectorChanges) {
|
||||
OnApplicationFocus(true);
|
||||
}
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
// The cursor should be visible when the game is paused.
|
||||
if (!Cursor.visible && Time.deltaTime == 0 && !m_DisableCursor) {
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Internal method which returns true if the button is being pressed.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the button.</param>
|
||||
/// <returns>True of the button is being pressed.</returns>
|
||||
protected override bool GetButtonInternal(string name)
|
||||
{
|
||||
if (m_Input.GetButton(name, InputBase.ButtonAction.GetButton)) {
|
||||
return true;
|
||||
}
|
||||
if (IsControllerConnected()) {
|
||||
if (Mathf.Abs(m_Input.GetAxisRaw(name)) == 1) {
|
||||
if (m_JoystickDownSet == null || !m_JoystickDownSet.Contains(name)) {
|
||||
if (m_ToAddJoystickDownSet == null) {
|
||||
m_ToAddJoystickDownSet = new HashSet<string>();
|
||||
}
|
||||
m_ToAddJoystickDownSet.Add(name);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Internal method which returns true if the button was pressed this frame.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the button.</param>
|
||||
/// <returns>True if the button is pressed this frame.</returns>
|
||||
protected override bool GetButtonDownInternal(string name)
|
||||
{
|
||||
if (IsControllerConnected() && Mathf.Abs(m_Input.GetAxisRaw(name)) == 1) {
|
||||
// The button should only be considered down on the first frame.
|
||||
if (m_JoystickDownSet != null && m_JoystickDownSet.Contains(name)) {
|
||||
return false;
|
||||
}
|
||||
if (m_ToAddJoystickDownSet == null) {
|
||||
m_ToAddJoystickDownSet = new HashSet<string>();
|
||||
}
|
||||
m_ToAddJoystickDownSet.Add(name);
|
||||
return true;
|
||||
}
|
||||
return m_Input.GetButton(name, InputBase.ButtonAction.GetButtonDown);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Internal method which returnstrue if the button is up.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the button.</param>
|
||||
/// <returns>True if the button is up.</returns>
|
||||
protected override bool GetButtonUpInternal(string name)
|
||||
{
|
||||
if (IsControllerConnected()) {
|
||||
if (Mathf.Abs(m_Input.GetAxisRaw(name)) == 1 && (m_JoystickDownSet == null || !m_JoystickDownSet.Contains(name))) {
|
||||
if (m_ToAddJoystickDownSet == null) {
|
||||
m_ToAddJoystickDownSet = new HashSet<string>();
|
||||
}
|
||||
m_ToAddJoystickDownSet.Add(name);
|
||||
return false;
|
||||
}
|
||||
if (m_JoystickDownSet != null && m_JoystickDownSet.Contains(name) && m_Input.GetAxisRaw(name) < m_JoystickUpThreshold) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return m_Input.GetButton(name, InputBase.ButtonAction.GetButtonUp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Internal method which returns the value of the axis with the specified name.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the axis.</param>
|
||||
/// <returns>The value of the axis.</returns>
|
||||
protected override float GetAxisInternal(string name)
|
||||
{
|
||||
return m_Input.GetAxis(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Internal method which returns the value of the raw axis with the specified name.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the axis.</param>
|
||||
/// <returns>The value of the raw axis.</returns>
|
||||
protected override float GetAxisRawInternal(string name)
|
||||
{
|
||||
return m_Input.GetAxisRaw(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the pointer is over a UI element.
|
||||
/// </summary>
|
||||
/// <returns>True if the pointer is over a UI element.</returns>
|
||||
public override bool IsPointerOverUI()
|
||||
{
|
||||
// The input will always be over a UI element with virtual inputs.
|
||||
if (m_Input is VirtualInput) {
|
||||
return false;
|
||||
}
|
||||
return base.IsPointerOverUI();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enables or disables gameplay input. An example of when it will not be enabled is when there is a fullscreen UI over the main camera.
|
||||
/// </summary>
|
||||
/// <param name="enable">True if the input is enabled.</param>
|
||||
protected override void EnableGameplayInput(bool enable)
|
||||
{
|
||||
base.EnableGameplayInput(enable);
|
||||
|
||||
if (enable && m_DisableCursor) {
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Does the game have focus?
|
||||
/// </summary>
|
||||
/// <param name="hasFocus">True if the game has focus.</param>
|
||||
protected override void OnApplicationFocus(bool hasFocus)
|
||||
{
|
||||
base.OnApplicationFocus(hasFocus);
|
||||
|
||||
if (enabled && hasFocus && m_DisableCursor) {
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c9c18933a275014f9b208ad09865ec5
|
||||
timeCreated: 1517506483
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: -10
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,8 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 55bf0ea4dba5bc047aec0d128bf591e0
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,63 +0,0 @@
|
||||
/// ---------------------------------------------
|
||||
/// Ultimate Character Controller
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
|
||||
namespace Opsive.UltimateCharacterController.Input.VirtualControls
|
||||
{
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
/// <summary>
|
||||
/// An abstract class for handing axis input.
|
||||
/// </summary>
|
||||
public abstract class VirtualAxis : VirtualControl, IPointerDownHandler, IPointerUpHandler
|
||||
{
|
||||
[Tooltip("The name of the horizontal input axis.")]
|
||||
[SerializeField] protected string m_HorizontalInputName = "Horizontal";
|
||||
[Tooltip("The name of the vertical input axis.")]
|
||||
[SerializeField] protected string m_VerticalInputName = "Vertical";
|
||||
|
||||
protected bool m_Pressed;
|
||||
protected Vector2 m_DeltaPosition;
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
|
||||
if (m_VirtualControlsManager != null) {
|
||||
m_VirtualControlsManager.RegisterVirtualControl(m_HorizontalInputName, this);
|
||||
m_VirtualControlsManager.RegisterVirtualControl(m_VerticalInputName, this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Callback when a pointer has pressed on the button.
|
||||
/// </summary>
|
||||
/// <param name="data">The pointer data.</param>
|
||||
public virtual void OnPointerDown(PointerEventData data)
|
||||
{
|
||||
if (m_Pressed) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_Pressed = true;
|
||||
m_DeltaPosition = Vector2.zero;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Callback when a pointer has released the button.
|
||||
/// </summary>
|
||||
/// <param name="data">The pointer data.</param>
|
||||
public virtual void OnPointerUp(PointerEventData data)
|
||||
{
|
||||
if (!m_Pressed) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_Pressed = false;
|
||||
m_DeltaPosition = Vector2.zero;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 39fe205d153209049a6a69118a0453d8
|
||||
timeCreated: 1517497774
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,97 +0,0 @@
|
||||
/// ---------------------------------------------
|
||||
/// Ultimate Character Controller
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
|
||||
namespace Opsive.UltimateCharacterController.Input.VirtualControls
|
||||
{
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
/// <summary>
|
||||
/// A virtual control that the player can press.
|
||||
/// </summary>
|
||||
public class VirtualButton : VirtualControl, IPointerDownHandler, IPointerUpHandler
|
||||
{
|
||||
[Tooltip("The name of the input.")]
|
||||
[SerializeField] protected string m_ButtonName;
|
||||
|
||||
private bool m_Pressed;
|
||||
private int m_Frame;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the default values.
|
||||
/// </summary>
|
||||
protected override void Awake()
|
||||
{
|
||||
if (string.IsNullOrEmpty(m_ButtonName)) {
|
||||
Debug.LogError("Error: The virtual button " + gameObject.name + " cannot have an empty input name.");
|
||||
return;
|
||||
}
|
||||
|
||||
base.Awake();
|
||||
|
||||
if (m_VirtualControlsManager != null) {
|
||||
m_VirtualControlsManager.RegisterVirtualControl(m_ButtonName, this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Callback when a pointer has pressed on the button.
|
||||
/// </summary>
|
||||
/// <param name="data">The pointer data.</param>
|
||||
public void OnPointerDown(PointerEventData data)
|
||||
{
|
||||
if (m_Pressed) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_Pressed = true;
|
||||
m_Frame = Time.frameCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Callback when a finger has released the button.
|
||||
/// </summary>
|
||||
/// <param name="data">The pointer data.</param>
|
||||
public void OnPointerUp(PointerEventData data)
|
||||
{
|
||||
if (!m_Pressed) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_Pressed = false;
|
||||
m_Frame = Time.frameCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if the button is true with the specified ButtonAction.
|
||||
/// </summary>
|
||||
/// <param name="action">The type of action to check.</param>
|
||||
/// <returns>The status of the action.</returns>
|
||||
public override bool GetButton(InputBase.ButtonAction action)
|
||||
{
|
||||
if (action == InputBase.ButtonAction.GetButton) {
|
||||
return m_Pressed;
|
||||
}
|
||||
|
||||
// GetButtonDown and GetButtonUp requires the button to be pressed or released within the same frame.
|
||||
if (Time.frameCount - m_Frame > 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return action == InputBase.ButtonAction.GetButtonDown ? m_Pressed : !m_Pressed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The object has been destroyed.
|
||||
/// </summary>
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (m_VirtualControlsManager != null) {
|
||||
m_VirtualControlsManager.UnregisterVirtualControl(m_ButtonName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 11b07129b6505394286595a777eac99e
|
||||
timeCreated: 1517497774
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,43 +0,0 @@
|
||||
/// ---------------------------------------------
|
||||
/// Ultimate Character Controller
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
|
||||
namespace Opsive.UltimateCharacterController.Input.VirtualControls
|
||||
{
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// An abstract class which represents a virtual control on the screen.
|
||||
/// </summary>
|
||||
public abstract class VirtualControl : MonoBehaviour
|
||||
{
|
||||
protected VirtualControlsManager m_VirtualControlsManager;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the default values.
|
||||
/// </summary>
|
||||
protected virtual void Awake()
|
||||
{
|
||||
m_VirtualControlsManager = GetComponentInParent<VirtualControlsManager>();
|
||||
if (m_VirtualControlsManager == null) {
|
||||
Debug.LogError("Error: Unable to find the VirtualControlsManager. This component must be a parent to the virtual input monitors.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if the button is true with the specified ButtonAction.
|
||||
/// </summary>
|
||||
/// <param name="action">The type of action to check.</param>
|
||||
/// <returns>The status of the action.</returns>
|
||||
public virtual bool GetButton(InputBase.ButtonAction action) { return false; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the value of the axis.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the axis.</param>
|
||||
/// <returns>The value of the axis.</returns>
|
||||
public virtual float GetAxis(string name) { return 0; }
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 091a397ef73762840a64871156645735
|
||||
timeCreated: 1517494095
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,147 +0,0 @@
|
||||
/// ---------------------------------------------
|
||||
/// Ultimate Character Controller
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
|
||||
namespace Opsive.UltimateCharacterController.Input.VirtualControls
|
||||
{
|
||||
using Opsive.Shared.Events;
|
||||
using Opsive.UltimateCharacterController.Utility;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Coordinates all of the virtual controls. All of the virtual controls must be a child of the VirtualControlsManager GameObject.
|
||||
/// </summary>
|
||||
public class VirtualControlsManager : MonoBehaviour
|
||||
{
|
||||
[Tooltip("The character used by the virtual input. Can be null.")]
|
||||
[SerializeField] protected GameObject m_Character;
|
||||
|
||||
public GameObject Character { get { return m_Character; } set { OnAttachCharacter(value); } }
|
||||
|
||||
private GameObject m_GameObject;
|
||||
private GameObject m_CameraGameObject;
|
||||
private Dictionary<string, VirtualControl> m_NameVirtualControlsMap = new Dictionary<string, VirtualControl>();
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the default values.
|
||||
/// </summary>
|
||||
protected virtual void Awake()
|
||||
{
|
||||
m_GameObject = gameObject;
|
||||
if (m_Character == null) {
|
||||
var camera = UnityEngineUtility.FindCamera(null);
|
||||
if (camera != null) {
|
||||
m_CameraGameObject = camera.gameObject;
|
||||
EventHandler.RegisterEvent<GameObject>(m_CameraGameObject, "OnCameraAttachCharacter", OnAttachCharacter);
|
||||
}
|
||||
} else {
|
||||
var character = m_Character;
|
||||
m_Character = null; // Set the character to null so the assignment will occur.
|
||||
OnAttachCharacter(character);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attaches the component to the specified character.
|
||||
/// </summary>
|
||||
/// <param name="character">The handler to attach the camera to.</param>
|
||||
private void OnAttachCharacter(GameObject character)
|
||||
{
|
||||
if (character == m_Character) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_Character != null) {
|
||||
var unityInput = m_Character.GetComponent<UnityInput>();
|
||||
if (unityInput == null) {
|
||||
m_GameObject.SetActive(false);
|
||||
return;
|
||||
}
|
||||
|
||||
unityInput.UnegisterVirtualControlsManager();
|
||||
}
|
||||
|
||||
m_Character = character;
|
||||
|
||||
var activateGameObject = false;
|
||||
if (character != null) {
|
||||
var unityInput = m_Character.GetComponent<UnityInput>();
|
||||
if (unityInput == null) {
|
||||
Debug.LogError($"Error: The character {m_Character.name} has no UnityInput component.");
|
||||
m_GameObject.SetActive(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// If the virtual controls weren't registered then the virtual input type isn't selected.
|
||||
activateGameObject = unityInput.RegisterVirtualControlsManager(this);
|
||||
}
|
||||
|
||||
m_GameObject.SetActive(activateGameObject);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Associates the input name with the virtual control object.
|
||||
/// </summary>
|
||||
/// <param name="inputName">The name to associate the virtual control object with.</param>
|
||||
/// <param name="virtualInput">The object to associate with the name.</param>
|
||||
public void RegisterVirtualControl(string inputName, VirtualControl virtualControl)
|
||||
{
|
||||
m_NameVirtualControlsMap.Add(inputName, virtualControl);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if the button is true with the specified ButtonAction.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the button.</param>
|
||||
/// <param name="action">The type of action to check.</param>
|
||||
/// <returns>The status of the action.</returns>
|
||||
public bool GetButton(string name, InputBase.ButtonAction action)
|
||||
{
|
||||
VirtualControl virtualControl;
|
||||
if (!m_NameVirtualControlsMap.TryGetValue(name, out virtualControl)) {
|
||||
//Debug.LogError("Error: No virtual input object exists with the name " + name);
|
||||
return false;
|
||||
}
|
||||
|
||||
return virtualControl.GetButton(action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the axis of the specified button.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the axis.</param>
|
||||
/// <returns>The axis value.</returns>
|
||||
public float GetAxis(string name)
|
||||
{
|
||||
VirtualControl virtualControl;
|
||||
if (!m_NameVirtualControlsMap.TryGetValue(name, out virtualControl)) {
|
||||
//Debug.LogError("Error: No virtual input object exists with the name " + name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return virtualControl.GetAxis(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the association with the object specified by the input name.
|
||||
/// </summary>
|
||||
/// <param name="inputName">The name of the object to remove association with.</param>
|
||||
public void UnregisterVirtualControl(string inputName)
|
||||
{
|
||||
m_NameVirtualControlsMap.Remove(inputName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The object has been destroyed.
|
||||
/// </summary>
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
if (m_CameraGameObject != null) {
|
||||
EventHandler.UnregisterEvent<GameObject>(m_CameraGameObject, "OnCameraAttachCharacter", OnAttachCharacter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0449f58c4ba50164eb2ae9c0a1e152b5
|
||||
timeCreated: 1520973872
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 390
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,98 +0,0 @@
|
||||
/// ---------------------------------------------
|
||||
/// Ultimate Character Controller
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
|
||||
namespace Opsive.UltimateCharacterController.Input.VirtualControls
|
||||
{
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
/// <summary>
|
||||
/// A virtual joystick that stays within the specified radius range. When the press is released the joystick knob will snap back to the starting position.
|
||||
/// </summary>
|
||||
public class VirtualJoystick : VirtualAxis, IDragHandler
|
||||
{
|
||||
[Tooltip("A reference to the joystick that moves with the press position.")]
|
||||
[SerializeField] protected RectTransform m_Joystick;
|
||||
[Tooltip("The maximum number of pixels that the joystick can move.")]
|
||||
[SerializeField] protected float m_Radius = 100;
|
||||
[Tooltip("The joystick will return a zero value when the radius is within the specified deadzone radius of the center.")]
|
||||
[SerializeField] protected float m_DeadzoneRadius = 5;
|
||||
|
||||
private Transform m_CanvasScalarTransform;
|
||||
private Vector2 m_JoystickStartPosition;
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
if (m_Joystick == null) {
|
||||
Debug.LogError("Error: A joystick transform must be specified.");
|
||||
enabled = false;
|
||||
return;
|
||||
}
|
||||
m_CanvasScalarTransform = GetComponentInParent<CanvasScaler>().transform;
|
||||
m_JoystickStartPosition = m_Joystick.anchoredPosition;
|
||||
|
||||
base.Awake();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Callback when a pointer has dragged the button.
|
||||
/// </summary>
|
||||
/// <param name="data">The pointer data.</param>
|
||||
public void OnDrag(PointerEventData data)
|
||||
{
|
||||
var canvasScale = m_CanvasScalarTransform == null ? Vector3.one : m_CanvasScalarTransform.localScale;
|
||||
m_DeltaPosition.x += data.delta.x / canvasScale.x;
|
||||
m_DeltaPosition.y += data.delta.y / canvasScale.y;
|
||||
m_DeltaPosition.x = Mathf.Clamp(m_DeltaPosition.x, -m_Radius, m_Radius);
|
||||
m_DeltaPosition.y = Mathf.Clamp(m_DeltaPosition.y, -m_Radius, m_Radius);
|
||||
if (m_DeltaPosition.magnitude > m_Radius) {
|
||||
m_DeltaPosition = m_DeltaPosition.normalized * m_Radius;
|
||||
}
|
||||
|
||||
// Update the joystick position.
|
||||
m_Joystick.anchoredPosition = m_JoystickStartPosition + m_DeltaPosition;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Callback when a finger has released the button.
|
||||
/// </summary>
|
||||
/// <param name="data">The pointer data.</param>
|
||||
public override void OnPointerUp(PointerEventData data)
|
||||
{
|
||||
if (!m_Pressed) {
|
||||
return;
|
||||
}
|
||||
|
||||
base.OnPointerUp(data);
|
||||
|
||||
m_Joystick.anchoredPosition = m_JoystickStartPosition;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the value of the axis.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the axis.</param>
|
||||
/// <returns>The value of the axis.</returns>
|
||||
public override float GetAxis(string name)
|
||||
{
|
||||
if (!m_Pressed) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (name == m_HorizontalInputName) {
|
||||
if (Mathf.Abs(m_DeltaPosition.x) > m_DeadzoneRadius) {
|
||||
return m_DeltaPosition.x / m_Radius;
|
||||
}
|
||||
} else {
|
||||
if (Mathf.Abs(m_DeltaPosition.y) > m_DeadzoneRadius) {
|
||||
return m_DeltaPosition.y / m_Radius;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73e2f24dbd189b049b25197daa8bce2f
|
||||
timeCreated: 1517497774
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,96 +0,0 @@
|
||||
/// ---------------------------------------------
|
||||
/// Ultimate Character Controller
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
|
||||
namespace Opsive.UltimateCharacterController.Input.VirtualControls
|
||||
{
|
||||
using Opsive.Shared.Game;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
/// <summary>
|
||||
/// A virtual touchpad that will move the axis based on the position of the press relative to the starting press position.
|
||||
/// </summary>
|
||||
public class VirtualTouchpad : VirtualAxis, IDragHandler
|
||||
{
|
||||
[Tooltip("Should the input value be stopped if there is no movement on the touch pad?")]
|
||||
[SerializeField] protected bool m_RequireActiveDrag;
|
||||
[Tooltip("The value to dampen the drag value by when no longer dragging. The higher the value the quicker the drag value will decrease.")]
|
||||
[SerializeField] protected float m_ActiveDragDamping = 1f;
|
||||
|
||||
private RectTransform m_RectTransform;
|
||||
private Vector2 m_LocalStartPosition;
|
||||
private Transform m_CanvasScalarTransform;
|
||||
private ScheduledEventBase m_ActiveDragScheduler;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the default values.
|
||||
/// </summary>
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
|
||||
m_RectTransform = GetComponent<RectTransform>();
|
||||
m_CanvasScalarTransform = GetComponentInParent<CanvasScaler>().transform;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Callback when a pointer has pressed on the button.
|
||||
/// </summary>
|
||||
/// <param name="data">The pointer data.</param>
|
||||
public override void OnPointerDown(PointerEventData data)
|
||||
{
|
||||
base.OnPointerDown(data);
|
||||
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle(m_RectTransform, data.pressPosition, null, out m_LocalStartPosition);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Callback when a pointer has dragged the button.
|
||||
/// </summary>
|
||||
/// <param name="data">The pointer data.</param>
|
||||
public void OnDrag(PointerEventData data)
|
||||
{
|
||||
if (RectTransformUtility.RectangleContainsScreenPoint(m_RectTransform, data.position, null)) {
|
||||
var canvasScale = m_CanvasScalarTransform == null ? Vector3.one : m_CanvasScalarTransform.localScale;
|
||||
m_DeltaPosition.x += data.delta.x / canvasScale.x;
|
||||
m_DeltaPosition.y += data.delta.y / canvasScale.y;
|
||||
Scheduler.Cancel(m_ActiveDragScheduler);
|
||||
if (m_RequireActiveDrag) {
|
||||
m_ActiveDragScheduler = Scheduler.Schedule(Time.fixedDeltaTime, DampenDeltaPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the delta drag position.
|
||||
/// </summary>
|
||||
private void DampenDeltaPosition()
|
||||
{
|
||||
m_DeltaPosition /= (1 + m_ActiveDragDamping);
|
||||
if (m_DeltaPosition.sqrMagnitude > 0.1f) {
|
||||
m_ActiveDragScheduler = Scheduler.Schedule(Time.fixedDeltaTime, DampenDeltaPosition);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the value of the axis.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the axis.</param>
|
||||
/// <returns>The value of the axis.</returns>
|
||||
public override float GetAxis(string name)
|
||||
{
|
||||
if (!m_Pressed) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (name == m_HorizontalInputName) {
|
||||
return m_DeltaPosition.x / (m_RectTransform.sizeDelta.x - m_LocalStartPosition.x);
|
||||
}
|
||||
return m_DeltaPosition.y / (m_RectTransform.sizeDelta.y - m_LocalStartPosition.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 14bcb8a1d22843644a54cab80a20b143
|
||||
timeCreated: 1517497774
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,79 +0,0 @@
|
||||
/// ---------------------------------------------
|
||||
/// Ultimate Character Controller
|
||||
/// Copyright (c) Opsive. All Rights Reserved.
|
||||
/// https://www.opsive.com
|
||||
/// ---------------------------------------------
|
||||
|
||||
namespace Opsive.UltimateCharacterController.Input
|
||||
{
|
||||
using UnityEngine;
|
||||
using Opsive.UltimateCharacterController.Input.VirtualControls;
|
||||
|
||||
/// <summary>
|
||||
/// Uses virtual buttons to detect input related actions.
|
||||
/// </summary>
|
||||
public class VirtualInput : InputBase
|
||||
{
|
||||
private VirtualControlsManager m_VirtualControlsManager;
|
||||
|
||||
/// <summary>
|
||||
/// Associates the VirtualControlsManager with the VirtualInput object.
|
||||
/// </summary>
|
||||
/// <param name="virtualControlsManager">The VirtualControlsManager to associate with the VirtualInput object.</param>
|
||||
public void RegisterVirtualControlsManager(VirtualControlsManager virtualControlsManager)
|
||||
{
|
||||
m_VirtualControlsManager = virtualControlsManager;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the VirtualControlsManager association.
|
||||
/// </summary>
|
||||
public void UnregisterVirtualControlsManager()
|
||||
{
|
||||
m_VirtualControlsManager = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the axis of the specified button.
|
||||
/// </summary>
|
||||
/// <param name="axisName">The name of the axis.</param>
|
||||
/// <returns>The axis value.</returns>
|
||||
public override float GetAxis(string name)
|
||||
{
|
||||
if (m_VirtualControlsManager == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return m_VirtualControlsManager.GetAxis(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the raw axis of the specified button.
|
||||
/// </summary>
|
||||
/// <param name="axisName">The name of the axis.</param>
|
||||
/// <returns>The raw axis value.</returns>
|
||||
public override float GetAxisRaw(string axisName)
|
||||
{
|
||||
if (m_VirtualControlsManager == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return m_VirtualControlsManager.GetAxis(axisName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if the button is true with the specified ButtonAction.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the button.</param>
|
||||
/// <param name="action">The type of action to check.</param>
|
||||
/// <returns>The status of the action.</returns>
|
||||
public override bool GetButton(string name, ButtonAction action)
|
||||
{
|
||||
if (m_VirtualControlsManager == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return m_VirtualControlsManager.GetButton(name, action);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 185317dcf40935e4dbdc603671118d92
|
||||
timeCreated: 1517487428
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user