在做项目时,比如声音开关我们涉及到按钮的图标切换。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using RenderHeads.Media.AVProVideo; public class SoundButton : MonoBehaviour { /// <summary> /// 要设置的图集 /// </summary> public Sprite Mysprit; /// <summary> /// 默认的图集(自动获取按钮默认) /// </summary> private Sprite Defallsprit; /// <summary> /// 更改图片开关 /// </summary> private bool ischange = false; public MediaPlayer PlayingPlayer; // Use this for initialization void Start () { ///监听点击事件 transform.GetComponent<Button>().onClick.AddListener(OnClick); ///获取按钮初始默认图片 Defallsprit = transform.GetComponent<Image>().sprite; } // Update is called once per frame void Update () { } /// <summary> /// 按钮点击后所执行方法 /// </summary> void OnClick() { //测试信息是否点击执行了 Debug.Log("click"); ischange = !ischange; if (ischange) { ///更改按钮图片 transform.GetComponent<Image>().sprite = Mysprit; PlayingPlayer.Control.SetVolume(0); } else { ///还原按钮图片 transform.GetComponent<Image>().sprite = Defallsprit; PlayingPlayer.Control.SetVolume(1); } }