Unity UGUI暂停按钮切换图片代码
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 6 public class PauseContorl : MonoBehaviour 7 { 8 bool isGamePause; 9 //游戏暂停按钮 10 private GameObject pauseButtonObj; 11 //当前暂停按钮的图片 12 private Image currentPauseImage; 13 //暂停按钮Sprite 14 private Sprite pauseSprite; 15 //开始按钮Sprite 16 private Sprite startSprite; 17 18 private void Start() 19 { 20 pauseButtonObj = GameObject.Find("PauseButton"); 21 pauseButtonObj.GetComponent<Button>().onClick.AddListener 22 ( 23 delegate () { SetPause(); } 24 ); 25 26 currentPauseImage = pauseButtonObj.GetComponent<Image>(); 27 28 pauseSprite = Resources.Load("Images/pause/pause", typeof(Sprite)) as Sprite; 29 startSprite = Resources.Load("Images/pause/start", typeof(Sprite)) as Sprite; 30 } 31 32 //设置游戏是否暂停 33 void SetPause() 34 { 35 if (isGamePause) 36 { 37 isGamePause = false; 38 39 currentPauseImage.sprite = pauseSprite; 40 } 41 else 42 { 43 isGamePause = true; 44 45 currentPauseImage.sprite = startSprite; 46 } 47 } 48 }