NGUI使用图集的精灵换图片

本文介绍了一种在Unity中使用UIButton实现UI精灵图片切换的方法。通过编写简单的C#脚本,当点击UIButton时,可以实现在两张图片之间的切换。这种方法适用于简单的游戏或应用交互设计。

    创建了一个sprite,选择的是某一个图集下的sprite,现在我想点击它来换图片。前提是你的sprite已经加了UIButton

 代码如下:   

 1     public void OnClick()
 2     {
 3         if (this.GetComponent<UIButton>().normalSprite == "picture1")
 4         {
 5             this.GetComponent<UISprite>().spriteName = "picture2";
 6             this.GetComponent<UIButton>().normalSprite = "picture2";
 7             return;
 8         }
 9         if (this.GetComponent<UIButton>().normalSprite == "picture2")
10         {
11             this.GetComponent<UISprite>().spriteName = "picture1";
12             this.GetComponent<UIButton>().normalSprite = "picture1";
13             return;
14         }     
15     }

   这个可以做对话,但是不推荐,比如你的对话有五十个,那你要用五十张图片。。。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值