Unity UGUI技巧 之 EventTrigger鼠标事件使用一

2025-05-25 06:04:06

Unity UGUI技巧 之 EventTrigger鼠标事件使用一。本节介绍。使用“EventTrigger”来绑定鼠标的进入、退出、按下、抬起、点击5个事件的简单案例,具体如下

工具/原料

Unity

UGUI

一、知识要点

1、EventTrigger:1)功能简述cla衡痕贤伎ss in UnityEngine.EventSystemsInherits from:Mo荏鱿胫协noBehaviourImplements interfaces:IBeginDragHandler,ICancelHandler,IDeselectHandler,IDragHandler,IDropHandler,IEndDragHandler,IEventSystemHandler,IInitializePotentialDragHandler,IMoveHandler,IPointerClickHandler,IPointerDownHandler,IPointerEnterHandler,IPointerExitHandler,IPointerUpHandler,IScrollHandler,ISelectHandler,ISubmitHandler,IUpdateSelectedHandler.Receives events from the EventSystem and calls registered functions for each event.The EventTrigger can be used to specify functions you wish to be called for eachEventSystemevent. You can assign multiple functions to a single event and whenever the EventTrigger receives that event it will call those functions in the order they were provided.NOTE: Attaching this component to a GameObject will make that object intercept ALL events, and no events will propagate to parent objects.

二、UGUI技巧 之 EventTrigger鼠标事件使用一

1、打开Unity,新建一个工程,具体如下图

Unity UGUI技巧 之 EventTrigger鼠标事件使用一

3、在工程中,新建一个脚本“EventTriggerTest”,双击脚本或者右键“Open C# Project”打开脚本,具体如下图

Unity UGUI技巧 之 EventTrigger鼠标事件使用一

5、“EventTrigger哌囿亡噱Test”脚本具体内容如下 :usingUnityEngine;usingUnityEngine.UI;publicclassEventTrig爿讥旌护gerTest:MonoBehaviour{publicImageimage;publicvoidMouseEnter(){image.color=Color.red; Debug.Log("MouseEnter"); }publicvoidMouseExit(){image.color=Color.white; Debug.Log("MouseExit"); }publicvoidMouseDown(){image.transform.localScale=0.8f*Vector3.one; Debug.Log("MouseDown"); }publicvoidMouseUp(){image.transform.localScale=Vector3.one; Debug.Log("MouseUp"); }publicvoidMouseClick(){Debug.Log("MouseClick"); }}

6、脚本编译正确,回到Unity界面,在场景中,新建一个“GameObject”,把脚本“QuitGame”赋给“GameObject”,具体如下图

Unity UGUI技巧 之 EventTrigger鼠标事件使用一

8、运行场景,对图片进行鼠标的进入退出按下抬起点击事件,图片对应变化,控制台Console打印对应信息,具体如下图

Unity UGUI技巧 之 EventTrigger鼠标事件使用一

9、到此,《Unity UGUI技巧 之 EventTrigger鼠标事件使用一》讲解结束,谢谢

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