This commit is contained in:
2026-06-09 09:18:17 +07:00
parent 3578a2750c
commit 71a096556a
5777 changed files with 6675 additions and 13 deletions

View File

@@ -0,0 +1,63 @@
/// ---------------------------------------------
/// 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;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 39fe205d153209049a6a69118a0453d8
timeCreated: 1517497774
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,97 @@
/// ---------------------------------------------
/// 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);
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 11b07129b6505394286595a777eac99e
timeCreated: 1517497774
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,43 @@
/// ---------------------------------------------
/// 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; }
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 091a397ef73762840a64871156645735
timeCreated: 1517494095
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,147 @@
/// ---------------------------------------------
/// 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);
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 0449f58c4ba50164eb2ae9c0a1e152b5
timeCreated: 1520973872
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 390
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,98 @@
/// ---------------------------------------------
/// 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;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 73e2f24dbd189b049b25197daa8bce2f
timeCreated: 1517497774
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,96 @@
/// ---------------------------------------------
/// 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);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 14bcb8a1d22843644a54cab80a20b143
timeCreated: 1517497774
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: