// --------------------------------------------------------------------------------------------------------------------
//
//
//
// Use this on Button texts to have some color transition on the text as well without corrupting button's behaviour.
//
// developer@exitgames.com
// --------------------------------------------------------------------------------------------------------------------
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Photon.Pun.UtilityScripts
{
///
/// Use this on toggles texts to have some color transition on the text depending on the isOn State.
///
[RequireComponent(typeof(Text))]
public class TextToggleIsOnTransition : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
///
/// The toggle Component.
///
public Toggle toggle;
Text _text;
///
/// The color of the normal on transition state.
///
public Color NormalOnColor= Color.white;
///
/// The color of the normal off transition state.
///
public Color NormalOffColor = Color.black;
///
/// The color of the hover on transition state.
///
public Color HoverOnColor= Color.black;
///
/// The color of the hover off transition state.
///
public Color HoverOffColor = Color.black;
bool isHover;
public void OnEnable()
{
_text = GetComponent();
OnValueChanged (toggle.isOn);
toggle.onValueChanged.AddListener(OnValueChanged);
}
public void OnDisable()
{
toggle.onValueChanged.RemoveListener(OnValueChanged);
}
public void OnValueChanged(bool isOn)
{
_text.color = isOn? (isHover?HoverOnColor:HoverOnColor) : (isHover?NormalOffColor:NormalOffColor) ;
}
public void OnPointerEnter(PointerEventData eventData)
{
isHover = true;
_text.color = toggle.isOn?HoverOnColor:HoverOffColor;
}
public void OnPointerExit(PointerEventData eventData)
{
isHover = false;
_text.color = toggle.isOn?NormalOnColor:NormalOffColor;
}
}
}