2017-06-24 29 views
-1

最近我已經進入Unity並開始製作我的第一款遊戲。我真的很高興,但最近我一直非常惱火。它不會讓我把我的按鈕從其他腳本進行交互

我已經在我的遊戲中製作了一個關卡系統(一旦你完成第一個關卡(所有關卡都是單獨的場景),你將進入下一關)。我已經找到了如何使用application.loadlevel等。但是我也想通過選擇等級菜單(您可以通過點擊一個按鈕來選擇過去的關卡或當前關卡)以便在上關閉難題up立方體。不幸的是,我不知道該怎麼做,因爲我所有的腳本都失敗了。

請提前幫助我,謝謝,我是初學者,所以不要解釋太先進的東西。告訴我該怎麼做,以及我需要在腳本中寫什麼。如果我必須使用預製件,請告訴我如何讓我感到困惑。

回答

1

首先,您需要保存某個已完成關卡的地方。這些信息必須以持續的方式保存,否則,每次啓動遊戲時,您的玩家都必須重新啓動整個遊戲。有很多方法可以這樣做,但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) ; 
} 
相關問題