【问题标题】:How do I load previous scene on unity3d如何在unity3d上加载以前的场景
【发布时间】:2016-09-07 19:36:13
【问题描述】:

我在构建设置中总共有五个级别。我需要加载。当玩家死亡时。场景切换以重新启动场景。我有重启场景代码。所以它只会重新启动当前场景。我不希望它那样做。我需要重新加载以前的 .我需要对其他级别做同样的事情。这是我的代码:

{



#pragma strict

import UnityEngine.SceneManagement;

@script AddComponentMenu ("EGUI/UI_Elements/Button")

// public class EGUI_Button extends EGUI_Element {}

// List of built-in functionality types
public enum ButtonAction 
{
    None,                   // Do nothing
    Custom,                 // Call function (name in callFunction) with parameter for actionRecipient object (this gameObject is default)
    LoadLevel,              // Load level with index/name in parameter
    RestartLevel,           // Restart current level
    ExitGame,               // Close application
    SetQuality,             // Set quality level according to parameter (Fastest, Fast, ... Fantastic)
    DecQuality,             // Decrease quality level 
    IncQuality,             // Increase quality level  
    SetResolution,          // Set screen resolution according to parameter (1024x768, 1920x1080 ... etc)
    OpenURL,                // Open URL specified in parameter
    CloseEverything,        // Close/disable whole GUI manager and all related GUI-elements. 
    Resume,                 // Close parent GUI-element and set time-scale to 1
    ShowAnother,            // Show GUI-element specified in actionRecipient
    ShowPrevious,           // Show previous GUI-element
    HideThis,               // Hide parent GUI-element  
    HideThis_ShowAnother,   // Hide parent GUI-element and show window specified in actionRecipient
    HideThis_ShowPrevious,  // Hide parent GUI-element and show previous window
    SoundSwitch,            // Enable/Disable all sounds in the scene
    LoadPreviousLevel,      // Load the previous level if there is one else load load the current one
};

var onClickAction: ButtonAction;    // Action preset to perform onClick
var actionRecipient: GameObject;    // Optional link to action recipient object
var callFunction: String;           // Optional name of custom function to call
var parameter: String;              // Optional parameter to send/use in the Action

//=====================================================================================================
// Overload parent OnClick function to Perform built-in actions
function OnClick () 
{
//     super.OnClick();
    PerformAction ();
}

//----------------------------------------------------------------------------------------------------
// Perform built-in actions according to selected type (onClickAction)
function PerformAction () 
{
    switch (onClickAction)
    {
        case ButtonAction.None:
            break;
        case ButtonAction.Custom:
            if(!actionRecipient)
                actionRecipient = gameObject;
            if(parameter.Length > 0)
                actionRecipient.SendMessage (callFunction, parameter, SendMessageOptions.DontRequireReceiver);
            else
                actionRecipient.SendMessage (callFunction, SendMessageOptions.DontRequireReceiver);
            break;
        case ButtonAction.LoadLevel:
            Time.timeScale = 1;
            try
                SceneManager.LoadScene(int.Parse(parameter));
            catch(error)
                SceneManager.LoadScene(parameter);
            break;

        case ButtonAction.RestartLevel: 
            Time.timeScale = 1;
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
            break;
        case ButtonAction.LoadPreviousLevel: 
            Time.timeScale = 1;
            if (SceneManager.GetActiveScene().buildIndex > 0) // if not the first scene load the prvious scene
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
            else
                SceneManager.LoadScene(0);
            break;
    }
}

case ButtonAction.ExitGame: 
            Application.Quit();
        break; 


      case ButtonAction.SetQuality: 
          switch (parameter)
            {
                case "Fastest":
                 QualitySettings.SetQualityLevel(QualityLevel.Fastest);
                break;

                case "Fast":
                 QualitySettings.SetQualityLevel(QualityLevel.Fast);
                break;

                case "Simple":
                 QualitySettings.SetQualityLevel(QualityLevel.Simple);
                break;

                case "Good":
                 QualitySettings.SetQualityLevel(QualityLevel.Good);
                break;


                case "Beautiful":
                 QualitySettings.SetQualityLevel(QualityLevel.Beautiful);
                break;


                case "Fantastic":
                 QualitySettings.SetQualityLevel(QualityLevel.Fantastic);
                break;
           }
        break;  


      case ButtonAction.IncQuality: 
          QualitySettings.IncreaseLevel();
      break; 


      case ButtonAction.DecQuality: 
          QualitySettings.DecreaseLevel();
        break; 


      case ButtonAction.SetResolution: 
         Screen.SetResolution ( int.Parse(parameter.Substring(0,parameter.IndexOf("x"))),  int.Parse(parameter.Substring(parameter.IndexOf("x")+1)), Screen.fullScreen);
        break;


      case ButtonAction.OpenURL: 
            Application.OpenURL(parameter);
        break;               


      case ButtonAction.CloseEverything: 
          GetGUIManager().gameObject.SetActive(false);
        break;                                                                                                                                                                                                                                                                      


      case ButtonAction.Resume: 
          Time.timeScale = 1;
          transform.parent.gameObject.SendMessage("Disable");
        break; 


      case ButtonAction.ShowAnother:
          if(actionRecipient) actionRecipient.GetComponent(EGUI_Element).SetActivation(true, transform.parent.gameObject);
        break; 


      case ButtonAction.ShowPrevious:
         if(senderObject) senderObject.SetActive(true);
        break; 


      case ButtonAction.HideThis:
          transform.parent.gameObject.SendMessage("Disable");
        break; 


      case ButtonAction.HideThis_ShowAnother:
          if(actionRecipient) actionRecipient.GetComponent(EGUI_Element).SetActivation(true, transform.parent.gameObject);
          transform.parent.gameObject.SendMessage("Disable");
        break; 


      case ButtonAction.HideThis_ShowPrevious:
          if(senderObject) senderObject.SetActive(true);
          transform.parent.gameObject.SendMessage("Disable");
        break; 


      case ButtonAction.SoundSwitch:
          if(actionRecipient) actionRecipient.GetComponent(AudioListener).enabled = !actionRecipient.GetComponent(AudioListener).enabled;
        break; 

    }
}


