Unity,我在使用DontDestroyOnLoad时遇到问题,无法在不同场景中进行跟踪 [英] Unity, I have problem with DontDestroyOnLoad to keep tracking in different scence
问题描述
第一个脚本
public static KeepTheMusicOn Instance;
void Awake()
{
if (!Instance)
Instance = this;
else
Destroy(this.gameObject);
DontDestroyOnLoad(this.gameObject);
}
有了该脚本,我可以在第二个场景中继续播放音乐,而无需重新启动音乐,并且在主菜单中,我具有可以使音乐静音的按钮,该按钮将运行我的第二个脚本.
第二个脚本:
public AudioSource mainMusic;
public void Update()
{
DontDestroyOnLoad(mainMusic);
}
public void MusicOnOff()
{
if (mainMusic.isPlaying)
{
mainMusic.Pause();
}
else
{
mainMusic.UnPause();
}
}
我的问题是,当我开始游戏时,可以在主菜单场景中使用按钮使音乐静音,但是当我进入游戏场景并返回菜单时,该按钮不执行任何操作. 这就是我的问题,我希望任何人都可以帮助我.对不起,我的英语不好.
声音听起来像在切换场景时会破坏按钮.当它们返回主菜单时,您将销毁音频控制器的重复实例Thing =>在Button中配置的引用会丢失.
在您的情况下,因为无论如何您都使用公共Singleton,您也可以(滥用)使用它,并将组件放在Button
本身上(这样,引用就不会丢失),并执行例如
[RequireComponent(typeof(Button))]
public class MusicButton : MonoBehaviour
{
[SerializeField] private Button button;
private void Awake()
{
if(!button) button = GetComponemt<Button>();
// dynamically add the callback
// it won't appear in the editor but get called in onClick
button.onClick.AddListener(OnClicked);
}
private void OnClicked()
{
KeepTheMusicOn.Instance.MusicOnOff();
}
}
如果您希望在编辑器中看到它,当然也可以从Awake
滚动它,将OnClicked
公开,并在按钮的onClick
事件中手动引用它.
im new in unity and i have a problem I am making a game that have 2 scence(Main Menu Scence and Game Scence), i put my music on Main Menu scence. I make a empty game object and i attach audio source there(music) , and i also attach script like this :
First script
public static KeepTheMusicOn Instance;
void Awake()
{
if (!Instance)
Instance = this;
else
Destroy(this.gameObject);
DontDestroyOnLoad(this.gameObject);
}
With that script i can keep music play in second scence wihtout restart the music, and in the main menu scence i have settings that have button to mute the music , the button will run my second script .
Second Script:
public AudioSource mainMusic;
public void Update()
{
DontDestroyOnLoad(mainMusic);
}
public void MusicOnOff()
{
if (mainMusic.isPlaying)
{
mainMusic.Pause();
}
else
{
mainMusic.UnPause();
}
}
My problem is when i start the game so im in my main menu scence i can mute the music with the button, but when i go to game scence and i back to menu, the button dont do anything. So that is my problem, i hope anyone can help me. Sorry for my bad english.
Sounds like when switching scenes you destroy the button. When you them go back to the main menu you destroy the duplicate instance of your audio controller thing => references configured in the Button are lost.
In your case since you use a public Singleton anyway you could as well (ab)use it and put a component on the Button
itself instead (thus the reference can not get lost) and do something like e.g.
[RequireComponent(typeof(Button))]
public class MusicButton : MonoBehaviour
{
[SerializeField] private Button button;
private void Awake()
{
if(!button) button = GetComponemt<Button>();
// dynamically add the callback
// it won't appear in the editor but get called in onClick
button.onClick.AddListener(OnClicked);
}
private void OnClicked()
{
KeepTheMusicOn.Instance.MusicOnOff();
}
}
If you prefer seeing it in the editor you can ofcourse as well rove it from Awake
, make the OnClicked
public and reference it in the button's onClick
event manually.
这篇关于Unity,我在使用DontDestroyOnLoad时遇到问题,无法在不同场景中进行跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!