当 GameObject 要访问其它对象上的组件时,有下面两种方法
方法一(推荐)
public class MainLogic : MonoBehaviour
{
public AudioSource bgm;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (bgm.isPlaying)
{
bgm.Pause();
}
else
{
bgm.Play();
}
}
}
}
方法二
public class MainLogic : MonoBehaviour
{
public GameObject bgmNode;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (bgmNode.GetComponent<AudioSource>().isPlaying)
{
bgmNode.GetComponent<AudioSource>().Pause();
}
else
{
bgmNode.GetComponent<AudioSource>().Play();
}
}
}
}
标签:bgmNode,别的,frame,Update,Start,void,引用,组件,public
From: https://www.cnblogs.com/duixue/p/18303930