/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.UI
{
using Opsive.Shared.Events;
using Opsive.Shared.Game;
using Opsive.UltimateCharacterController.Traits;
using UnityEngine;
using UnityEngine.UI;
///
/// The AttributeMonitor will update the UI for the character's attributes.
///
public class AttributeMonitor : CharacterMonitor
{
[Tooltip("The AttributeManager that contains the attribute. If null the character's Attribute Manager will be used.")]
[SerializeField] protected AttributeManager m_AttributeManager;
[Tooltip("The name of the attribute that the UI should monitor.")]
[SerializeField] protected string m_AttributeName = "Health";
[Tooltip("A reference used to the slider used to show the attribute value.")]
[SerializeField] protected Slider m_Slider;
private Attribute m_Attribute;
///
/// Initialize the default values.
///
protected override void Awake()
{
if (m_Slider == null) {
m_Slider = GetComponent();
}
if (m_AttributeManager != null) {
EventHandler.RegisterEvent(m_AttributeManager.gameObject, "OnAttributeUpdateValue", OnUpdateValue);
}
// The monitor can't display if there is no slider.
if (m_Slider != null) {
base.Awake();
} else {
enabled = false;
}
}
///
/// Attaches the monitor to the specified character.
///
/// The character to attach the monitor to.
protected override void OnAttachCharacter(GameObject character)
{
if (m_Character != null) {
if (m_AttributeManager != null && m_AttributeManager.gameObject == character) {
EventHandler.UnregisterEvent(m_AttributeManager.gameObject, "OnAttributeUpdateValue", OnUpdateValue);
m_AttributeManager = null;
}
}
base.OnAttachCharacter(character);
if (m_Character == null) {
return;
}
if (m_AttributeManager == null) {
m_AttributeManager = m_Character.GetCachedComponent();
EventHandler.RegisterEvent(m_AttributeManager.gameObject, "OnAttributeUpdateValue", OnUpdateValue);
}
if (m_AttributeManager == null) {
enabled = false;
gameObject.SetActive(false);
return;
}
m_Attribute = m_AttributeManager.GetAttribute(m_AttributeName);
if (m_Attribute == null) {
enabled = false;
gameObject.SetActive(false);
return;
}
enabled = true;
m_Slider.value = (m_Attribute.Value - m_Attribute.MinValue) / (m_Attribute.MaxValue - m_Attribute.MinValue);
}
///
/// The attribute's value has been updated.
///
/// The attribute that was updated.
private void OnUpdateValue(Attribute attribute)
{
if (attribute != m_Attribute) {
return;
}
m_Slider.value = (m_Attribute.Value - m_Attribute.MinValue) / (m_Attribute.MaxValue - m_Attribute.MinValue);
}
///
/// The object has been destroyed.
///
protected override void OnDestroy()
{
base.OnDestroy();
if (m_AttributeManager != null) {
EventHandler.UnregisterEvent(m_AttributeManager.gameObject, "OnAttributeUpdateValue", OnUpdateValue);
}
}
///
/// Can the UI be shown?
///
/// True if the UI can be shown.
protected override bool CanShowUI()
{
return base.CanShowUI() && m_Attribute != null;
}
}
}