0
我有用戶界面按鈕來切換聲音。 OnClick事件鏈接到這個單例遊戲對象。當我移動到下一個場景並返回到主場景時,我發現OnClick對象在對象仍然存在於層次結構中時丟失了!所以有什麼問題 ?用戶界面按鈕停止工作後,我加載另一場景
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SoundsManagerController : MonoBehaviour {
static SoundsManagerController Instance = null;
void Awake()
{
// First we check if there are any other instances conflicting
if (Instance != null)
{
// If that is the case, we destroy other instances
Destroy(gameObject);
}
else {
// Here we save our singleton instance
Instance = this;
// Furthermore we make sure that we don't destroy between scenes (this is optional)
DontDestroyOnLoad(gameObject);
}
}
public void toggleSound(){
Instance.GetComponent<AudioSource>().enabled = !Instance.GetComponent< AudioSource>().enabled;
}
}
感謝您的幫助..但是你可以提供更好的方法鏈接或例子,因爲我不喜歡有多個畫布,讓他們的孩子的單身對象 – dotfreelancer
凡在UI按鈕放置切換? –
在畫布下 – dotfreelancer