141 lines
4.8 KiB
C#
141 lines
4.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using Lean.Common;
|
|
using FSA = UnityEngine.Serialization.FormerlySerializedAsAttribute;
|
|
|
|
namespace Lean.Touch
|
|
{
|
|
/// <summary>This component calls the OnUp event when a finger stops touching the screen.</summary>
|
|
[HelpURL(LeanTouch.HelpUrlPrefix + "LeanFingerUp")]
|
|
[AddComponentMenu(LeanTouch.ComponentPathPrefix + "Finger Up")]
|
|
public class LeanFingerUp : MonoBehaviour
|
|
{
|
|
[System.Serializable] public class LeanFingerEvent : UnityEvent<LeanFinger> {}
|
|
[System.Serializable] public class Vector3Event : UnityEvent<Vector3> {}
|
|
[System.Serializable] public class Vector2Event : UnityEvent<Vector2> {}
|
|
|
|
/// <summary>Ignore fingers with StartedOverGui?</summary>
|
|
public bool IgnoreStartedOverGui { set { ignoreStartedOverGui = value; } get { return ignoreStartedOverGui; } } [FSA("IgnoreStartedOverGui")] [SerializeField] private bool ignoreStartedOverGui = true;
|
|
|
|
/// <summary>Ignore fingers with OverGui?</summary>
|
|
public bool IgnoreIsOverGui { set { ignoreIsOverGui = value; } get { return ignoreIsOverGui; } } [FSA("IgnoreIsOverGui")] [SerializeField] private bool ignoreIsOverGui;
|
|
|
|
/// <summary>If the specified object is set and isn't selected, then this component will do nothing.</summary>
|
|
public LeanSelectable RequiredSelectable { set { requiredSelectable = value; } get { return requiredSelectable; } } [FSA("RequiredSelectable")] [SerializeField] private LeanSelectable requiredSelectable;
|
|
|
|
/// <summary>This event will be called if the above conditions are met when your finger stops touching the screen.</summary>
|
|
public LeanFingerEvent OnFinger { get { if (onFinger == null) onFinger = new LeanFingerEvent(); return onFinger; } } [FSA("onUp")] [FSA("OnUp")] [SerializeField] private LeanFingerEvent onFinger;
|
|
|
|
/// <summary>The method used to find world coordinates from a finger. See LeanScreenDepth documentation for more information.</summary>
|
|
public LeanScreenDepth ScreenDepth = new LeanScreenDepth(LeanScreenDepth.ConversionType.DepthIntercept);
|
|
|
|
/// <summary>This event will be called if the above conditions are met when your finger stops touching the screen.
|
|
/// Vector3 = Finger position in world space.</summary>
|
|
public Vector3Event OnWorld { get { if (onWorld == null) onWorld = new Vector3Event(); return onWorld; } } [SerializeField] [FSA("onPosition")] private Vector3Event onWorld;
|
|
|
|
/// <summary>This event will be called if the above conditions are met when your finger stops touching the screen.
|
|
/// Vector2 = Finger position in screen space.</summary>
|
|
public Vector2Event OnScreen { get { if (onScreen == null) onScreen = new Vector2Event(); return onScreen; } } [SerializeField] private Vector2Event onScreen;
|
|
|
|
#if UNITY_EDITOR
|
|
protected virtual void Reset()
|
|
{
|
|
requiredSelectable = GetComponentInParent<LeanSelectable>();
|
|
}
|
|
#endif
|
|
|
|
protected virtual void Start()
|
|
{
|
|
if (requiredSelectable == null)
|
|
{
|
|
requiredSelectable = GetComponentInParent<LeanSelectable>();
|
|
}
|
|
}
|
|
|
|
protected virtual void OnEnable()
|
|
{
|
|
LeanTouch.OnFingerUp += HandleFingerUp;
|
|
}
|
|
|
|
protected virtual void OnDisable()
|
|
{
|
|
LeanTouch.OnFingerUp -= HandleFingerUp;
|
|
}
|
|
|
|
private void HandleFingerUp(LeanFinger finger)
|
|
{
|
|
if (ignoreStartedOverGui == true && finger.StartedOverGui == true)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (ignoreIsOverGui == true && finger.IsOverGui == true)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (requiredSelectable != null && requiredSelectable.IsSelected == false)
|
|
{
|
|
return;
|
|
}
|
|
|
|
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 = LeanFingerUp;
|
|
|
|
[UnityEditor.CanEditMultipleObjects]
|
|
[UnityEditor.CustomEditor(typeof(TARGET))]
|
|
public class LeanFingerUp_Editor : LeanEditor
|
|
{
|
|
protected override void OnInspector()
|
|
{
|
|
TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts);
|
|
|
|
Draw("ignoreStartedOverGui", "Ignore fingers with StartedOverGui?");
|
|
Draw("ignoreIsOverGui", "Ignore fingers with OverGui?");
|
|
Draw("requiredSelectable", "If the specified object is set and isn't selected, then this component will do nothing.");
|
|
|
|
Separator();
|
|
|
|
var showUnusedEvents = DrawFoldout("Show Unused Events", "Show all events?");
|
|
|
|
if (Any(tgts, t => t.OnFinger.GetPersistentEventCount() > 0) == true || showUnusedEvents == true)
|
|
{
|
|
Draw("onFinger");
|
|
}
|
|
|
|
if (Any(tgts, t => t.OnWorld.GetPersistentEventCount() > 0) == true || showUnusedEvents == true)
|
|
{
|
|
Draw("ScreenDepth");
|
|
Draw("onWorld");
|
|
}
|
|
|
|
if (Any(tgts, t => t.OnScreen.GetPersistentEventCount() > 0) == true || showUnusedEvents == true)
|
|
{
|
|
Draw("onScreen");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif |