前言:文章不定期更新,有转载的,也有自己整理的。
UI触发的事件类型
Unity3D的UGUI系统的将UI可能触发的事件分为12个类型,即EventTriggerType枚举的 12个值。如下图所示:
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
实现事件的方式:
方式一:继承基础接口实现
使用场景:在原有组件进行扩展,例如在imager上实现接口IDragHandler后,即可检测鼠标或手指在图片上滑动,实现 IPointerDownHandler接口,即可检测鼠标或手指是否触碰到图片
步骤一:创建ClickObject脚本。
步骤二:实现接口IPointerClickHandler,public void OnPointerClick(PointerEventData eventData)方法。
步骤三:创建一个名为Panel_IPointer的空对象。并且将ClickObject脚本附加到对象上。
步骤四:启动,并点击Panel_IPointer对象。在Console输出如下:
方式二:Unity3D编辑器操作设置实现(不推荐)
注意事项:由于是编辑器上实现的,代码上找不出所响应的事件是哪组件触发的,因此不建议使用此方式。在项目庞大时,不易维护。
步骤:创建一个命名为Empty的UI对象,用于接收和响应点击事件。创建一个名为Panel的UI对象,用于触发点击事件。
步骤:Panel对象添加EventTrigger组件," Add New" -> 选择" PointerClick"。将Empty对象拖拽到触发者位置。然后点击"No Function"选择我们写在Test脚本中的OnTestClick事件。
步骤:设置好这些之后。我们的事件触发就已经完成了。运行Unity3D。点击窗口中Panel对象。Console输出内容如下:
方式三:程序动态设置实现
使用场景:使用最多的方式。除了事先在代码上定义好各个组件后在Hierarchy上进行挂载,另一种方式就是动态的绑定,效率上当然没有已经绑定的要快。
下面我们就介绍代码控制。ScriptControl.cs脚本
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class BtnControl : MonoBehaviour {
public Button button;
void Start()
{
button = transform.gameObject.GetComponent<Button>();
}
void OnEnable()
{
button.onClick.AddListener(TestClick);
}
void OnDisable()
{
button.onClick.RemoveAllListeners();
}
public void TestClick()
{
Debug.Log("Test Click");
}
}
添加自定义监听事件:
使用场景:除了官方所给出的监听事件,我们也能实现出自己所想要的监听事件。
public class test : MonoBehaviour
{
public class ScrollEndEvent : UnityEvent
{
}
public ScrollEndEvent onScrollTop = new ScrollEndEvent();
public ScrollEndEvent onScrollBottom = new ScrollEndEvent();
public ScrollEndEvent onScrollLeft = new ScrollEndEvent();
public ScrollEndEvent onScrollRight = new ScrollEndEvent();
void OnEnable()
{
onScrollTop.AddListener(Top);
}
void OnDisable()
{
onScrollTop.RemoveAllListeners();
}
private void Top()
{
Debug.LogFormat("Test_Top");
}
public virtual void Update()
{
if (onScrollTop != null)
{
onScrollTop.Invoke();
}
if (onScrollBottom != null)
{
onScrollBottom.Invoke();
}
if (onScrollLeft != null)
{
onScrollLeft.Invoke();
}
if (onScrollRight != null)
{
onScrollRight.Invoke();
}
}
}
EventSystem组件提供的一些有意思的接口
其实文档都有http://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.html 只是也许你没有注意。
点击EventSystem对象,你可以看到运行时候的一些详细数据:
变量:
firstSelectedGameObject:这个值可以在面板设置,如果你需要游戏在启动的时候自动选中某个对象,需要鼠标的那一下点击。
currentSelectedGameObject:当前选中的对象,你可以通过这个值判断当前是否鼠标点击在对象上,因为也许你有拖动摄像机的功能,但是你又不喜欢点击某些对象的时候这个功能又被响应,所以通过这个变量判断是一个很好的办法。
接口:
IsPointerOverGameObject:当前鼠标是否在UI上或系统可检测到的对象。
SetSelectedGameObject:这个接口也许你会忽略,但是它很棒。因为你点击场景对象的时候,如果不调用这个接口,你的对象是收不到OnSelect事件的,currentSelectedGameObject的值也不会被设置的,必须在点击事件里调用这个接口设置选中对象!
Ex:
public void OnPointerClick(PointerEventData eventData)
{
print ("OnPointerClick...");
currEvent.SetSelectedGameObject(gameObject);
}
不用在场景里找EventSystem对象,EventSystem组件有一个current静态变量,它就是你要的对象,直接EventSystem.current即可使用。