1. 程式人生 > >NGUI 3.5教程(五)按鈕-圖片的切換

NGUI 3.5教程(五)按鈕-圖片的切換

我們接著上一個例子,做一個按鈕的圖片切換。

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

編寫TestButton.cs指令碼:

using UnityEngine;
using System.Collections;

public class TestButton : MonoBehaviour {
	protected bool switchFlag = true;

	// Use this for initialization
	void Start () {
	
	}

	void OnClick () {
		if (this.switchFlag) {
			this.GetComponent<UISprite> ().spriteName = "pause";
			this.GetComponent<UIButton> ().normalSprite = "pause";
			this.switchFlag = false;
		}
		else{
			this.GetComponent<UISprite>().spriteName = "start";
			this.GetComponent<UIButton>().normalSprite = "start";
			this.switchFlag = true;
		}
	}
}

將指令碼掛在simple button上

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

點選後,出現暫停按鈕。