Unity3D UGUI事件处理

由于Unity3D官方出了新的UI系统,导致了不少的NGUI用户转向了UGUI。虽然现在UGUI相关的控件脚本等不及NGUI,但随着AssetStore里各种插件的开发,UGUI的发展前景还是比较看好的。

UGUI里面的事件处理代码跟NGUI不同,主要是通过实现接口方法来执行事件。

提供的接口如下:

  • IPointerEnterHandler – OnPointerEnter – Called when a pointer enters the object
  • IPointerExitHandler – OnPointerExit – Called when a pointer exits the object
  • IPointerDownHandler – OnPointerDown – Called when a pointer is pressed on the object
  • IPointerUpHandler – OnPointerUp – Called when a pointer is released (called on the original the pressed object)
  • IPointerClickHandler – OnPointerClick – Called when a pointer is pressed and released on the same object
  • IInitializePotentialDragHandler – OnInitializePotentialDrag – Called when a drag target is found, can be used to initialise values
  • IBeginDragHandler – OnBeginDrag – Called on the drag object when dragging is about to begin
  • IDragHandler – OnDrag – Called on the drag object when a drag is happening
  • IEndDragHandler – OnEndDrag – Called on the drag object when a drag finishes
  • IDropHandler – OnDrop – Called on the object where a drag finishes
  • IScrollHandler – OnScroll – Called when a mouse wheel scrolls
  • IUpdateSelectedHandler – OnUpdateSelected – Called on the selected object each tick
  • ISelectHandler – OnSelect – Called when the object becomes the selected object
  • IDeselectHandler – OnDeselect – Called on the selected object becomes deselected
  • IMoveHandler – OnMove – Called when a move event occurs (left, right, up, down, ect)
  • ISubmitHandler – OnSubmit – Called when the submit button is pressed
  • ICancelHandler – OnCancel – Called when the cancel button is pressed

其实很好理解,就是一些点击进入拖动等操作,实现相应的接口即可。

例如点击事件:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class TestPointerEnterEvent : MonoBehaviour,IPointerClickHandler
{
    public void OnPointerClick(PointerEventData data)
    {
        Debug.Log("Pointer click.");
    }
}

不过UGUI目前在鼠标事件和控件事件上有重叠,比如在点击控件的时候没有屏蔽掉鼠标事件,我也只是用其他方法来解决的,但我在stackoverflow上搜索的,暂时还没有完美的解决方案,不知道后续要怎么搞。

发表评论

电子邮件地址不会被公开。