Skip to content

Instantly share code, notes, and snippets.

@klaszlo8207
Last active January 18, 2023 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klaszlo8207/79d269a7d956aacb6e53c2cb556143a7 to your computer and use it in GitHub Desktop.
Save klaszlo8207/79d269a7d956aacb6e53c2cb556143a7 to your computer and use it in GitHub Desktop.
using UnityEngine.SceneManagement;
namespace _MyScripts.Utils
{
public static class SceneUtils
{
private static string NameFromIndex(int buildIndex)
{
var path = SceneUtility.GetScenePathByBuildIndex(buildIndex);
var slash = path.LastIndexOf('/');
var name = path.Substring(slash + 1);
var dot = name.LastIndexOf('.');
return name.Substring(0, dot);
}
public static int SceneIndexFromName(string sceneName)
{
for (var i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
{
var testedScreen = NameFromIndex(i);
if (testedScreen == sceneName)
return i;
}
return -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment