我的构建设置共有五个级别。我需要加载。当玩家死亡时。场景切换重新启动场景。我有重启场景代码。所以它只会重新启动当前场景。我不希望它这样做。我需要重新加载之前的.我需要对其他级别做同样的事情。这是我的代码:
{
#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# 脚本可以使您的任务变得更加轻松。
private List<string> sceneHistory = new List<string>(); //running history of scenes
//The last string in the list is always the current scene running
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;
}