NGUI按钮控制乐曲的播放和停止

NGUI按钮控制乐曲的播放和停止

单个按钮的

控制脚本(“QieHuan”)和声源(AudioSource)加在NGUI按钮上,如图game视图
控制脚本+声源

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class QieHuan : MonoBehaviour
 {
      private bool isCanPlay=true;
      public void ClickBtn()
      {
            if (isCanPlay)
            {
                gameObject.GetComponent<UISprite>().spriteName = "播放";
                gameObject.GetComponent<UIButton>().normalSprite = "播放";//需要把UIButton脚本里normalSprite一块儿改了
                gameObject.GetComponent<AudioSource>().Play();
                isCanPlay1 = false;
            }
            else
            {
                gameObject.GetComponent<UISprite>().spriteName = "停止";
                gameObject.GetComponent<UIButton>().normalSprite = "停止";
                gameObject.GetComponent<AudioSource>().Stop();
                isCanPlay = true;
            }
      }
}

三个按钮的

在这里插入图片描述
在这里插入图片描述
需对按钮上的控制脚本进行修改

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class QieHuan : MonoBehaviour {
    public GameObject btn1;
    public GameObject btn2;
    public GameObject btn3;
    public static bool isCanPlay1=true;
    public static bool isCanPlay2=true;
    public static bool isCanPlay3 = true;
   // public bool l;
    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
    public void ClickBtn()
    {
       
        if (gameObject.name == "btn1")
        {
            btn2.GetComponent<UISprite>().spriteName = "停止";
            btn2.GetComponent<UIButton>().normalSprite = "停止";
            btn2.GetComponent<AudioSource>().Stop();
            isCanPlay2 = true;

            btn3.GetComponent<UISprite>().spriteName = "停止";
            btn3.GetComponent<UIButton>().normalSprite = "停止";
            btn3.GetComponent<AudioSource>().Stop();
            isCanPlay3 = true;

            if (isCanPlay1)
            {
                gameObject.GetComponent<UISprite>().spriteName = "播放";
                gameObject.GetComponent<UIButton>().normalSprite = "播放";
                gameObject.GetComponent<AudioSource>().Play();
                isCanPlay1 = false;
            }
            else
            {
                gameObject.GetComponent<UISprite>().spriteName = "停止";
                gameObject.GetComponent<UIButton>().normalSprite = "停止";
                gameObject.GetComponent<AudioSource>().Stop();
                isCanPlay1 = true;
            }
        }
        if (gameObject.name == "btn2")
        {
            btn3.GetComponent<UISprite>().spriteName = "停止";
            btn3.GetComponent<UIButton>().normalSprite = "停止";
            btn3.GetComponent<AudioSource>().Stop();
            isCanPlay3 = true;
            btn1.GetComponent<UISprite>().spriteName = "停止";
            btn1.GetComponent<UIButton>().normalSprite = "停止";
            btn1.GetComponent<AudioSource>().Stop();
            isCanPlay1 = true;
            //Debug.Log(isCanPlay1);

            if (isCanPlay2)
            {
                gameObject.GetComponent<UISprite>().spriteName = "播放";
                gameObject.GetComponent<UIButton>().normalSprite = "播放";
                gameObject.GetComponent<AudioSource>().Play();
                isCanPlay2= false;
            }
            else
            {
                gameObject.GetComponent<UISprite>().spriteName = "停止";
                gameObject.GetComponent<UIButton>().normalSprite = "停止";
                gameObject.GetComponent<AudioSource>().Stop();
                isCanPlay2 = true;
            }
        }
        if (gameObject.name == "btn3")
        {
            btn1.GetComponent<UISprite>().spriteName = "停止";
            btn1.GetComponent<UIButton>().normalSprite = "停止";
            btn1.GetComponent<AudioSource>().Stop();
            isCanPlay1 = true;
            btn2.GetComponent<UISprite>().spriteName = "停止";
            btn2.GetComponent<UIButton>().normalSprite = "停止";
            btn2.GetComponent<AudioSource>().Stop();
            isCanPlay2 = true;
            //Debug.Log(isCanPlay1);

            if (isCanPlay3)
            {
                gameObject.GetComponent<UISprite>().spriteName = "播放";
                gameObject.GetComponent<UIButton>().normalSprite = "播放";
                gameObject.GetComponent<AudioSource>().Play();
                isCanPlay3 = false;
            }
            else
            {
                gameObject.GetComponent<UISprite>().spriteName = "停止";
                gameObject.GetComponent<UIButton>().normalSprite = "停止";
                gameObject.GetComponent<AudioSource>().Stop();
                isCanPlay3= true;
            }
        }


    }
}

