zoukankan      html  css  js  c++  java
  • 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事件了。


  • 相关阅读:
    mysql常用命令
    CSS样式
    定位
    background
    文本属性和字体属性
    超链接导航案例
    margin塌陷
    浮动
    GIT 修改提交地址
    VUE ElementUI 表格多选框实现单选
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4244808.html
Copyright © 2011-2022 走看看