using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using Lean.Transition;
using Lean.Common;
namespace Lean.Gui
{
/// This component allows you to perform an action as long as the mouse is hovering over the current UI element, or a finger is on top.
[HelpURL(LeanGui.HelpUrlPrefix + "LeanHover")]
[AddComponentMenu(LeanGui.ComponentMenuPrefix + "Hover")]
public class LeanHover : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
/// Enable this if you want EnterTransitions + OnEnter to be invoked once for each mouse/finger that enters this element.
public bool MultiEnter { set { multiEnter = value; } get { return multiEnter; } } [SerializeField] private bool multiEnter;
/// Enable this if you want ExitTransitions + OnExit to be invoked once for each mouse/finger that exits this element.
public bool MultiExit { set { multiExit = value; } get { return multiExit; } } [SerializeField] private bool multiExit;
/// This allows you to perform a transition when the mouse/finger enters this UI element.
/// You can create a new transition GameObject by right clicking the transition name, and selecting Create.
/// For example, the Graphic.color Transition (LeanGraphicColor) component can be used to change the color.
/// NOTE: Any transitions you perform here must be reverted in the Exit Transitions setting using a matching transition component.
public LeanPlayer EnterTransitions { get { if (enterTransitions == null) enterTransitions = new LeanPlayer(); return enterTransitions; } } [SerializeField] private LeanPlayer enterTransitions;
/// This allows you to perform a transition when the mouse/finger exits this UI element.
/// You can create a new transition GameObject by right clicking the transition name, and selecting Create.
/// For example, the Graphic.color Transition (LeanGraphicColor) component can be used to change the color.
public LeanPlayer ExitTransitions { get { if (exitTransitions == null) exitTransitions = new LeanPlayer(); return exitTransitions; } } [SerializeField] private LeanPlayer exitTransitions;
/// This allows you to perform an action when the mouse/finger enters this UI element.
public UnityEvent OnEnter { get { if (onEnter == null) onEnter = new UnityEvent(); return onEnter; } } [SerializeField] private UnityEvent onEnter;
/// This allows you to perform an action when the mouse/finger exits this UI element.
public UnityEvent OnExit { get { if (onExit == null) onExit = new UnityEvent(); return onExit; } } [SerializeField] private UnityEvent onExit;
/// Track the currently down pointers so we can toggle the transition.
[System.NonSerialized]
private List enteredPointers = new List();
public void OnPointerEnter(PointerEventData eventData)
{
if (eventData.button == PointerEventData.InputButton.Left)
{
enteredPointers.Add(eventData.pointerId);
if (MultiEnter == false && enteredPointers.Count > 1)
{
return;
}
if (enterTransitions != null)
{
enterTransitions.Begin();
}
if (onEnter != null)
{
onEnter.Invoke();
}
}
}
public void OnPointerExit(PointerEventData eventData)
{
if (enteredPointers.Remove(eventData.pointerId) == true)
{
if (MultiExit == false && enteredPointers.Count > 0)
{
return;
}
if (exitTransitions != null)
{
exitTransitions.Begin();
}
if (onExit != null)
{
onExit.Invoke();
}
}
}
}
}
#if UNITY_EDITOR
namespace Lean.Gui.Editor
{
using TARGET = LeanHover;
[UnityEditor.CanEditMultipleObjects]
[UnityEditor.CustomEditor(typeof(TARGET))]
public class LeanHover_Editor : LeanEditor
{
protected override void OnInspector()
{
TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts);
Draw("multiEnter", "Enable this if you want EnterTransitions + OnEnter to be invoked once for each mouse/finger that enters this element.");
Draw("multiExit", "Enable this if you want ExitTransitions + OnExit to be invoked once for each mouse/finger that exits this element.");
Separator();
Draw("enterTransitions", "This allows you to perform a transition when the mouse/finger enters this UI element. You can create a new transition GameObject by right clicking the transition name, and selecting Create. For example, the Graphic.color Transition (LeanGraphicColor) component can be used to change the color.\n\nNOTE: Any transitions you perform here must be reverted in the Exit Transitions setting using a matching transition component.");
Draw("exitTransitions", "This allows you to perform a transition when the mouse/finger exits this UI element. You can create a new transition GameObject by right clicking the transition name, and selecting Create. For example, the Graphic.color Transition (LeanGraphicColor) component can be used to change the color.");
Separator();
Draw("onEnter", "This allows you to perform an action when the mouse/finger enters this UI element.");
Draw("onExit", "This allows you to perform an action when the mouse/finger exits this UI element.");
}
}
}
#endif