新增滑鼠偵聽器
有時,你希望在元件本身提供的特定事件(特別是滑鼠事件)上新增偵聽器。為此,你必須使用 EventTrigger
元件自行新增它們:
using UnityEngine;
using UnityEngine.EventSystems;
[RequireComponent(typeof( EventTrigger ))]
public class CustomListenersExample : MonoBehaviour
{
void Start( )
{
EventTrigger eventTrigger = GetComponent<EventTrigger>( );
EventTrigger.Entry entry = new EventTrigger.Entry( );
entry.eventID = EventTriggerType.PointerDown;
entry.callback.AddListener( ( data ) => { OnPointerDownDelegate( (PointerEventData)data ); } );
eventTrigger.triggers.Add( entry );
}
public void OnPointerDownDelegate( PointerEventData data )
{
Debug.Log( "OnPointerDownDelegate called." );
}
}
各種 eventID 都是可能的:
- PointerEnter
- PointerExit
- PointerDown
- PointerUp
- PointerClick
- 拖動
- 下降
- 滾動
- UpdateSelected
- 選擇
- 取消
- 移動
- InitializePotentialDrag
- BeginDrag
- EndDrag
- 提交
- 取消