using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using Lean.Transition; using Lean.Common; namespace Lean.Gui { /// This component allows you to perform a transition when this UI element is selected. [HelpURL(LeanGui.HelpUrlPrefix + "LeanSelection")] [AddComponentMenu(LeanGui.ComponentMenuPrefix + "Selection")] public class LeanSelection : MonoBehaviour { /// This allows you to perform a transition when this UI element is selected. /// 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 Deselect Transitions setting using a matching transition component. public LeanPlayer SelectTransitions { get { if (selectTransitions == null) selectTransitions = new LeanPlayer(); return selectTransitions; } } [SerializeField] private LeanPlayer selectTransitions; /// This allows you to perform a transition when this UI element is deselected. /// 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 Select Transitions setting using a matching transition component. public LeanPlayer DeselectTransitions { get { if (deselectTransitions == null) deselectTransitions = new LeanPlayer(); return deselectTransitions; } } [SerializeField] private LeanPlayer deselectTransitions; [System.NonSerialized] private bool lastSelected; protected virtual void Update() { var selected = false; var eventSystem = EventSystem.current; if (eventSystem != null) { if (eventSystem.currentSelectedGameObject == gameObject) { selected = true; } } if (lastSelected != selected) { if (selected == true) { if (selectTransitions != null) { selectTransitions.Begin(); } } else { if (deselectTransitions != null) { deselectTransitions.Begin(); } } lastSelected = selected; } } } } #if UNITY_EDITOR namespace Lean.Gui.Editor { using TARGET = LeanSelection; [UnityEditor.CanEditMultipleObjects] [UnityEditor.CustomEditor(typeof(TARGET))] public class LeanSelection_Editor : LeanEditor { protected override void OnInspector() { TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts); if (Any(tgts, t => t.GetComponent() == null)) { Error("This GameObject doesn't have a Selectable component. You must add one, such as a LeanButton."); } Draw("selectTransitions", "This allows you to perform a transition when this UI element is selected. 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 Deselect Transitions setting using a matching transition component."); Draw("deselectTransitions", "This allows you to perform a transition when this UI element is deselected. 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 Select Transitions setting using a matching transition component."); } } } #endif