using UnityEngine; using UnityEngine.Events; using Lean.Common; namespace Lean.Touch { /// This component counts how many seconds this LeanSelectable has been selected and sends it out via event. [HelpURL(LeanTouch.PlusHelpUrlPrefix + "LeanSelectableTime")] [AddComponentMenu(LeanTouch.ComponentPathPrefix + "Selectable Time")] public class LeanSelectableTime : LeanSelectableBehaviour { [System.Serializable] public class FloatEvent : UnityEvent {} public enum SendType { WhileSelected, WhileSelectedAndWhenDeselected, Always } /// This allows you to control when the events will be invoked. /// WhileSelected = Every frame while this object is selected. /// WhileSelectedAndWhenDeselected = Every frame while this object is selected, and the first frame it gets deselected. /// Always = Every frame, regardless of the selection. public SendType Send { set { send = value; } get { return send; } } [SerializeField] private SendType send; /// Based on the Send setting, this event will be invoked. /// Float = Seconds selected. public FloatEvent OnSeconds { get { if (onSeconds == null) onSeconds = new FloatEvent(); return onSeconds; } } [SerializeField] private FloatEvent onSeconds; [SerializeField] private float seconds; protected virtual void Update() { if (Selectable != null && Selectable.IsSelected == true) { seconds += Time.deltaTime; } else if (seconds > 0.0f) { seconds = 0.0f; if (send == SendType.WhileSelected) { return; } } else if (send != SendType.Always) { return; } if (onSeconds != null) { onSeconds.Invoke(seconds); } } } } #if UNITY_EDITOR namespace Lean.Touch.Editor { using TARGET = LeanSelectableTime; [UnityEditor.CanEditMultipleObjects] [UnityEditor.CustomEditor(typeof(TARGET))] public class LeanPlane_Editor : LeanEditor { protected override void OnInspector() { TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts); Draw("send", "This allows you to control when the events will be invoked.\n\nWhileSelected = Every frame while this object is selected.\n\nWhileSelectedAndWhenDeselected = Every frame while this object is selected, and the first frame it gets deselected.\n\nAlways = Every frame, regardless of the selection."); Separator(); Draw("onSeconds"); } } } #endif