写了一晚上作业,里面一个脚本挂到了空物体上再去添加onclik事件,这个脚本呢,有两个public audio clip。由于还有其他函数,它还被挂到了canvas上。
题外话:保存一下音频播放切换停止静音的代码先`
AudioSource audioctrl;
GameObject textobj;
bool isplay = false;
bool isStop = false;
bool isMute = false;
GameObject Button;
bool i=false;
public AudioClip[] audios=new AudioClip[2];
// Start is called before the first frame update
void Start()
{
audioctrl = GameObject.Find("Canvas").GetComponent<AudioSource>();
textobj = GameObject.Find("openplay");
Button= GameObject.Find("open");
GameObject.Find("Canvas").GetComponent<AudioSource>().clip = audios[0];
Debug.Log(audioctrl.mute);
}
// Update is called once per frame
void Update()
{
}
public void anothersongplay()
{
if (!i) i = true;
else i = false;
if (i)
audioctrl.clip = audios[1];
else
audioctrl.clip = audios[0];
GameObject.Find("openplay").GetComponent<Text>().text = "播放";
GameObject.Find("stopplay").GetComponent<Text>().text = "暂停";
bool isplay = false;
bool isStop = false;
bool isMute = false;
}
public void openmusic()
{
Button = GameObject.Find("open");
textobj = Button.transform.Find("openplay").gameObject;
if (!isplay)
{
audioctrl.Play();
textobj.GetComponent<Text>().text = "正在播放";
}
else
{
audioctrl.Stop();
textobj.GetComponent<Text>().text = "播放";
}
isplay = !isplay;
}
public void stopmusic()
{
Button = GameObject.Find("stop");
textobj = Button.transform.Find("stopplay").gameObject;
if (isplay)
{
if (!isStop)
{
audioctrl.Pause();
textobj.GetComponent<Text>().text = "继续";
}
else
{
audioctrl.Play();
textobj.GetComponent<Text>().text = "暂停";
}
isStop = !isStop;
}
}
public void musicmute()
{
Button = GameObject.Find("mute");
textobj = Button.transform.Find("muteplay").gameObject;
if (!isMute)
{
audioctrl.mute=true;
GameObject.Find("stop").GetComponent<Button>().interactable = false;
GameObject.Find("open").GetComponent<Button>().interactable = false;
textobj.GetComponent<Text>().text = "恢复正常";
}
else
{
audioctrl.mute=false;
GameObject.Find("stop").GetComponent<Button>().interactable = true;
GameObject.Find("open").GetComponent<Button>().interactable = true;
textobj.GetComponent<Text>().text = "静音";
}
isMute = !isMute;
}
}
于是喜闻乐见的我往canvas上的script疯狂添加音频,根本没想起来空物体上的脚本函数才是被调用的那个。简单来说,以为多挂载还是引用同一个类而已。但不是,至少public不是,得记住派生关系。
还是个不懂类是啥的菜鸟啊。