using UnityEngine; using UnityEngine.Events; using System.Collections.Generic; using Lean.Common; namespace Lean.Touch { /// This component allows you to detect when a specific amount of fingers stop touching the screen. [HelpURL(LeanTouch.PlusHelpUrlPrefix + "LeanMultiUp")] [AddComponentMenu(LeanTouch.ComponentPathPrefix + "Multi Up")] public class LeanMultiUp : MonoBehaviour { [System.Serializable] public class LeanFingerEvent : UnityEvent {} [System.Serializable] public class Vector3Event : UnityEvent {} [System.Serializable] public class Vector2Event : UnityEvent {} /// The method used to find fingers to use with this component. See LeanFingerFilter documentation for more information. public LeanFingerFilter Use = new LeanFingerFilter(true); /// This event will be called if the above conditions are met when your finger stops touching the screen. public LeanFingerEvent OnFinger { get { if (onFinger == null) onFinger = new LeanFingerEvent(); return onFinger; } } [SerializeField] private LeanFingerEvent onFinger; /// The method used to find world coordinates from a finger. See LeanScreenDepth documentation for more information. public LeanScreenDepth ScreenDepth = new LeanScreenDepth(LeanScreenDepth.ConversionType.DepthIntercept); /// This event will be called if the above conditions are met when your finger stops touching the screen. /// Vector3 = Finger position in world space. public Vector3Event OnWorld { get { if (onWorld == null) onWorld = new Vector3Event(); return onWorld; } } [SerializeField] private Vector3Event onWorld; /// This event will be called if the above conditions are met when your finger stops touching the screen. /// Vector2 = Finger position in screen space. public Vector2Event OnScreen { get { if (onScreen == null) onScreen = new Vector2Event(); return onScreen; } } [SerializeField] private Vector2Event onScreen; [System.NonSerialized] private List fingers = new List(); /// If you've set Use to ManuallyAddedFingers, then you can call this method to manually add a finger. public void AddFinger(LeanFinger finger) { Use.AddFinger(finger); } /// If you've set Use to ManuallyAddedFingers, then you can call this method to manually remove a finger. public void RemoveFinger(LeanFinger finger) { Use.RemoveFinger(finger); } /// If you've set Use to ManuallyAddedFingers, then you can call this method to manually remove all fingers. public void RemoveAllFingers() { Use.RemoveAllFingers(); } #if UNITY_EDITOR protected virtual void Reset() { Use.UpdateRequiredSelectable(gameObject); } #endif protected virtual void Awake() { Use.UpdateRequiredSelectable(gameObject); } protected virtual void Update() { // Get fingers var fingers = Use.UpdateAndGetFingers(); foreach (var finger in fingers) { if (finger.Up == true) { if (onFinger != null) { onFinger.Invoke(finger); } if (onWorld != null) { var position = ScreenDepth.Convert(finger.StartScreenPosition, gameObject); onWorld.Invoke(position); } if (onScreen != null) { onScreen.Invoke(finger.ScreenPosition); } } } } } } #if UNITY_EDITOR namespace Lean.Touch.Editor { using TARGET = LeanMultiUp; [UnityEditor.CanEditMultipleObjects] [UnityEditor.CustomEditor(typeof(TARGET))] public class LeanMultiUp_Editor : LeanEditor { private bool showUnusedEvents; protected override void OnInspector() { TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts); Draw("Use"); Separator(); var usedA = Any(tgts, t => t.OnFinger.GetPersistentEventCount() > 0); var usedB = Any(tgts, t => t.OnWorld.GetPersistentEventCount() > 0); var usedC = Any(tgts, t => t.OnScreen.GetPersistentEventCount() > 0); var showUnusedEvents = DrawFoldout("Show Unused Events", "Show all events?"); if (usedA == true || showUnusedEvents == true) { Draw("onFinger"); } if (usedB == true || showUnusedEvents == true) { Draw("ScreenDepth"); Draw("onWorld"); } if (usedC == true || showUnusedEvents == true) { Draw("onScreen"); } } } } #endif