using UnityEngine; using UnityEngine.Events; using Lean.Common; namespace Lean.Touch { /// This component can be used to pick objects in your scene that have the LeanPickable component attached. /// NOTE: This component requires you to call the SelectScreenPosition method externally (e.g. using the LeanFingerTap component). [HelpURL(LeanTouch.PlusHelpUrlPrefix + "LeanPick")] [AddComponentMenu(LeanTouch.ComponentPathPrefix + "Pick")] public class LeanPick : MonoBehaviour { [System.Serializable] public class LeanPickableEvent : UnityEvent {} public LeanScreenQuery ScreenQuery = new LeanScreenQuery(LeanScreenQuery.MethodType.Raycast); /// The tag required for an object to be selected. public string RequiredTag { set { requiredTag = value; } get { return requiredTag; } } [SerializeField] private string requiredTag; /// This event will be invoked when an object is picked. public LeanPickableEvent OnPickable { get { if (onPickable == null) onPickable = new LeanPickableEvent(); return onPickable; } } [SerializeField] private LeanPickableEvent onPickable; /// This method allows you to pick at the finger's StartScreenPosition. /// NOTE: This method be called from somewhere for this component to work (e.g. LeanFingerTap). public void PickStartScreenPosition(LeanFinger finger) { SelectScreenPosition(finger, finger.StartScreenPosition); } /// This method allows you to pick at the finger's current ScreenPosition. /// NOTE: This method be called from somewhere for this component to work (e.g. LeanFingerTap). public void PickScreenPosition(LeanFinger finger) { SelectScreenPosition(finger, finger.ScreenPosition); } /// This method allows you to initiate selection of a finger at a custom screen position. /// NOTE: This method be called from a custom script for this component to work. public void SelectScreenPosition(LeanFinger finger, Vector2 screenPosition) { var point = default(Vector3); var result = ScreenQuery.Query(gameObject, screenPosition, ref point); // Discard if tag doesn't match if (result != null && string.IsNullOrEmpty(RequiredTag) == false && result.tag != RequiredTag) { result = null; } if (result != null) { if (onPickable != null) { onPickable.Invoke(result); } result.InvokePick(finger, screenPosition); } } } } #if UNITY_EDITOR namespace Lean.Touch.Editor { using TARGET = LeanPick; [UnityEditor.CanEditMultipleObjects] [UnityEditor.CustomEditor(typeof(TARGET))] public class LeanPick_Editor : LeanEditor { protected override void OnInspector() { TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts); base.OnInspector(); Draw("ScreenQuery"); Draw("requiredTag", "The tag required for an object to be selected."); Separator(); var showUnusedEvents = DrawFoldout("Show Unused Events", "Show all events?"); if (showUnusedEvents == true || Any(tgts, t => t.OnPickable.GetPersistentEventCount() > 0)) { Draw("onPickable"); } } } } #endif