[Unity-12] 切換Button的圖片

我們來做一個按鈕的圖片切換。

功能,就像播放器的“開始”和“暫停”。


編寫TestButton.cs腳本:

[csharp] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class TestButton : MonoBehaviour {  
  5.     protected bool switchFlag = true;  
  6.   
  7.     // Use this for initialization  
  8.     void Start () {  
  9.       
  10.     }  
  11.   
  12.     void OnClick () {  
  13.         if (this.switchFlag) {  
  14.             this.GetComponent<UISprite> ().spriteName = "pause";  
  15.             this.GetComponent<UIButton> ().normalSprite = "pause";  
  16.             this.switchFlag = false;  
  17.         }  
  18.         else{  
  19.             this.GetComponent<UISprite>().spriteName = "start";  
  20.             this.GetComponent<UIButton>().normalSprite = "start";  
  21.             this.switchFlag = true;  
  22.         }  
  23.     }  
  24. }  

將腳本掛在simple button上


然後,運行:運行效果如下:

點擊後,出現暫停按鈕。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章