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事件了。
本文详细介绍了在NGUI中通过添加BoxCollider、设置UIRoot下Camera参数以及编写C#Script脚本来实现精灵Sprite的OnClick事件触发过程。通过实例演示,展示了如何在Inspector窗口配置精灵和脚本,确保事件响应得以正常工作。
2562

被折叠的 条评论
为什么被折叠?



