/// --------------------------------------------- /// 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; /// /// Coordinates all of the virtual controls. All of the virtual controls must be a child of the VirtualControlsManager GameObject. /// 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 m_NameVirtualControlsMap = new Dictionary(); /// /// Initialize the default values. /// 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(m_CameraGameObject, "OnCameraAttachCharacter", OnAttachCharacter); } } else { var character = m_Character; m_Character = null; // Set the character to null so the assignment will occur. OnAttachCharacter(character); } } /// /// Attaches the component to the specified character. /// /// The handler to attach the camera to. private void OnAttachCharacter(GameObject character) { if (character == m_Character) { return; } if (m_Character != null) { var unityInput = m_Character.GetComponent(); if (unityInput == null) { m_GameObject.SetActive(false); return; } unityInput.UnegisterVirtualControlsManager(); } m_Character = character; var activateGameObject = false; if (character != null) { var unityInput = m_Character.GetComponent(); 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); } /// /// Associates the input name with the virtual control object. /// /// The name to associate the virtual control object with. /// The object to associate with the name. public void RegisterVirtualControl(string inputName, VirtualControl virtualControl) { m_NameVirtualControlsMap.Add(inputName, virtualControl); } /// /// Returns if the button is true with the specified ButtonAction. /// /// The name of the button. /// The type of action to check. /// The status of the action. 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); } /// /// Returns the axis of the specified button. /// /// The name of the axis. /// The axis value. 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); } /// /// Removes the association with the object specified by the input name. /// /// The name of the object to remove association with. public void UnregisterVirtualControl(string inputName) { m_NameVirtualControlsMap.Remove(inputName); } /// /// The object has been destroyed. /// protected virtual void OnDestroy() { if (m_CameraGameObject != null) { EventHandler.UnregisterEvent(m_CameraGameObject, "OnCameraAttachCharacter", OnAttachCharacter); } } } }