Unity插件之NGUI学习(6)—— 关于Widget如何添加触发事件

时间:2014-10-10 17:18:44   收藏:0   阅读:398

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。

bubuko.com,布布扣

然后添加C# Script脚本,

using UnityEngine;
using System.Collections;


public class SpriteClickTest : MonoBehaviour {


private UISpriteAnimation spriteAnimation; 


void Start()
{
spriteAnimation = GetComponent<UISpriteAnimation>();
}


void OnClick() 
{
if (spriteAnimation.isPlaying) {
// 暂停动画
spriteAnimation.Stop();
} else {
// 动画重新播放
spriteAnimation.Reset();
}
}
}

在Inspector窗口

bubuko.com,布布扣


点击运行游戏,刚才设置在精灵的脚本,就会相应OnClick事件了。


评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!