Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在unity3d上加载上一个场景_Unity3d_Load_Unityscript_Scene - Fatal编程技术网

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

如何在unity3d上加载上一个场景,unity3d,load,unityscript,scene,Unity3d,Load,Unityscript,Scene,我在构建设置中总共有五个级别。我需要装货。当玩家死亡时。“场景”切换以重新启动场景。我有重启场景代码。因此,它只会重新启动当前场景。我不想让它那样做。我需要重新加载上一个。我需要对其他级别执行相同的操作。这是我的密码: { #pragma strict import UnityEngine.SceneManagement; @script AddComponentMenu ("EGUI/UI_Elements/Button") // public class EGUI_Button

我在构建设置中总共有五个级别。我需要装货。当玩家死亡时。“场景”切换以重新启动场景。我有重启场景代码。因此,它只会重新启动当前场景。我不想让它那样做。我需要重新加载上一个。我需要对其他级别执行相同的操作。这是我的密码:

{



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

    }
}


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


}
首先,

Application.LoadLevel和Application.loadedLevel已过时

见:

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

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

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

// load the previous scene
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
使用此C#脚本可以使您的任务更轻松

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

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;
}
(83,11):BCE0044:预期EOF,发现“案例”。(84,1):BCE0044:预期EOF,发现“案例”。