UGUI组件EventTrigger用法

一.Unity编辑器中EventTrigger组件用法

1.添加事件类型

UGUI组件EventTrigger用法_第1张图片 

2.绑定gameObject指定组件的方法

UGUI组件EventTrigger用法_第2张图片

3.方法执行逻辑

public class NewBehaviourScript : MonoBehaviour
{
    public void PointerDown()
    {
        Debug.Log("Trigger PointerDown");
    }
}

4.按下鼠标,绑定方法成功调用 UGUI组件EventTrigger用法_第3张图片

 二.代码使用EventTrigger组件

    private void addEventTriggerType(EventTriggerType eventTriggerType, UnityAction callback)
    {
        //设置事件类型和回调
        EventTrigger.Entry entryObj = new EventTrigger.Entry ();
        entryObj.eventID = eventTriggerType;
        entryObj.callback.AddListener(callback);
        //将设置好的对象和组件关联起来
        EventTrigger componentET = gameObject.AddComponent ();
        componentET.triggers.Add(entryObj);
    }
    void Start()
    {
        addEventTriggerType(EventTriggerType.PointerDown, codeTriggerPointerDown);
    }

    public void codeTriggerPointerDown(BaseEventData data)
    {
        Debug.Log("codeTriggerPointerDown");
    }

UGUI组件EventTrigger用法_第4张图片 

三.EventTryggerType类型

PointerEnter = 0 鼠标进入
PointerExit = 1 鼠标离开
PointerDown = 2 鼠标按下
PointerUp = 3 鼠标抬起
PointerClick = 4 鼠标点击(鼠标抬起时已不在原UI上时不会触发,在PointerUp之后调用)
Drag = 5 鼠标拖拽
Drop = 6 拖拽结束时鼠标不在被拖拽UI上并且在另外一个UI上时触发(在PointerUp之后)
Scroll = 7 滑轮滚动时
UpdateSelected = 8 被选中后的每一帧
Select = 9 在被选中时
Deselect = 10 结束选中时
Move = 11 按方向键时
InitializePotentialDrag = 12 初始化拖拽(在PointerDown之后,PoinerUp之前调用,点击就会调用)
BeginDrag = 13 拖拽开始(鼠标按下不移动不会触发)
EndDrag = 14 拖拽结束
Submit = 15 默认为Enter键
Cancel = 16 默认为Esc键
 

你可能感兴趣的:(UGUI,Unity,unity)