//----------------------------------------------------------------------------------------------------


}

【问题讨论】:

    标签: unity3d load unityscript scene


    【解决方案1】:

    首先,

    Application.LoadLevel 和 Application.loadedLevel 已过时。

    见:

    Application.LoadLevel Reference

    Application.loadedLevel Reference

    相反,Unity 建议您在场景管理 API 中使用 SceneManager 类。

    我的建议是这样做以重新加载当前场景:

    // reload the current scene
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    
    // load the previous scene
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
    

    【讨论】:

    • (83,11): BCE0044: 期待 EOF,找到“案例”。
    • (84,1): BCE0044: 期待 EOF,找到 'case'。
    【解决方案2】:

    使用此 C# 脚本让您的任务更轻松。

    私有列表sceneHistory = new List(); //运行场景历史 //列表中最后一个字符串始终是当前正在运行的场景

    void Start()
    {
        DontDestroyOnLoad(this.gameObject);  //Allow this object to persist between scene changes
    }
    
    //Call this whenever you want to load a new scene
    //It will add the new scene to the sceneHistory list
    public void LoadScene(string newScene)
    {
        sceneHistory.Add(newScene);
        SceneManager.LoadScene(newScene);
    }
    
    //Call this whenever you want to load the previous scene
    //It will remove the current scene from the history and then load the new last scene in the history
    //It will return false if we have not moved between scenes enough to have stored a previous scene in the history
    public bool PreviousScene()
    {
        bool returnValue = false;
        if (sceneHistory.Count >= 2)  //Checking that we have actually switched scenes enough to go back to a previous scene
        {
            returnValue = true;
            sceneHistory.RemoveAt(sceneHistory.Count -1);
            SceneManager.LoadScene(sceneHistory[sceneHistory.Count -1]);
        }
    
        return returnValue;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      相关资源
      最近更新 更多