/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Demo.UI
{
using Opsive.Shared.Events;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Character.MovementTypes;
using UnityEngine;
using UnityEngine.UI;
///
/// Monitors the text components which show the active perspective and movement type.
///
public class CharacterStatusMonitor : MonoBehaviour
{
[Tooltip("A reference to the perspective UI text.")]
[SerializeField] protected Text m_PerspectiveText;
[Tooltip("A reference to the movement type UI text.")]
[SerializeField] protected Text m_MovementTypeText;
private UltimateCharacterLocomotion m_CharacterLocomotion;
///
/// Initialize the default values.
///
private void Start()
{
var camera = Utility.UnityEngineUtility.FindCamera(null);
var character = camera.GetComponent().Character;
m_CharacterLocomotion = character.GetComponent();
m_PerspectiveText.text = GetPerspectiveText();
m_PerspectiveText.enabled = true;
m_MovementTypeText.text = GetMovementTypeText();
m_MovementTypeText.enabled = true;
EventHandler.RegisterEvent(character, "OnCharacterChangeMovementType", OnMovementTypeChanged);
}
///
/// Returns the perspective text.
///
/// The perspective text.
private string GetPerspectiveText()
{
return m_CharacterLocomotion.FirstPersonPerspective ? "FIRST PERSON" : "THIRD PERSON";
}
///
/// Returns the movement type text.
///
/// The movement type text.
private string GetMovementTypeText()
{
var movementType = m_CharacterLocomotion.ActiveMovementType.GetType().Name.ToUpper();
if (movementType == "PSEUDO3D") {
return "2.5D";
}
return movementType;
}
///
/// The movement type has changed - update the UI.
///
/// The movement type that was changed.
/// Was the specified movement type activated?
private void OnMovementTypeChanged(MovementType movementType, bool activated)
{
if (!activated || m_CharacterLocomotion == null) {
return;
}
m_PerspectiveText.text = GetPerspectiveText();
m_MovementTypeText.text = GetMovementTypeText();
}
///
/// The object has been destroyed.
///
private void OnDestroy()
{
if (m_CharacterLocomotion != null) {
EventHandler.UnregisterEvent(m_CharacterLocomotion.gameObject, "OnCharacterChangeMovementType", OnMovementTypeChanged);
}
}
}
}