Skip to content

Instantly share code, notes, and snippets.

@YN-Studio
Last active May 4, 2023 18:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YN-Studio/b6be1829cc493bae04b6a27ad94bfcbb to your computer and use it in GitHub Desktop.
Save YN-Studio/b6be1829cc493bae04b6a27ad94bfcbb to your computer and use it in GitHub Desktop.
A custom editor window for quickly open pre-defined scene assets
using System.Collections.Generic;
using Sirenix.OdinInspector.Editor;
using UnityEditor;
using UnityEngine;
using Sirenix.OdinInspector;
using Sirenix.Utilities.Editor;
using UnityEditor.SceneManagement;
public class LevelManagementEditor : OdinMenuEditorWindow
{
private SceneData sceneData;
//Add your own menu here
[MenuItem("Tool/")]
private static void OpenWindow()
{
GetWindow<LevelManagementEditor>("Fast Level Open/快速打开关卡").Show();
}
protected override OdinMenuTree BuildMenuTree()
{
var tree = new OdinMenuTree();
tree.Config.DrawSearchToolbar = true;
tree.Add("Level-Open Presets", new OpenDefaultScenes());
//Add your own scene asset folder paths here
tree.AddAllAssetsAtPath("Level/G", "Assets/Scenes/G", typeof(SceneAsset), true, true).AddThumbnailIcons();
tree.AddAllAssetsAtPath("Level/D", "Assets/Scenes/D", typeof(SceneAsset), true, true).AddThumbnailIcons();
return tree;
}
protected override void OnBeginDrawEditors()
{
OdinMenuTreeSelection selected = this.MenuTree.Selection;
if (selected.SelectedValue is SceneAsset)
{
SirenixEditorGUI.BeginHorizontalToolbar();
{
GUILayout.FlexibleSpace();
if (SirenixEditorGUI.ToolbarButton("Singly Open/单独打开"))
{
OpenSceneSelected(selected, OpenSceneMode.Single);
}
if (SirenixEditorGUI.ToolbarButton("Additively Open/添加场景"))
{
OpenSceneSelected(selected, OpenSceneMode.Additive);
}
if (SirenixEditorGUI.ToolbarButton("Additively Open No Loading/添加场景(不激活)"))
{
OpenSceneSelected(selected, OpenSceneMode.AdditiveWithoutLoading);
}
GUILayout.FlexibleSpace();
}
SirenixEditorGUI.EndHorizontalToolbar();
//显示场景信息(名称和路径),按钮用于ping到project window中
//Show Level data(name and path), a button to ping the scene asset selected
SirenixEditorGUI.BeginBox(new GUIContent(), false, default);
{
SirenixEditorGUI.Title("Scene Data", "", TextAlignment.Center, true);
SirenixEditorGUI.MessageBox("Scene Name: " + ShowSceneData(selected));
SirenixEditorGUI.BeginHorizontalToolbar();
{
GUILayout.FlexibleSpace();
if (SirenixEditorGUI.ToolbarButton("Ping scene asset/打开文件所在位置"))
{
if (sceneData != null)
{
SceneAsset scene = AssetDatabase.LoadAssetAtPath<SceneAsset>(sceneData.path);
EditorGUIUtility.PingObject(scene);
}
}
}
SirenixEditorGUI.EndHorizontalToolbar();
GUILayout.FlexibleSpace();
}
SirenixEditorGUI.EndBox();
}
}
/// <summary>
/// Use to store the Scriptable Objects of scene open presets(LevelOpenData)
/// 用于直接打开默认的几个核心场景
/// </summary>
public class OpenDefaultScenes
{
[AssetList(AutoPopulate = true, Path = "Scenes/")]
[InlineEditor(InlineEditorModes.GUIAndPreview)]
[LabelText("Scene Open Data(SO)")]
public LevelOpenData openData;
}
/// <summary>
/// Open scene in specified mode
/// 以指定的方法,打开在菜单栏中选择的场景
/// </summary>
private void OpenSceneSelected(OdinMenuTreeSelection selection, OpenSceneMode mode = OpenSceneMode.Additive)
{
EditorSceneManager.SaveOpenScenes();
SceneAsset currentSelectedScene = selection.SelectedValue as SceneAsset;
string path = AssetDatabase.GetAssetPath(currentSelectedScene);
EditorSceneManager.OpenScene(path, mode);
}
/// <summary>
/// Show the scene data in the editor
/// 在左侧菜单栏所选中的scene
/// </summary>
private string ShowSceneData(OdinMenuTreeSelection selection)
{
SceneAsset currentSelectedScene = selection.SelectedValue as SceneAsset;
if (currentSelectedScene != null)
{
string sceneName = currentSelectedScene.name;
string path = AssetDatabase.GetAssetPath(currentSelectedScene);
sceneData = new SceneData(sceneName, path);
return sceneData.sceneName + "\n" + "Scene Path: " + sceneData.path;
}
return null;
}
private class SceneData
{
public readonly string sceneName;
public readonly string path;
public SceneData(string sceneName, string path)
{
this.sceneName = sceneName;
this.path = path;
}
}
}
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.Serialization;
[CreateAssetMenu(fileName = "场景打开存档", menuName = "LevelOpenData", order = 0)]
public class LevelOpenData : ScriptableObject
{
[LabelText("Scenes and Open modes")]
public List<OpenData> opendata;
[Button("Open Scenes/打开场景", ButtonSizes.Large)]
public void OpenScene()
{
EditorSceneManager.SaveOpenScenes();
foreach (var data in opendata)
{
string path = AssetDatabase.GetAssetPath(data.scene);
EditorSceneManager.OpenScene(path, data.openSceneMode);
}
}
}
[System.Serializable]
public class OpenData
{
[FormerlySerializedAs("asset")]
public SceneAsset scene;
public OpenSceneMode openSceneMode = OpenSceneMode.Additive;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment