基于射线检测的鼠标事件
将一个带有碰撞体的GameObject挂上ColliderMouseEventTrigger,即可像UI一样收到相关的鼠标事件。
支持的鼠标事件:
https://github.com/VMware233/VMFramework/blob/main/Assets/VMFramework/Main/GameEvents/ColliderMouseEvent/Enums/MouseEventType.cs
using System;
using Sirenix.OdinInspector;
namespace VMFramework.GameEvents
{
[Flags]
public enum MouseEventType
{
[LabelText("æ äºä»¶")]
None = 0,
[LabelText("æéè¿å
¥")]
PointerEnter = 1 << 0,
[LabelText("æé离å¼")]
PointerExit = 1 << 1,
[LabelText("æéæ¬å")]
PointerHover = 1 << 2,
[LabelText("ä»»æé¼ æ 鮿ä¸")]
AnyMouseButtonDown = 1 << 3,
[LabelText("ä»»æé¼ æ 鮿¾å¼")]
AnyMouseButtonUp = 1 << 4,
[LabelText("ä»»æé¼ æ 鮿¬å")]
AnyMouseButtonStay = 1 << 5,
[LabelText("é¼ æ 左鮿ä¸")]
LeftMouseButtonDown = 1 << 6,
[LabelText("é¼ æ 左鮿¾å¼")]
LeftMouseButtonUp = 1 << 7,
[LabelText("é¼ æ å·¦é®ç¹å»")]
LeftMouseButtonClick = 1 << 8,
[LabelText("é¼ æ 左鮿¬å")]
LeftMouseButtonStay = 1 << 9,
[LabelText("é¼ æ å³é®æä¸")]
RightMouseButtonDown = 1 << 10,
[LabelText("é¼ æ å³é®æ¾å¼")]
RightMouseButtonUp = 1 << 11,
[LabelText("é¼ æ å³é®ç¹å»")]
RightMouseButtonClick = 1 << 12,
[LabelText("é¼ æ å³é®æ¬å")]
RightMouseButtonStay = 1 << 13,
[LabelText("é¼ æ ä¸é®æä¸")]
MiddleMouseButtonDown = 1 << 14,
[LabelText("é¼ æ ä¸é®æ¾å¼")]
MiddleMouseButtonUp = 1 << 15,
[LabelText("é¼ æ ä¸é®ç¹å»")]
MiddleMouseButtonClick = 1 << 16,
[LabelText("é¼ æ ä¸é®æ¬å")]
MiddleMouseButtonStay = 1 << 17,
[LabelText("ææ½å¼å§")]
DragBegin = 1 << 18,
[LabelText("ææ½ä¸")]
DragStay = 1 << 19,
[LabelText("ææ½ç»æ")]
DragEnd = 1 << 20,
}
}
添加Callback:
https://github.com/VMware233/VMFramework/blob/main/Assets/Examples/GameEvents/ColliderMouseEventDemo.cs
using System;
using System.Collections.Generic;
using UnityEngine;
using VMFramework.Core.Linq;
using VMFramework.GameEvents;
using VMFramework.Procedure;
namespace VMFramework.Examples
{
[ManagerCreationProvider("Demo")]
public sealed class ColliderMouseEventDemo : ManagerBehaviour<ColliderMouseEventDemo>
{
protected override void GetInitializationActions(ICollection<InitializationAction> actions)
{
base.GetInitializationActions(actions);
actions.Add(new InitializationAction(InitializationOrder.InitComplete, OnInitComplete, this));
}
private void OnInitComplete(Action onDone)
{
ColliderMouseEventManager.AddCallback(MouseEventType.PointerEnter, OnPointerEnter);
ColliderMouseEventManager.AddCallback(MouseEventType.PointerExit, OnPointerLeave);
onDone();
}
private void OnPointerEnter(ColliderMouseEventTrigger trigger)
{
Debug.Log("Pointer Entered: " + trigger.name);
}
private void OnPointerLeave(ColliderMouseEventTrigger trigger)
{
Debug.Log("Pointer Left: " + trigger.name);
}
}
}
最后更新于
这有帮助吗?