Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Editor window to quickly open scenes" task #267

Merged
merged 3 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 041bdbf27ff47ea4586c6b730fc5114c, type: 3}
m_Name: SceneAccessHolder
m_EditorClassIdentifier:
sceneList:
- name: MainMenu
path: D:\indie\_OnGoing\open-project-Unity\UOP1_Project\Assets\Scenes\Menus\MainMenu.unity
visible: 1
- name: Forest
path: D:\indie\_OnGoing\open-project-Unity\UOP1_Project\Assets\Scenes\Locations\Forest.unity
visible: 0
- name: Beach
path: D:\indie\_OnGoing\open-project-Unity\UOP1_Project\Assets\Scenes\Locations\Beach.unity
visible: 1
- name: TestingGround
path: D:\indie\_OnGoing\open-project-Unity\UOP1_Project\Assets\Scenes\TestingGround.unity
visible: 1
- name: Initialization
path: D:\indie\_OnGoing\open-project-Unity\UOP1_Project\Assets\Scenes\Initialization.unity
visible: 1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

217 changes: 217 additions & 0 deletions UOP1_Project/Assets/Scripts/EditorTools/SceneAccessTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
using UnityEngine;
using UnityEditor;
using System.IO;
using UnityEditorInternal;
using UnityEditor.SceneManagement;
using System.Collections.Generic;
using System.Linq;

