控制脚本(“QieHuan”)和声源(AudioSource)加在NGUI按钮上,如图
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class QieHuan : MonoBehaviour
{
private bool isCanPlay=true;
public void ClickBtn()
{
if (isCanPlay)
{
gameObject.GetComponent().spriteName = "播放";
gameObject.GetComponent().normalSprite = "播放";//需要把UIButton脚本里normalSprite一块儿改了
gameObject.GetComponent().Play();
isCanPlay1 = false;
}
else
{
gameObject.GetComponent().spriteName = "停止";
gameObject.GetComponent().normalSprite = "停止";
gameObject.GetComponent().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().spriteName = "停止";
btn2.GetComponent().normalSprite = "停止";
btn2.GetComponent().Stop();
isCanPlay2 = true;
btn3.GetComponent().spriteName = "停止";
btn3.GetComponent().normalSprite = "停止";
btn3.GetComponent().Stop();
isCanPlay3 = true;
if (isCanPlay1)
{
gameObject.GetComponent().spriteName = "播放";
gameObject.GetComponent().normalSprite = "播放";
gameObject.GetComponent().Play();
isCanPlay1 = false;
}
else
{
gameObject.GetComponent().spriteName = "停止";
gameObject.GetComponent().normalSprite = "停止";
gameObject.GetComponent().Stop();
isCanPlay1 = true;
}
}
if (gameObject.name == "btn2")
{
btn3.GetComponent().spriteName = "停止";
btn3.GetComponent().normalSprite = "停止";
btn3.GetComponent().Stop();
isCanPlay3 = true;
btn1.GetComponent().spriteName = "停止";
btn1.GetComponent().normalSprite = "停止";
btn1.GetComponent().Stop();
isCanPlay1 = true;
//Debug.Log(isCanPlay1);
if (isCanPlay2)
{
gameObject.GetComponent().spriteName = "播放";
gameObject.GetComponent().normalSprite = "播放";
gameObject.GetComponent().Play();
isCanPlay2= false;
}
else
{
gameObject.GetComponent().spriteName = "停止";
gameObject.GetComponent().normalSprite = "停止";
gameObject.GetComponent().Stop();
isCanPlay2 = true;
}
}
if (gameObject.name == "btn3")
{
btn1.GetComponent().spriteName = "停止";
btn1.GetComponent().normalSprite = "停止";
btn1.GetComponent().Stop();
isCanPlay1 = true;
btn2.GetComponent().spriteName = "停止";
btn2.GetComponent().normalSprite = "停止";
btn2.GetComponent().Stop();
isCanPlay2 = true;
//Debug.Log(isCanPlay1);
if (isCanPlay3)
{
gameObject.GetComponent().spriteName = "播放";
gameObject.GetComponent().normalSprite = "播放";
gameObject.GetComponent().Play();
isCanPlay3 = false;
}
else
{
gameObject.GetComponent().spriteName = "停止";
gameObject.GetComponent().normalSprite = "停止";
gameObject.GetComponent().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().enabled = true;
Btn2.GetComponent().enabled = false;
Btn3.GetComponent().enabled = false;
Btn2.GetComponent().isCanPlay = true;
Btn3.GetComponent().isCanPlay = true;
yuePu1.SetActive(true);
yuePu2_1.SetActive(false);
yuePu2_2.SetActive(false);
yuePu3.SetActive(false);
}
else if(gameObject.name == "Btn_Controll2")
{
Btn1.GetComponent().enabled = false;
Btn2.GetComponent().enabled = true;
Btn3.GetComponent().enabled = false;
Btn1.GetComponent().isCanPlay = true;
Btn3.GetComponent().isCanPlay = true;
yuePu1.SetActive(false);
yuePu2_1.SetActive(true);
StartCoroutine(DelayYuePu2_2Appear());
yuePu3.SetActive(false);
}
else if(gameObject.name == "Btn_Controll3")
{
Btn1.GetComponent().enabled = false;
Btn2.GetComponent().enabled = false;
Btn3.GetComponent().enabled = true;
Btn1.GetComponent().isCanPlay = true;
Btn2.GetComponent().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().spriteName = "播放";
GetComponent().Play();
if (gameObject.name == "Btn_Controll1")
{
Btn2.GetComponent().spriteName = "停止";
Btn3.GetComponent().spriteName = "停止";
}
else if (gameObject.name == "Btn_Controll2")
{
Btn1.GetComponent().spriteName = "停止";
Btn3.GetComponent().spriteName = "停止";
}
else if (gameObject.name == "Btn_Controll3")
{
Btn1.GetComponent().spriteName = "停止";
Btn2.GetComponent().spriteName = "停止";
}
isCanPlay = false;
}
else
{
GetComponent().spriteName = "停止";
GetComponent().Stop();
isCanPlay = true;
}
//Debug.Log("dainji");
}
IEnumerator DelayYuePu2_2Appear()
{
yield return new WaitForSeconds(19);
yuePu2_1.SetActive(false);
yuePu2_2.SetActive(true);
}
}