2019-04-07 16:03:16 +02:00
|
|
|
using System.Collections.Generic;
|
2019-03-17 23:43:20 +01:00
|
|
|
using UnityEngine;
|
2019-04-07 16:03:16 +02:00
|
|
|
using UnityEngine.Serialization;
|
2019-10-01 17:27:22 +02:00
|
|
|
using UnityAtoms;
|
2019-03-17 23:43:20 +01:00
|
|
|
|
|
|
|
namespace UnityAtoms.UI
|
|
|
|
{
|
2019-10-15 23:20:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// A MonoBehaviour that you can add to a `CanvasGroup` and makes it transition based on a `StringVariable` value.
|
|
|
|
///
|
|
|
|
/// **TODO**: Add support for differnt transitions. Maybe integrate with DOTween?
|
|
|
|
/// </summary>
|
2019-05-29 18:52:27 +02:00
|
|
|
[AddComponentMenu("Unity Atoms/UI/Container")]
|
2019-09-25 21:05:06 +02:00
|
|
|
public class UIContainer : MonoBehaviour, IAtomListener<string>
|
2019-03-17 23:43:20 +01:00
|
|
|
{
|
2019-10-15 23:20:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Variable that we listens to.
|
|
|
|
/// </summary>
|
2019-03-17 23:43:20 +01:00
|
|
|
[SerializeField]
|
2019-04-08 16:14:50 +02:00
|
|
|
private StringVariable _UIStateVariable = null;
|
2019-04-07 16:03:16 +02:00
|
|
|
|
2019-10-15 23:20:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// A list of states that this `UIContainer` will be visible for.
|
|
|
|
/// </summary>
|
2019-03-17 23:43:20 +01:00
|
|
|
[SerializeField]
|
2019-04-08 16:14:50 +02:00
|
|
|
private List<StringConstant> _visibleForStates = null;
|
2019-03-17 23:43:20 +01:00
|
|
|
|
2019-04-07 16:03:16 +02:00
|
|
|
private void Start()
|
2019-03-17 23:43:20 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
StateNameChanged(_UIStateVariable.Value);
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
|
2019-10-15 23:20:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Handler for when the state is changed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="stateName"></param>
|
2019-03-17 23:43:20 +01:00
|
|
|
public void OnEventRaised(string stateName)
|
|
|
|
{
|
|
|
|
StateNameChanged(stateName);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void StateNameChanged(string stateName)
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
if (_visibleForStates.Exists((state) => state.Value == stateName))
|
2019-03-17 23:43:20 +01:00
|
|
|
{
|
|
|
|
GetComponent<CanvasGroup>().alpha = 1f;
|
|
|
|
GetComponent<CanvasGroup>().blocksRaycasts = true;
|
|
|
|
GetComponent<CanvasGroup>().interactable = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GetComponent<CanvasGroup>().alpha = 0f;
|
|
|
|
GetComponent<CanvasGroup>().blocksRaycasts = false;
|
|
|
|
GetComponent<CanvasGroup>().interactable = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
if (_UIStateVariable.Changed != null)
|
2019-03-17 23:43:20 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_UIStateVariable.Changed.RegisterListener(this);
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
if (_UIStateVariable.Changed != null)
|
2019-03-17 23:43:20 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_UIStateVariable.Changed.UnregisterListener(this);
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|