首先,您需要保存某個已完成關卡的地方。這些信息必須以持續的方式保存,否則,每次啓動遊戲時,您的玩家都必須重新啓動整個遊戲。有很多方法可以這樣做,但PlayerPrefs可能是一個起點。
一旦任何級別完成(加載下一場景之前),請撥打以下功能:
public void OnLevelCompleted()
{
// Retrieve name of current scene/level
string sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
PlayerPrefs.SetInt(sceneName, 1) ; // Indicates the level is completed
}
然後,在你家裏的場景,附加一個腳本到你的按鈕與下面的代碼段:
public string SceneName ; // Indicate which level this button must load once you click on it. Be carefull, the name must be the same as in your Build Settings
protected void Awake()
{
UnityEngine.UI.Button button = GetComponent<UnityEngine.UI.Button>();
if(button != null)
{
// Make the button load the given scene
button.onClick.AddListener(() => UnityEngine.SceneManagement.SceneManager.LoadScene(SceneName)) ;
// Make the button interactable only if the given scene/level has been completed
button.interactable = PlayerPrefs.GetInt(SceneName) > 0 ;
}
else
Debug.LogWarning("No button component attached", this) ;
}