using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using Lean.Transition;
using Lean.Common;
namespace Lean.Gui
{
/// This component will place the current RectTransform above the currently selected object.
[RequireComponent(typeof(RectTransform))]
[HelpURL(LeanGui.HelpUrlPrefix + "LeanSelectionHighlight")]
[AddComponentMenu(LeanGui.ComponentMenuPrefix + "Selection Highlight")]
public class LeanSelectionHighlight : MonoBehaviour
{
/// The camera rendering the target transform/position.
/// None = MainCamera.
public Camera WorldCamera { set { worldCamera = value; } get { return worldCamera; } } [SerializeField] private Camera worldCamera;
/// This allows you to perform a transition when the highlight begins.
/// 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 Hide Transitions setting using a matching transition component.
public LeanPlayer ShowTransitions { get { if (showTransitions == null) showTransitions = new LeanPlayer(); return showTransitions; } } [SerializeField] private LeanPlayer showTransitions;
/// This allows you to perform a transition when the highlight ends.
/// 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 Show Transitions setting using a matching transition component.
public LeanPlayer HideTransitions { get { if (hideTransitions == null) hideTransitions = new LeanPlayer(); return hideTransitions; } } [SerializeField] private LeanPlayer hideTransitions;
/// This allows you to perform an action when the highlight starts.
public UnityEvent OnShow { get { if (onShow == null) onShow = new UnityEvent(); return onShow; } } [SerializeField] private UnityEvent onShow;
/// This allows you to perform an action when the highlight ends.
public UnityEvent OnHide { get { if (onHide == null) onHide = new UnityEvent(); return onHide; } } [SerializeField] private UnityEvent onHide;
[SerializeField]
private bool showing;
[System.NonSerialized]
protected RectTransform rectTransform;
[System.NonSerialized]
protected RectTransform canvasRectTransform;
protected virtual void LateUpdate()
{
var eventSystem = EventSystem.current;
var show = false;
if (eventSystem != null)
{
var selected = eventSystem.currentSelectedGameObject;
if (selected != null)
{
var selectedRect = selected.GetComponent();
if (selectedRect != null)
{
show = UpdateRect(selectedRect);
}
}
}
if (showing != show)
{
showing = show;
if (showing == true)
{
if (showTransitions != null)
{
showTransitions.Begin();
}
}
else
{
if (hideTransitions != null)
{
hideTransitions.Begin();
}
}
}
}
private bool UpdateRect(RectTransform target)
{
var camera = worldCamera;
if (camera == null)
{
camera = Camera.main;
}
if (camera != null)
{
if (rectTransform == null)
{
rectTransform = GetComponent();
}
if (canvasRectTransform == null)
{
var canvas = GetComponentInParent