You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
3.6 KiB
132 lines
3.6 KiB
8 years ago
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
|
||
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
|
||
9 years ago
|
|
||
9 years ago
|
using UnityEngine;
|
||
9 years ago
|
using UnityEngine.EventSystems;
|
||
10 years ago
|
|
||
|
namespace Fungus
|
||
|
{
|
||
8 years ago
|
/// <summary>
|
||
|
/// Detects mouse clicks and touches on a Game Object, and sends an event to all Flowchart event handlers in the scene.
|
||
|
/// The Game Object must have a Collider or Collider2D component attached.
|
||
|
/// Use in conjunction with the ObjectClicked Flowchart event handler.
|
||
|
/// </summary>
|
||
8 years ago
|
public class Clickable2D : MonoBehaviour, IClickable2D, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
|
||
8 years ago
|
{
|
||
|
[Tooltip("Is object clicking enabled")]
|
||
8 years ago
|
[SerializeField] protected bool clickEnabled = true;
|
||
10 years ago
|
|
||
8 years ago
|
[Tooltip("Mouse texture to use when hovering mouse over object")]
|
||
8 years ago
|
[SerializeField] protected Texture2D hoverCursor;
|
||
9 years ago
|
|
||
8 years ago
|
[Tooltip("Use the UI Event System to check for clicks. Clicks that hit an overlapping UI object will be ignored. Camera must have a PhysicsRaycaster component, or a Physics2DRaycaster for 2D colliders.")]
|
||
8 years ago
|
[SerializeField] protected bool useEventSystem;
|
||
9 years ago
|
|
||
8 years ago
|
protected virtual void ChangeCursor(Texture2D cursorTexture)
|
||
|
{
|
||
|
if (!clickEnabled)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
Cursor.SetCursor(cursorTexture, Vector2.zero, CursorMode.Auto);
|
||
|
}
|
||
|
|
||
|
protected virtual void DoPointerClick()
|
||
|
{
|
||
|
if (!clickEnabled)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// TODO: Cache these objects for faster lookup
|
||
|
ObjectClicked[] handlers = GameObject.FindObjectsOfType<ObjectClicked>();
|
||
|
foreach (ObjectClicked handler in handlers)
|
||
|
{
|
||
|
handler.OnObjectClicked(this);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected virtual void DoPointerEnter()
|
||
|
{
|
||
|
ChangeCursor(hoverCursor);
|
||
|
}
|
||
|
|
||
|
protected virtual void DoPointerExit()
|
||
|
{
|
||
|
// Always reset the mouse cursor to be on the safe side
|
||
|
SetMouseCursor.ResetMouseCursor();
|
||
|
}
|
||
|
|
||
8 years ago
|
#region Legacy OnMouseX methods
|
||
8 years ago
|
|
||
8 years ago
|
protected virtual void OnMouseDown()
|
||
|
{
|
||
|
if (!useEventSystem)
|
||
|
{
|
||
|
DoPointerClick();
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
protected virtual void OnMouseEnter()
|
||
|
{
|
||
|
if (!useEventSystem)
|
||
|
{
|
||
|
DoPointerEnter();
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
protected virtual void OnMouseExit()
|
||
|
{
|
||
|
if (!useEventSystem)
|
||
|
{
|
||
|
DoPointerExit();
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
|
#endregion
|
||
|
|
||
|
#region IClickable2D implementation
|
||
|
|
||
|
public bool ClickEnabled { set { clickEnabled = value; } }
|
||
|
|
||
8 years ago
|
#endregion
|
||
8 years ago
|
|
||
8 years ago
|
#region IPointerClickHandler implementation
|
||
|
|
||
8 years ago
|
public void OnPointerClick(PointerEventData eventData)
|
||
|
{
|
||
8 years ago
|
if (useEventSystem)
|
||
|
{
|
||
|
DoPointerClick();
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
#endregion
|
||
|
|
||
|
#region IPointerEnterHandler implementation
|
||
|
|
||
8 years ago
|
public void OnPointerEnter(PointerEventData eventData)
|
||
|
{
|
||
|
if (useEventSystem)
|
||
|
{
|
||
|
DoPointerEnter();
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
#endregion
|
||
|
|
||
|
#region IPointerExitHandler implementation
|
||
|
|
||
8 years ago
|
public void OnPointerExit(PointerEventData eventData)
|
||
|
{
|
||
|
if (useEventSystem)
|
||
|
{
|
||
|
DoPointerExit();
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
#endregion
|
||
|
}
|
||
9 years ago
|
}
|