public class SceneAccessTool : EditorWindow, IHasCustomMenu
{
private SceneAccessHolderSO _inspected;
private SerializedObject _serializedObject;
private Editor _sceneAccessHolderSOEditor;
public enum Layout { List, Grid}
private Layout _layout;
private bool _showOptions = false;
private bool _editMode = false;
private ReorderableList list;

private void OnEnable()
{

_inspected = Resources.Load<SceneAccessHolderSO>("SceneAccessHolder");
SetUpList();
}
private void SetUpList()
{
_serializedObject = new SerializedObject(_inspected);

if (_editMode)
{
list = new ReorderableList(_serializedObject,
_serializedObject.FindProperty("sceneList"),
true, false, false, false);
list.drawElementCallback =
(Rect rect, int index, bool isActive, bool isFocused) =>
{
var element = list.serializedProperty.GetArrayElementAtIndex(index);
rect.y += 2;
EditorGUI.PropertyField(
new Rect(rect.x, rect.y, rect.width - 30, EditorGUIUtility.singleLineHeight),
element.FindPropertyRelative("name"), GUIContent.none);

EditorGUI.PropertyField(
new Rect(rect.x + rect.width - 25, rect.y, 25, EditorGUIUtility.singleLineHeight),
element.FindPropertyRelative("visible"), GUIContent.none);
};
}
}
[MenuItem("Tools/SceneAccessTool")]
public static void ShowWindow()
{
GetWindow<SceneAccessTool>("SceneAccessTool");
}
private void OnGUI()
{
if (_inspected != null)
{
if(_showOptions)
ShowOptions();
ShowSceneList();
}
}

public void ShowOptions()
{
GUILayout.BeginHorizontal();
if (GUILayout.Button("Populate Scene List"))
{
PopulateSceneList();
}
if (GUILayout.Button("Clear List"))
{
ClearSceneList();
}
if (GUILayout.Button("Edit Mode"))
{
ToggleEditMode();
}
GUILayout.EndHorizontal();
}
public void ShowSceneList()
{
if (_editMode)
{
_serializedObject.Update();
list.DoLayoutList();
_serializedObject.ApplyModifiedProperties();
}
else
{
if (_layout == Layout.List)
{
for (int i = 0; i < _inspected.sceneList.Count; i++)
{
var sceneItem = _inspected.sceneList[i];
if (!sceneItem.visible)
{
continue;
}
if (GUILayout.Button(sceneItem.name))
{
EditorSceneManager.OpenScene(sceneItem.path);
}
}
}
else
{
int gridSize = 48;
int widthCount = gridSize;
GUILayout.BeginHorizontal();
for (int i = 0; i < _inspected.sceneList.Count; i++)
{
var sceneItem = _inspected.sceneList[i];
if (!sceneItem.visible)
{
continue;
}

GUIStyle customButton = new GUIStyle("button");
customButton.fontSize = 10;
customButton.alignment = TextAnchor.UpperLeft;
customButton.wordWrap = true;
GUIContent guiContent = new GUIContent(sceneItem.name);
if (GUILayout.Button(
guiContent,
customButton,
GUILayout.Width(gridSize),
GUILayout.Height(gridSize)))
{
EditorSceneManager.OpenScene(sceneItem.path);
}

widthCount += gridSize+4;

if (widthCount > position.width)
{
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
widthCount = gridSize;
}
}

GUILayout.EndHorizontal();
}

}
}

/// <summary>
/// Find all scenes in the project and put them in the list
/// </summary>
private void PopulateSceneList()
{
EditorBuildSettingsScene[] currentScenes = EditorBuildSettings.scenes;
EditorBuildSettingsScene[] filteredScenes = currentScenes.Where(ebss => File.Exists(ebss.path)).ToArray();
List<SceneAccessHolderSO.SceneInfo> allScene = new List<SceneAccessHolderSO.SceneInfo>();
for (int i = 0; i < filteredScenes.Length; i++)
{
allScene.Add(
new SceneAccessHolderSO.SceneInfo
{
name = Path.GetFileNameWithoutExtension(filteredScenes[i].path),
path = Path.GetFullPath(filteredScenes[i].path),
visible = true
});
}
//add the new scenes
foreach(SceneAccessHolderSO.SceneInfo sceneInfo in allScene)
{
if (!_inspected.sceneList.Contains(sceneInfo))
{
_inspected.sceneList.Add(sceneInfo);
}
}
//remove the deleted scenes
foreach (SceneAccessHolderSO.SceneInfo sceneInfo in _inspected.sceneList.ToList())
{
if (!allScene.Contains(sceneInfo))
{
_inspected.sceneList.Remove(sceneInfo);
}
}
}

private void ClearSceneList()
{
_inspected.sceneList.Clear();
}

public void AddItemsToMenu(GenericMenu menu)
{
menu.AddItem(new GUIContent("ToggleOptions"), false, ToggleOptions);
menu.AddItem(new GUIContent("ToggleLayout"), false, ToggleLayout);
}

private void ToggleOptions()
{
_showOptions = !_showOptions;
}
private void ToggleLayout()
{
if (_layout == Layout.List)
{
_layout = Layout.Grid;
}
else
{
_layout = Layout.List;
}
}
private void ToggleEditMode()
{
_editMode = !_editMode;
SetUpList();
}
}
11 changes: 11 additions & 0 deletions UOP1_Project/Assets/Scripts/EditorTools/SceneAccessTool.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class ReadOnlyAttribute : PropertyAttribute
{

}

[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property,
GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, true);
}

public override void OnGUI(Rect position,
SerializedProperty property,
GUIContent label)
{
GUI.enabled = false;
EditorGUI.PropertyField(position, property, label, true);
GUI.enabled = true;
}
}
/// <summary>
/// It holds a list of scenes that's shown in the scene quick access tool
/// </summary>
[CreateAssetMenu(fileName = "SceneAccessHolder", menuName = "Scene Data/SceneAccessHolder")]
public class SceneAccessHolderSO : ScriptableObject
{
[Serializable]
public struct SceneInfo : IEquatable<SceneInfo>
{
[ReadOnly] public string name;
[ReadOnly] public string path;
public bool visible;

public bool Equals(SceneInfo other)
{
// Would still want to check for null etc. first.
return this.path == other.path;
}
}

[Header("List of Scenes")]
public List<SceneInfo> sceneList;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.