Unity插件之NGUI学习(6)—— 关于Widget怎样加入触发事件(触发OnClick)

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 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窗体



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


posted @ 2015-01-23 18:04  mfrbuaa  阅读(423)  评论(0)    收藏  举报