UGUI UI事件通用脚本

Unity中的UI系统之UGUI UGUI是什么UGUI是Unity引擎内自带的UI系统官方称之为:Unity UI是目前Unity商业游戏开发中使用最广泛的UI系统开发解决方案它是基于Unity游戏对象的UI系统,只能用来做游戏UI功能不能用于开发Unity编辑器中内置的用户界面如何在Unity中使用UGUI直接在Hierarchy窗口中右键选择UIUI中所有内容都是UGUI相关控件六大基础组件1、Canvas对象上依附的:①Canvas:画布组件,主要用于渲染UI控件②。 阅读详情

UGUI的一个通用EventListener脚本

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


public class UIEventListener : MonoBehaviour,  
IPointerClickHandler,  
IPointerDownHandler,  
IPointerEnterHandler,  
IPointerExitHandler,  
IPointerUpHandler,  
ISelectHandler,  
IUpdateSelectedHandler,  
IDeselectHandler,  
IDragHandler,  
IEndDragHandler,  
IDropHandler,  
IScrollHandler,  
IMoveHandler
{  
//    public delegate void VoidDelegate (GameObject go);  
    public Action<GameObject> onClick;  
    public Action<GameObject> onDown;  
    public Action<GameObject> onEnter;  
    public Action<GameObject> onExit;  
    public Action<GameObject> onUp;  
    public Action<GameObject> onSelect;  
    public Action<GameObject> onUpdateSelect;  
    public Action<GameObject> onDeSelect;  
    public Action<GameObject, PointerEventData> onDrag;  
    public Action<GameObject> onDragEnd;  
    public Action<GameObject> onDrop;  
    public Action<GameObject> onScroll;  
    public Action<GameObject> onMove;  
    
    public void OnPointerClick (PointerEventData eventData) {
        if (onClick != null) {
            onClick (gameObject);
        }
            
    }  
    public void OnPointerDown (PointerEventData eventData) {
        if (onDown != null) {
            onDown (gameObject);
        }
    }  
    public void OnPointerEnter (PointerEventData eventData) {
        if (onEnter != null) {
            onEnter (gameObject);
        }
    }  
    public void OnPointerExit (PointerEventData eventData) {
        if (onExit != null) {
            onExit (gameObject);
        }
    }  
    public void OnPointerUp (PointerEventData eventData) {
        if (onUp != null) {
            onUp (gameObject);
        }
    }  
    public void OnSelect (BaseEventData eventData) {
        if (onSelect != null) {
            onSelect (gameObject);
        }
    }  
    public void OnUpdateSelected (BaseEventData eventData) {
        if (onUpdateSelect != null) {
            onUpdateSelect (gameObject);
        }
    }  
    public void OnDeselect (BaseEventData eventData) {
        if (onDeSelect != null) {
            onDeSelect (gameObject);
        }
    }  
    public void OnDrag (PointerEventData eventData) {
        if (onDrag != null) {
            onDrag (gameObject, eventData);
        }
    }  
    public void OnEndDrag (PointerEventData eventData) {
        if (onDragEnd != null) {
            onDragEnd (gameObject);
        }
    }  
    public void OnDrop (PointerEventData eventData) {
        if (onDrop != null) {
            onDrop (gameObject);
        }
    }  
    public void OnScroll (PointerEventData eventData) {
        if (onScroll != null) {
            onScroll (gameObject);
        }
    }  
    public void OnMove (AxisEventData eventData) {
        if (onMove != null) {
            onMove (gameObject);
        }
    }  
    
    static public UIEventListener Get (GameObject go) {  
        UIEventListener listener = go.GetComponent<UIEventListener> ();  
        if (listener == null) {
            listener = go.AddComponent<UIEventListener> ();
        }  
        return listener;  
    }  
}  

 

[UGUI] 用脚本给UI绑定事件(对比手动绑定) 注:此文内容所使用Unity版本为5.1.1f1,在较早的版本中,代码可能略有不同,最后一行代码trigger.triggers.Add(…) 可能为 trigger.delegates.Add(…)一直觉得用脚本给UGUI绑定事件过程很繁琐,不易记。最近发现如果对比着手动绑定事件的过程,是非常容易记住的,因为两者的过程几乎是一致的,下面对比一下:一、先来看看手动绑定的过程: 阅读详情

相关推荐

【Unity3D UGUI】事件接口(零)总述

Unity3D UGUI 事件接口系列之总述。

Eazey丶亦泽 6480

Unity(二十八):事件、渗透事件、键盘事件

