unity image可以调用unity3d onclickk吗

温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
&先以PointerClick为例。这个是用于某点点击事件。其他事件都可以根据相同的办法调用。
&&&&之所以使用PointerClick为例。是因为在最后笔者会提到一个特殊的实现方式。而相比于其他事件类型,有且仅有Click事件存在特殊实现。
&&&&我们要实现事件主要有3种方式:&&&&方式一:继承基础接口实现
&&&&步骤一:创建ClickObject脚本。继承MonoBehaviour和IPointerClickHandler。
&&&&步骤二:实现public void OnPointerClick(PointerEventData eventData)方法:
&&&&步骤三:创建一个名为Panel_IPointer的空对象。并且将ClickObject脚本附加到对象上。
&&&&步骤四:启动,并点击Panel_IPointer对象。在Console输出如下:
&&&&&方式二:Unity3D编辑器操作设置实现
步骤一:创建一个C#脚本。在脚本中添加一个public方法。
步骤二:创建一个命名为Empty的UI对象,用于接收和响应点击事件。创建一个名为Panel的UI对象,用于触发点击事件。
步骤三:Panel对象添加EventTrigger组件," Add New" -& 选择" PointerClick"。将Empty对象拖拽到触发者位置。然后点击"No Function"选择我们写在Test脚本中的OnTestClick事件。
&&&&步骤四:设置好这些之后。我们的事件触发就已经完成了。运行Unity3D。点击窗口中Panel对象。Console输出内容如下:
&&&&方式三:程序动态设置实现
&&&&我们在日常的开发中。可能需要动态的需要变更绑定的事件。那么我们如何才能使用C#代码控制绑定触发事件呢?
&&&&下面我们就介绍代码控制。ScriptControl.cs脚本
& using System.Collections.Gusing UnityEusing UnityEngine.Eusing UnityEngine.EventSpublic class ScriptControl : MonoBehaviour {&&& // Use this for initialization&&& void Start ()&&& {&&&&&&& var trigger = transform.gameObject.GetComponent&EventTrigger&();&&&&&&& if (trigger == null)&&&&&&&&&&& trigger = transform.gameObject.AddComponent&EventTrigger&();&&&&&&& // 实例化delegates&&&&&&& trigger.delegates = new List&EventTrigger.Entry&();&&&&&&& // 定义需要绑定的事件类型。并设置回调函数&&&&&&& EventTrigger.Entry entry = new EventTrigger.Entry();&&&&&&& // 设置 事件类型&&&&&&& entry.eventID = EventTriggerType.PointerC&&&&&&& // 设置回调函数&&&&&&& entry.callback = new EventTrigger.TriggerEvent();&&&&&&& UnityAction&BaseEventData& callback = new UnityAction&BaseEventData&(OnScriptControll);&&&&&&& entry.callback.AddListener(callback);&&&&&&& // 添加事件触发记录到GameObject的事件触发组件&&&&&&& trigger.delegates.Add(entry);&&& }&&& &&& // Update is called once per frame&&& void Update () {&&& &&& }&&& public void OnScriptControll(BaseEventData arg0)&&& {&&&&&&& Debug.Log("Test Click");&&& }}点击事件的特殊实现方式:使用Button控件实现 &&&&&&&&针对Click事件还存在一种特殊方式:uGUI系统中官方提供了一种Button控件。Button封装了官方提供的一套OnClick事件。操作完全类似于方式二。便不详述了。&&&&&&&&使用Button我们可以实现动态的变更鼠标绑定的点击事件。如下代码所示: using UnityEusing System.Cusing UnityEngine.UI;public class BtnControl : MonoBehaviour {&&& // Use this for initialization&&& void Start ()&&& {&&&&&&& var button = transform.gameObject.GetComponent&Button&();&&&&&&& if (button != null)&&&&&&& {&&&&&&&&&&& button.onClick.RemoveAllListeners();&&&&&&&&&&& button.onClick.AddListener(TestClick);&&&&&&& }&&& }&&& public void TestClick()&&& {&&&&&&& Debug.Log("Test Click. This is Type 4");&&& }&&& &&& // Update is called once per frame&&& void Update () {&&&&&&& &&& }}
阅读(1287)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'Unity3D中uGUI事件系统简述及使用方法总结',
blogAbstract:'\r\n'
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}UGUI点击事件(多个) - 博客频道 - CSDN.NET
大魔王 的博客
分类:unity3d UGUI
UnityEngine;
System.Collections;
UnityEngine.EventSystems;
EventTriggerListener
UnityEngine.EventSystems.EventTrigger{
VoidDelegate
(GameObject
VoidDelegate
VoidDelegate
VoidDelegate
VoidDelegate
VoidDelegate
VoidDelegate
VoidDelegate
onUpdateSelect;
EventTriggerListener
(GameObject
EventTriggerListener
go.GetComponent&EventTriggerListener&();
go.AddComponent&EventTriggerListener&();
OnPointerClick(PointerEventData
eventData)
if(onClick
onClick(gameObject);
OnPointerDown
(PointerEventData
eventData){
onDown(gameObject);
OnPointerEnter
(PointerEventData
eventData){
if(onEnter
onEnter(gameObject);
OnPointerExit
(PointerEventData
eventData){
onExit(gameObject);
OnPointerUp
(PointerEventData
eventData){
onUp(gameObject);
(BaseEventData
eventData){
if(onSelect
onSelect(gameObject);
OnUpdateSelected
(BaseEventData
eventData){
if(onUpdateSelect
onUpdateSelect(gameObject);
然后在你的界面里面写入监听按钮的代码。
UnityEngine;
System.Collections;
UnityEngine.UI;
UnityEngine.EventSystems;
UnityEngine.Events;
MonoBehaviour
transform.Find(&Button&).GetComponent&Button&();
transform.Find(&Image&).GetComponent&Image&();
EventTriggerListener.Get(button.gameObject).onClick
=OnButtonClick;
EventTriggerListener.Get(image.gameObject).onClick
=OnButtonClick;
OnButtonClick(GameObject
//在这里监听按钮的点击事件
button.gameObject){
(&DoSomeThings&);
适用于直接获取场景中已经创建好的ui_button
如果是预制体,通过Instantiate()创建的话,如下即可
GameObject&gameObject&=&Instantiate&(PrefabObejct,&Vector3.zero,&Quaternion.identity)&as&GameObject;
gameObject.AddComponent&Button&&().transition&=&Selectable.Transition.None;
gameObject.GetComponent&Button&&().onClick.AddListener&(delegate&{
&&&&&&&&&&&&&&&&print(gameObject.name);
&&&&&&&&&&&&});
排名:千里之外
(5)(6)(4)(7)(2)(12)(5)(1)(2)(1)(4)(1)(1)(1)(1)(8)(7)(1)(3)(1)(3)(1)1484人阅读
unity(9)
NGUI中,Button本身就带有OnClick事件,但是Sprite,Label等( 也绑有Widget的)并没有触发事件,其实NGUI的事件触发都必须添加Box Collider,并勾选Is Trigger,在Inspector窗口设置Box大小尺寸,可以在Widget的Collider勾选auto-adjust to match。还有一个比较重要的参数需要设置正确,即是UI Root下Camera参数,在Inspector窗口中,要确定UICamera中的Event Type选择3D UI,Event
Mask选择Everything。
然后添加C# Script脚本,
using UnityE
using System.C
public class SpriteClickTest : MonoBehaviour {
private UISpriteAnimation spriteA&
void Start()
spriteAnimation = GetComponent&UISpriteAnimation&();
void OnClick()&
if (spriteAnimation.isPlaying) {
// 暂停动画
spriteAnimation.Stop();
// 动画重新播放
spriteAnimation.Reset();
在Inspector窗口
点击运行游戏,刚才设置在精灵的脚本,就会相应OnClick事件了。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:48671次
排名:千里之外
原创:27篇
评论:10条
(1)(2)(1)(1)(5)(10)(1)(1)(3)(1)(1)(2)(1)(1)(3)在Unity3D中加载外部图片的两种方法
在Unity3D中加载外部图片的两种方法
各位朋友大家好,我是秦元培,欢迎大家关注我的博客。最近在做项目的过程中遇到这样的一个需求:玩家可以在游戏过程中进行实时存档,在存档过程中会保存当前游戏进度,同时会截取当前游戏画面并加载到游戏存档界面中。当下一次进入游戏的时候,将读取本地存档图片并加载到游戏界面中。这在单机游戏中是特别常见的一种功能,这里主要有两个关键点。首先是截取游戏画面,这个问题大家可以在这篇文章中找到答案。其次是从本地加载图片,因为这里要保证可读可写,因此传统的Resources.Load()方式和AssetBundle方式均无法实现这样的功能。那么怎样从外部加载图片到游戏中,这就是我们今天要讨论的内容啦。好了,这里介绍两种方法来实现这一目的。
喜闻乐见的WWW方式
喜闻乐见的WWW方式之所以喜闻乐见,这是因为这是我们最为熟悉的一种,我们都知道通过WWW可以从网络上加载文本、图片、音频等形式的内容,那么通过WWW能否加载本地外部(相对于应用程序)资源呢?答案是肯定的,这是因为WWW可以支持http和file两种协议。我们通常接触到的WWW默认都是指http协议,现在我们来说说file协议,该协议可以用来访问本地资源()。例如我们希望加载文件D:\TestFile\pic001.png这个文件,则此时对应的C#脚本为:
[AppleScript]
纯文本查看 复制代码
WWW www = new
WWW(&file://D:\\TestFile\\pic001.png);
yield return
null && string.IsNullOrEmpty(www.error))
&&&&//获取Texture
&&&&Texture texture=www.&&
&&&&//更多操作...&&&&&&
注意到这里出现了yield return结构,这表示这里使用到了协程,因此我们需要付出的代价就是需要在项目中使用StartCoroutine等协程相关的方法来调用这些协程。虽然在中使用协程是件简单的事情,可是如果我们随随便便地使用协程而不注意去维护这些协程,那么这些让我们引以为傲的简单代码可能就会变成我们痛苦不堪的无尽深渊。
亘古不变的传统IO方式
好了,下面我们隆重推出亘古不变的传统IO方式,这种方式相信大家都没有接触过,所以这里将这种方法和大家分享。既然是传统的IO方式,那么无非就是各种IO流的处理啦。好,我们一起来看下面这段代码:
[AppleScript]
纯文本查看 复制代码
//创建文件读取流
FileStream fileStream =
new FileStream(screen,
FileMode.Open, FileAccess.Read);
fileStream.Seek(0,
SeekOrigin.Begin);
//创建文件长度缓冲区
byte[] bytes =
new byte[fileStream.Length];
//读取文件
fileStream.Read(bytes,
0, (int)fileStream.Length);
//释放文件读取流
fileStream.Close();
fileStream.Dispose();
fileStream =
//创建Texture
int width=800;
int height=640;
Texture2D texture
= new Texture2D(width,
texture.LoadImage(bytes);
可以看到在使用这种方式读取图片文件的时候主要是将图片文件转化为byte[]数组,再利用Texture2D的LoadImage方法转化为中的Texture2D。这种方法需要在创建过程中传入图片的大小,在这里我们创建了一张800X640的图片。经过博主的研究发现,这种方式加载外部图片相对于使用WWW加载外部图片效率更高,所以如果大家遇到类似的需求,博主个人推荐大家使用这种方式进行加载。
到目前为止我们解决了如何从外部加载图片到Unity3D中,现在我们回到最开始的问题,我们从外部读取到这些图片以后需要将它们加载到游戏界面中。比如当我们使用UGUI的时候,UGUI中的Image控件需要一个来作为它的填充内容,那么此时我们就需要将Texture转化为Sprite.号了,下面我们给出一个简单的例子:
[AppleScript]
纯文本查看 复制代码
using UnityE
using System.C
using UnityEngine.UI;
using System.IO;
public class
TestLoading : MonoBehaviour
&/summary&
&&&&private Image
&&&&void Start
&&&&&&&&image
= this.transform.Find(&Image&).GetComponent&Image&();
&&&&&&&&//为不同的按钮绑定不同的事件
&&&&&&&&this.transform.Find(&LoadByWWW&).GetComponent&Button&().onClick.AddListener
&&&&&&&&&&&delegate(){LoadByWWW();}
&&&&&&&&);
&&&&&&&&this.transform.Find(&LoadByIO&).GetComponent&Button&().onClick.AddListener
&&&&&&&&&&delegate(){LoadByIO();}
&&&&&&&&);
以IO方式进行加载
&/summary&
&&&&private void LoadByIO()
&&&&&&&&double startTime
= (double)Time.
&&&&&&&&//创建文件读取流
&&&&&&&&FileStream fileStream
= new FileStream(&D:\\test.jpg&,
FileMode.Open, FileAccess.Read);
&&&&&&&&fileStream.Seek(0,
SeekOrigin.Begin);
&&&&&&&&//创建文件长度缓冲区
&&&&&&&&byte[] bytes
= new byte[fileStream.Length];
&&&&&&&&//读取文件
&&&&&&&&fileStream.Read(bytes,
0, (int)fileStream.Length);
&&&&&&&&//释放文件读取流
&&&&&&&&fileStream.Close();
&&&&&&&&fileStream.Dispose();
&&&&&&&&fileStream
&&&&&&&&//创建Texture
&&&&&&&&int width
&&&&&&&&int height
&&&&&&&&Texture2D texture
= new Texture2D(width,
&&&&&&&&texture.LoadImage(bytes);
&&&&&&&&//创建Sprite
&&&&&&&&Sprite sprite
= Sprite.Create(texture,
new Rect(0,
0, texture.width, texture.height),
new Vector2(0.5f,
&&&&&&&&image.sprite
&&&&&&&&startTime=(double)Time.time-startT
&&&&&&&&Debug.Log(&IO加载用时:&
+ startTime);
以WWW方式进行加载
&/summary&
&&&&private void LoadByWWW()
&&&&&&&&StartCoroutine(Load());
&&&&IEnumerator Load()
&&&&&&&&double startTime
= (double)Time.
&&&&&&&&//请求WWW
&&&&&&&&WWW www
= new WWW(&file://D:\\test.jpg&);
&&&&&&&&yield return
&&&&&&&&if(www !=
null && string.IsNullOrEmpty(www.error))
&&&&&&&&&&&&//获取Texture
&&&&&&&&&&&&Texture2D texture=www.
&&&&&&&&&&&&//创建Sprite
&&&&&&&&&&&&Sprite sprite
= Sprite.Create(texture,
new Rect(0,
0, texture.width, texture.height),
new Vector2(0.5f,
&&&&&&&&&&&&image.sprite
&&&&&&&&&&&&startTime
= (double)Time.time
&&&&&&&&&&&&Debug.Log(&WWW加载用时:&
+ startTime);
现在我们运行程序可以发现两种方式均可以让图片加载进来,为了对比两种方式在执行效率上的高低,我们在脚本中加入了相关代码,通过对比可以发现使用IO方式加载一张227k的图片需要的时间为0s,而使用WWW方式加载需要0.0185s,因此传统的IO方式具有更高的效率,建议大家在遇到这类问题时尽可能地使用这种方式。好了,今天的内容就是这样啦。
我的热门文章
即使是一小步也想与你分享

我要回帖

更多关于 image onclick事件 的文章

 

随机推荐