ugui的ugui buttonn怎么工作的

程序写累了,就来玩玩酷跑小游戏吧,嘿嘿。
雨松MOMO送你一首歌曲,嘿嘿。
UGUI研究院之控件以及按钮的监听事件系统(五)
UGUI研究院之控件以及按钮的监听事件系统(五)
围观78291次
编辑日期: 字体:
继续学习,我相信大家在做NGUI开发的时候处理事件都会用到UIEventListener,那么UGUI中怎么办呢?先看UGUI的事件有那些吧。
Supported Events
The Eventsystem supports a number of events, and they can be customised further in user custom user written InputModules.
The events that are supported by the StandaloneInputModule and TouchInputModule are provided by interface and can be implemented on a MonoBehaviour by implementing the interface. If you have a valid EventSystem configured the events will be called at the correct time.
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
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
OK 怎么样才能让UGUI监听的方式和NGUI差不多呢? 这里我给大家引一个思路,把下面代码放在你的项目里。
123456789101112131415161718192021222324252627282930313233343536373839404142
using UnityEngine;using System.Collections;using UnityEngine.EventSystems;public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger{ public delegate void VoidDelegate (GameObject go); public VoidDelegate onClick; public VoidDelegate onDown; public VoidDelegate onEnter; public VoidDelegate onExit; public VoidDelegate onUp; public VoidDelegate onSelect; public VoidDelegate onUpdateSelect;& static public EventTriggerListener Get (GameObject go) {
EventTriggerListener listener = go.GetComponent&EventTriggerListener&();
if (listener == null) listener = go.AddComponent&EventTriggerListener&();
return listener; } public override void OnPointerClick(PointerEventData eventData) {
if(onClick != null)
onClick(gameObject); } public override void OnPointerDown (PointerEventData eventData){
if(onDown != null) onDown(gameObject); } public override void OnPointerEnter (PointerEventData eventData){
if(onEnter != null) onEnter(gameObject); } public override void OnPointerExit (PointerEventData eventData){
if(onExit != null) onExit(gameObject); } public override void OnPointerUp (PointerEventData eventData){
if(onUp != null) onUp(gameObject); } public override void OnSelect (BaseEventData eventData){
if(onSelect != null) onSelect(gameObject); } public override void OnUpdateSelected (BaseEventData eventData){
if(onUpdateSelect != null) onUpdateSelect(gameObject); }}
然后在你的界面里面写入监听按钮的代码。
<div class="crayon-num" data-line="crayon-57f4a<div class="crayon-num crayon-striped-num" data-line="crayon-57f4a<div class="crayon-num" data-line="crayon-57f4a<div class="crayon-num crayon-striped-num" data-line="crayon-57f4a<div class="crayon-num" data-line="crayon-57f4a<div class="crayon-num crayon-striped-num" data-line="crayon-57f4a<div class="crayon-num" data-line="crayon-57f4a<div class="crayon-num crayon-striped-num" data-line="crayon-57f4a<div class="crayon-num" data-line="crayon-57f4a<div class="crayon-num crayon-striped-num" data-line="crayon-57f4a<div class="crayon-num" data-line="crayon-57f4a<div class="crayon-num crayon-striped-num" data-line="crayon-57f4a<div class="crayon-num" data-line="crayon-57f4a<div class="crayon-num crayon-striped-num" data-line="crayon-57f4a<div class="crayon-num" data-line="crayon-57f4a<div class="crayon-num crayon-striped-num" data-line="crayon-57f4a<div class="crayon-num" data-line="crayon-57f4a<div class="crayon-num crayon-striped-num" data-line="crayon-57f4a<div class="crayon-num" data-line="crayon-57f4a<div class="crayon-num crayon-striped-num" data-line="crayon-57f4a<div class="crayon-num" data-line="crayon-57f4a<div class="crayon-num crayon-striped-num" data-line="crayon-57f4a<div class="crayon-num" data-line="crayon-57f4a
using UnityEngine;using System.Collections;using UnityEngine.UI;using UnityEngine.EventSystems;using UnityEngine.Events;public class UIMain : MonoBehaviour { Button button; Image image; void Start ()
button = transform.Find("Button").GetComponent&Button&();
image = transform.Find("Image").GetComponent&Image&();
EventTriggerListener.Get(button.gameObject).onClick =OnButtonClick;
EventTriggerListener.Get(image.gameObject).onClick =OnButtonClick; }& private void OnButtonClick(GameObject go){
//在这里监听按钮的点击事件
if(go == button.gameObject){
Debug.Log ("DoSomeThings");
虽然还有一些别的监听方式,但是我觉得这种方式是最科学的,大家可根据自己项目的需求继续拓展EventTriggerListener类。
本文固定链接:
转载请注明:
雨松MOMO提醒您:亲,如果您觉得本文不错,快快将这篇文章分享出去吧 。另外请点击网站顶部彩色广告或者捐赠支持本站发展,谢谢!
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝直接向我捐款哦。
您可能还会对这些文章感兴趣!UGUI运行时如果鼠标直接在button上UI无法响应_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
UGUI运行时如果鼠标直接在button上UI无法响应
上传于||暂无简介
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
你可能喜欢程序写累了,就来玩玩酷跑小游戏吧,嘿嘿。
雨松MOMO送你一首歌曲,嘿嘿。
UGUI研究院之不规则按钮的响应区域(十四)
UGUI研究院之不规则按钮的响应区域(十四)
围观15016次
编辑日期: 字体:
接上一篇文章
比如一些不规则按钮最好可以设置它的响应区域。如下图所示,用Polygon Collider2D组件圈出精灵响应事件的区域。
123456789101112131415161718192021222324252627
using UnityEngine;using System.Collections;using UnityEngine.UI;&public class ImagePlus : Image { PolygonCollider2D collider; void Awake() {
collider = GetComponent&PolygonCollider2D&(); } override public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera) {
return ContainsPoint(collider.points,sp); }
bool ContainsPoint ( Vector2[]polyPoints, Vector2 p) {
var j = polyPoints.Length-1;
var inside = false;
for (int i = 0; i & polyPoints.Length; j = i++) {
polyPoints[i].x+=transform.position.x;
polyPoints[i].y+=transform.position.y;
if ( ((polyPoints[i].y &= p.y && p.y & polyPoints[j].y) || (polyPoints[j].y &= p.y && p.y & polyPoints[i].y)) &&
&&&&(p.x & (polyPoints[j].x - polyPoints[i].x) * (p.y - polyPoints[i].y) / (polyPoints[j].y - polyPoints[i].y) + polyPoints[i].x))
inside = !inside;
return inside;
如果只想用BoxCollider2D那么就简单的改改上面AABB的算法就行了。
本文固定链接:
转载请注明:
雨松MOMO提醒您:亲,如果您觉得本文不错,快快将这篇文章分享出去吧 。另外请点击网站顶部彩色广告或者捐赠支持本站发展,谢谢!
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝直接向我捐款哦。
您可能还会对这些文章感兴趣!}

我要回帖

更多关于 ugui button 点击事件 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信