效果 事件 事件在Canvas的GraphicRaycaster组件启用时生效 1、2 的事件 Assets/Scripts/Event_01_02.csusing UnityEngine; using UnityEngine.UI; public class Event_01_02 : MonoBehaviour { public Image EventUI01; public Button EventUI02; private void Awake()

You瞧谁不起 - 道不尽世间的沧桑,诉不完人生的悲凉 1480

Unity中UGUI事件系统(一)

实现接口IPointerClickHandler(鼠标点击)、IPointerEnterHandler(鼠标进入)、IPointerExitHandler(鼠标移出)、IPointerDownHandler(鼠标按下)、IPointerUpHandler(鼠标松开)、UIEventClick脚本挂载到需要点击的UI上。1. 添加UIEventSelect脚本,该脚本实现了ISelectHandler(选定),IUpdateSelectedHandler(选中),IDeselectHandler(未选中)。

Umbella的博客 2260

UnityUI--UI事件

在Button(按钮)的课程中按钮的点击,其实就是事件。但是实际项目中,UI并不是只有点击这一种事件,比如:鼠标悬浮、鼠标按下、鼠标弹起,鼠标拖拽等等。而且UI本身可能并不是一个按钮,可能是图片或者什么其他的,因为按钮只能点击;如果要使事件做事,就要添加到对应的游戏物体上。

qq_73628117的博客 489

UGUI源码分析:EventSystem事件系统

系列 UGUI源码分析系列总览 文章目录系列EventSystemEventDataBaseInputModuleProcessMouseEventProcessTouchEventsRaycastersRaycasterManagerPhysics2DRaycaster PhysicsRaycasterRaycastResultEventSystemHandlerExecuteEvents Ev...

vin129的博客 9468

UGUI鼠标悬停事件

一开始我想用OnMouseOver方法,但是它只适用于GUI和非UI物体,对于UGUI无效。 方法1 在Update函数里用射线检测 具体见博客:https://www.cnblogs.com/yanghui0702/p/yanghui20181018.html 方法2 使用EventTrigger组件 我使用的unity版本为5.6,高版本不一定适用。 UGUI物体有个组件叫EventTrigg...

realfancy的博客 6364

[UnityUI]UGUI的事件系统

官方文档链接:http://docs.unity3d.com/460/Documentation/Manual/SupportedEvents.html Supported Events The Eventsystem supports a number of events, and they can be customised further in user custom user writ

宏哥的博客 1万+

UGUI的事件机制

UGUI使用了新的event机制,我们之前在NGUI中,为了处理ui的交互,通常是在ui组件挂载的mono上写onclick(),onpress()这些函数,换到ugui后,unity使用了新的event 机制。但是unity的文档解释的很乱,这里整理一下。

leon 5751

[Unity]UI事件(鼠标进入、离开、点击)

1、首先继承IPointerEnterHandler、IPointerExitHandler等接口。注意:这些只对UI有效,而OnMouseEnter只对加Collider的非UI有效。如:OnPointerEnter、OnPointerExit。

m0_70000383的博客 3313

Unity UGUI 按钮绑定事件的方式

Unity UGUI 按钮绑定事件的方式 第一种方式是手动的添加监听事件 step1:创建一个按钮 step1: 创建一个空物体 step3:给按钮写监听脚本 step4:将监听脚本放在空物体的身上 step5:在点击按钮下面的OnClick添加监听事件将空物体放在里面,然后选择我们的方法using System.Collections; using System.Collection

GZasplin的博客 2335

【unity游戏开发入门到精通——UGUI】UI事件监听接口

本文介绍了Unity UGUI事件接口的使用方法,主要包括常用事件接口(如点击、拖拽等)和导航相关接口的实现步骤。文章详细说明了如何通过继承接口、实现方法并挂载脚本来监听UI交互事件,并提供了完整的代码示例。同时解析了PointerEventData类中的关键参数,如pointerId、position、delta等,帮助开发者理解事件数据的传递机制。特别提醒移动端与PC端在事件处理上的差异,为开发者提供了跨平台UI交互的实用指导。

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 1552

UI自动化测试-第一个测试脚本

本文介绍了UI自动化测试入门的第一个测试脚本,并对脚本内容进行了详细的解析,看完此文,相信没有UI自动化基础的同学,也能够开始UI自动化测试用例的编写了!欢迎交流,拜拜。

seanyang_的博客 2056

告别裸机思维:在STM32F407上玩转LwIP,让你的设备轻松接入TCP网络

本文详细介绍了如何在STM32F407微控制器上使用LwIP协议栈实现TCP网络连接,从裸机编程到网络化的架构革命。通过事件驱动模型、内存优化配置和工业级稳定性实现,帮助开发者高效构建稳定TCP连接,适用于物联网设备联网等场景。

weixin_33725515的博客 562

UGUI UI事件

当前指针的位置,返回一个vector2向量,这个值是一个屏幕坐标,左下角为原点(0,0),右上角为(屏幕宽,屏幕高),这个屏幕是根据当前分辨率来的。当前触发OnPointerEnter的对象,一般鼠标移动到ui上的时候会触发这个事件,对应的是OnPointerExit,鼠标移出的时候。Debug.Log("你的鼠标离开了:" + name + " " + tag " + Time.time);UI 组件提供了大量的接口,因此可以在对象的脚本中继承并实现这些接口来扩展本身的功能,从而实现不同的事件。

qq_59714046的博客 1187

FPGA以太网接口设计避坑指南:GMII与RGMII时序转换的三种实现方案对比

本文深入解析FPGA以太网接口设计中GMII与RGMII时序转换的三种实现方案,包括Xilinx官方IP核、手工原语逻辑和开源方案gmii_to_rgmii。通过对比分析各方案的优缺点和适用场景,为工程师提供技术选型指南,帮助避免常见设计陷阱,提升高速网络设备开发的效率和可靠性。

weixin_42570484的博客 393

CSRT跟踪算法的使用

Fu Xianjun. All Rights Reserved. 文章目录 前言 一、CSRT跟踪算法是什么? 二、使用步骤 1.引入库 2.读入数据 总结 前言 随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。 提示:以下是本篇文章正文内容,下面案例可供参考 一、CSRT跟踪算法是什么? 在具有通道和空间可靠性的判别相关滤波器(DCF-CSR)中,我们使用空间可靠性图将滤波器支持调整为从帧中选择区.

sinat_53821602的博客 7906

Unity之UGUI详解

UGUI 文章目录UGUI六大基础组件概述Canvas对象上依附的:CanvasCanvas ScalerGraphic RaycasterRectTransformEventSystem对象上依附的:EventSystemStandalone Input ModuleCanvas画布组件Screen Space overlayScreen Space CameraWorld SpaceCanvasScaler画布缩放控制器组件学前准备:Constant Pixel Size恒定像素模式Scale With

BraveRunTo的博客 3万+

ardupilot 为什么要采样四元数姿态控制

本节主要说明清楚ardupilot姿态控制为什么要用到四元数,欢迎批评指正!!!

陌城烟雨 1119

UGUI 按钮监听事件

博主之前一直使用的是NGUI3.6.8版本,其中我们注册按钮事件,除了把物体拖到按钮的UIButton脚本的OnClick列表中并选择物体上挂载脚本的方法之外,还有动态的去添加方法。这样便能够在动态生成的按钮上添加监听事件。一般挂载脚本的是按钮的事件确定且有多个按钮。 UIEventListener.Get(button).onClick = OnClickAction;

黯小天的博客 3012

RK3568平台开发系列讲解(音频篇)配置声卡

📢本篇文章介绍如何配置声卡。

内核笔记 2450

VS2019编译配置Easy3D

VS2019编译配置Easy3D

dayuhaitang1的博客 2602

Unity中UI系统3——UGUI

UGUI六大基础组件、三大基础控件,在制作背包面板时的方便性...

SunsetSunup的博客 2815

数仓建模:维度表合并时,如何处理可能出现的数据冲突?

通过以上方法,可以有效地处理维度表合并时可能出现的数据冲突,确保合并后的数据完整性、一致性和准确性,满足业务需求和数据仓库的性能要求。在合并过程中,需要根据实际情况选择合适的处理方法,并进行充分的测试和验证。(2)实战中总结的SQL技巧,帮助SQLBOY在SQL语言上有质的飞越,无论你应对业务难题及面试都会游刃有余【如果您觉得本文还不错,对你有帮助,那么不妨可以关注一下我的数字化建设实践之路专栏,这里的内容会更精彩。技巧总结,让你认识不一样的数仓。,数据库会抛出异常,可根据异常处理机制解决。

石榴姐yyds 1134

Unity UGUI 给Image添加点击事件的三种方式

Button 在Image上面添加一个Button组件,将Button组件的过渡效果Transition设置为None,并挂上测试脚本组件(BtnOnClickTest.cs): BtnOnClickTest.cs using UnityEngine; using UnityEngine.UI; public class BtnOnClickTest : MonoBehaviour { private void Awake() { GetCom...

fightsyj的博客 1万+

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Unity UGUI性能优化: UI点击事件

UGUI的UI点击事件性能问题 UGUI的事件本质上就是发送射线,由于UI的操作有一些复杂的手势,所以UGUI帮我们又封装了一层。创建任意UI时都会自动创建EventSystem对象,并且绑定EventSystem.cs和StandaloneInputModule.cs 每个Canvas要想监听点击事件必须绑定GraphicRaycaster脚本 同时参与点击事件的Canvas越多效率也就越低了,...

linxinfa的专栏 2016

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

上一篇: 【安卓开发】【二】活动Activity
下一篇: 【unity】关于shader引用默认图片导致的依赖出错问题
HelloCLanguage
博客等级 码龄10年 10粉丝 11原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值