PC-20230316NUNE\Administrator 2b467e56ad no message
2024-02-20 18:39:12 +08:00

49 lines
1.3 KiB
C#

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;
namespace HPJ.Examples
{
public class SceneLoader : MonoBehaviour
{
private void Update()
{
if (EventSystem.current.currentSelectedGameObject != null)
{
return;
}
if (Input.GetKeyDown(KeyCode.KeypadMinus))
{
LoadPreviousScene();
}
else if (Input.GetKeyDown(KeyCode.KeypadPlus))
{
LoadNextScene();
}
}
public void LoadNextScene()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int nextSceneIndex = currentSceneIndex + 1;
if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
{
nextSceneIndex = 0;
}
SceneManager.LoadScene(nextSceneIndex);
}
public void LoadPreviousScene()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int previousSceneIndex = currentSceneIndex - 1;
if (previousSceneIndex < 0)
{
previousSceneIndex = SceneManager.sceneCountInBuildSettings - 1;
}
SceneManager.LoadScene(previousSceneIndex);
}
}
}