以下为实际项目中的截图和代码(用另一套逻辑,实现三个乐曲的切换,并伴随相应乐谱图片的切换)——代码仍然是加在每个Button上,因为还涉及到一首曲子中有两张乐谱图片情况,细节没有完全展开,仅做参考,请勿套用
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ControllAudio : MonoBehaviour {
    public bool isCanPlay=true;
    public GameObject Btn1;
    public GameObject Btn2;
    public GameObject Btn3;
    public GameObject yuePu1;
    public GameObject yuePu2_1;
    public GameObject yuePu2_2;
    public GameObject yuePu3;
    //public AudioSource CA1;
    //public AudioSource CA2;
    //public AudioSource CA3;
    //public UISprite S1;
    //public UISprite S2;
    //public UISprite S3;
    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
    public void OnClick()
    {
        if(gameObject.name == "Btn_Controll1")
        {
            Btn1.GetComponent<AudioSource>().enabled = true;
            Btn2.GetComponent<AudioSource>().enabled = false;
            Btn3.GetComponent<AudioSource>().enabled = false;

            Btn2.GetComponent<ControllAudio>().isCanPlay = true;
            Btn3.GetComponent<ControllAudio>().isCanPlay = true;

            yuePu1.SetActive(true);
            yuePu2_1.SetActive(false);
            yuePu2_2.SetActive(false);
            yuePu3.SetActive(false);
        }
        else if(gameObject.name == "Btn_Controll2")
        {
            Btn1.GetComponent<AudioSource>().enabled = false;
            Btn2.GetComponent<AudioSource>().enabled = true;
            Btn3.GetComponent<AudioSource>().enabled = false;

            Btn1.GetComponent<ControllAudio>().isCanPlay = true;
            Btn3.GetComponent<ControllAudio>().isCanPlay = true;

            yuePu1.SetActive(false);
            yuePu2_1.SetActive(true);
            StartCoroutine(DelayYuePu2_2Appear());
            yuePu3.SetActive(false);
        }
        else if(gameObject.name == "Btn_Controll3")
        {
            Btn1.GetComponent<AudioSource>().enabled = false;
            Btn2.GetComponent<AudioSource>().enabled = false;
            Btn3.GetComponent<AudioSource>().enabled = true;

            Btn1.GetComponent<ControllAudio>().isCanPlay = true;
            Btn2.GetComponent<ControllAudio>().isCanPlay = true;

            yuePu1.SetActive(false);
            yuePu2_1.SetActive(false);
            yuePu2_2.SetActive(false);
            yuePu3.SetActive(true);
        }
       
       // i++;
       // Debug.Log(i+gameObject.name);
        if(isCanPlay)
        {
            GetComponent<UISprite>().spriteName = "播放";
            GetComponent<AudioSource>().Play();
            if (gameObject.name == "Btn_Controll1")
            {

                Btn2.GetComponent<UISprite>().spriteName = "停止";
                Btn3.GetComponent<UISprite>().spriteName = "停止";
            }
            else if (gameObject.name == "Btn_Controll2")
            {
                Btn1.GetComponent<UISprite>().spriteName = "停止";
                Btn3.GetComponent<UISprite>().spriteName = "停止";
            }
            else if (gameObject.name == "Btn_Controll3")
            {
                Btn1.GetComponent<UISprite>().spriteName = "停止";
                Btn2.GetComponent<UISprite>().spriteName = "停止";
            }
            isCanPlay = false;
        }
        else
        {
            GetComponent<UISprite>().spriteName = "停止";
            GetComponent<AudioSource>().Stop();
            isCanPlay = true;

        }
       
        //Debug.Log("dainji");
    }
    IEnumerator DelayYuePu2_2Appear()
    {
        yield return new WaitForSeconds(19);
        yuePu2_1.SetActive(false);
        yuePu2_2.SetActive(true);
    }
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值