Skip to content

Instantly share code, notes, and snippets.

@Abbabon
Last active January 24, 2021 18:57
Show Gist options
  • Save Abbabon/10c73cacd8c66e3457a0ef8ae382b73b to your computer and use it in GitHub Desktop.
Save Abbabon/10c73cacd8c66e3457a0ef8ae382b73b to your computer and use it in GitHub Desktop.
SceneNavigationEditorWindow
using System;
using System.Collections.Generic;
using System.Linq;
using Game.Core.Scripts.GameTypes;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
namespace Game.Core.Editor
{
public class SceneNavigationEditorWindow : OdinEditorWindow
{
[MenuItem("Tools/Scene Navigation %g")]
private static void OpenWindow()
{
GetWindow<SceneNavigationEditorWindow>().GameScenes = InitializeGameScenes();
GetWindow<SceneNavigationEditorWindow>().Show();
}
[TableList(AlwaysExpanded = true, IsReadOnly = true, HideToolbar = true)]
public List<GameSceneActions> GameScenes;
private static List<GameSceneActions> InitializeGameScenes()
{
var gameScenes = new List<GameSceneActions>();
foreach (var sceneId in Enum.GetValues(typeof(SceneId)))
{
var sceneActions = new GameSceneActions((SceneId) sceneId);
gameScenes.Add(sceneActions);
}
return gameScenes;
}
}
public class GameSceneActions
{
private SceneId _sceneId;
[TableColumnWidth(30)]
public readonly string Name;
private string _assetPath = null;
public GameSceneActions(SceneId sceneId)
{
_sceneId = sceneId;
var sceneIdString = _sceneId.ToString();
Name = string.Concat(sceneId.ToString()
.Select(character => char.IsUpper(character) ? " " + character : character.ToString()))
.TrimStart(' ');
var assetsPaths = AssetDatabase.FindAssets($"t:scene {sceneIdString}");
if (assetsPaths.Length == 0)
{
Debug.Log($"No Scene Assets Found by SceneNavigationEditorWindow for {sceneIdString}");
return;
}
foreach (var guid in assetsPaths) //for cases in which several scene of the same name exists
{
var path = AssetDatabase.GUIDToAssetPath(guid);
var pathParts = path.Split('/');
if (path.Contains(".unity"))
{
var assetName = pathParts.Last().Replace(".unity", "");
if (String.Equals(assetName, sceneIdString, StringComparison.CurrentCultureIgnoreCase))
{
_assetPath = path;
break;
}
if (assetName.Contains(sceneIdString))
{
_assetPath = path;
break;
}
if (sceneIdString.Contains(assetName))
{
_assetPath = path;
break;
}
}
}
}
[HorizontalGroup("Actions")]
[TableColumnWidth(50)]
[Button("Launch Scene")]
public void OpenScene()
{
if (_assetPath != null)
{
EditorApplication.ExecuteMenuItem("File/Save");
EditorApplication.ExecuteMenuItem("File/Save Project");
if (Event.current != null && Event.current.alt)
{
EditorSceneManager.OpenScene(_assetPath, OpenSceneMode.Additive);
}
else
{
EditorSceneManager.OpenScene(_assetPath);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment