如何在unity3d上加载上一个场景

时间:2016-09-07 19:36:13

标签: unity3d load unityscript scene

我在构建设置中总共有五个级别。我需要加载。当玩家去世时场景切换到重新启动场景。我有重启场景代码。所以它只重新启动当前场景。我不希望它那样做。我需要重新加载以前的。我需要和其他级别一样。这是我的代码:

{



#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; 

    }
}


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


}

2 个答案:

答案 0 :(得分:2)

首先,

Application.LoadLevel& Application.loadedLevel已过时。

见:

Application.LoadLevel Reference

Application.loadedLevel Reference

相反,Unity建议您使用Scene Management API中的SceneManager类。

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

// reload the current scene
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

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

答案 1 :(得分:0)

使用此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;
}