Created
May 21, 2019 19:43
-
-
Save pencilking2002/e053d1545c174b1dee233cf9275e3b4a to your computer and use it in GitHub Desktop.
View and edit all your Scriptable Objects in a single Editor Window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using Object = UnityEngine.Object; | |
using System; | |
// DESCRIPTION | |
// This is a Unity editor script that allows you to view and edit all your Scriptable Objects in one Editor Window | |
// INSTRUCTIONS | |
// In Unity, place this file in an Editor folder. | |
// To use the tool, go to Tools > Scriptable Object Window in the top menu. | |
// The tool automatically docks next to your inspector. | |
// Place your Scriptable Objects into the slots and rejoice in how easy this makes your life! | |
// Notes: Tested in Unity 2018.4.0f1 | |
namespace ChaoticFormulaBF | |
{ | |
public class ScriptableObjectWindow : EditorWindow | |
{ | |
private Editor[] editorArr; | |
[SerializeField] | |
private Object[] settingsArr; | |
private int numObjects = 2; | |
private string soPathsKey = "soPaths"; | |
private static GUIStyle _soContainer; | |
private static GUIStyle soContainer | |
{ | |
get | |
{ | |
if (_soContainer == null) | |
_soContainer = new GUIStyle(GUI.skin.box); | |
return _soContainer; | |
} | |
} | |
[MenuItem("Tools/Scriptable Object Window")] | |
private static void Init() | |
{ | |
Type inspectorType = Type.GetType("UnityEditor.InspectorWindow,UnityEditor.dll"); | |
GetWindow<ScriptableObjectWindow>(inspectorType); | |
var window = (ScriptableObjectWindow) GetWindow(typeof(ScriptableObjectWindow)); | |
window.Show(); | |
} | |
private void OnEnable() | |
{ | |
titleContent = new GUIContent("Scriptable Object Window"); | |
if (EditorPrefsExist()) | |
{ | |
LoadObjectsFromEditorPrefs(); | |
} | |
else | |
{ | |
SetObjectArray(); | |
} | |
} | |
private void OnGUI() | |
{ | |
GUILayout.BeginVertical(soContainer); | |
RenderObjectNumberField(); | |
RenderObjectFields(); | |
GUILayout.EndVertical(); | |
for (int i = 0; i < settingsArr.Length; i++) | |
{ | |
if (settingsArr[i] != null) | |
{ | |
GUILayout.Label(settingsArr[i].name); | |
GUILayout.BeginVertical(soContainer); | |
if (editorArr[i] == null) | |
editorArr[i] = Editor.CreateEditor(settingsArr[i]); | |
editorArr[i].OnInspectorGUI(); | |
GUILayout.EndVertical(); | |
} | |
} | |
} | |
private bool EditorPrefsExist() | |
{ | |
return EditorPrefs.HasKey(soPathsKey); | |
} | |
private void LoadObjectsFromEditorPrefs() | |
{ | |
var soPaths = EditorPrefs.GetString(soPathsKey); | |
string[] paths = soPaths.Split(','); | |
numObjects = paths.Length; | |
settingsArr = new Object[numObjects]; | |
editorArr = new Editor[numObjects]; | |
for (int i = 0; i < settingsArr.Length; i++) | |
{ | |
if (System.IO.File.Exists(paths[i])) | |
{ | |
settingsArr[i] = AssetDatabase.LoadAssetAtPath<ScriptableObject>(paths[i]); | |
} | |
} | |
} | |
private void SetEditorPrefs() | |
{ | |
string paths = ""; | |
for (int i = 0; i < numObjects; i++) | |
{ | |
string comma = ","; | |
// Ignore the comma for the last object | |
if (i == numObjects - 1) | |
comma = ""; | |
paths += AssetDatabase.GetAssetPath(settingsArr[i]) + comma; | |
} | |
if (!string.IsNullOrEmpty(paths)) | |
{ | |
EditorPrefs.SetString(soPathsKey, paths); | |
Debug.Log("Recorded SO paths: " + paths); | |
} | |
} | |
/// <summary> | |
/// Creates arrays to be used in displaying object fields | |
/// </summary> | |
private void SetObjectArray() | |
{ | |
settingsArr = new Object[numObjects]; | |
editorArr = new Editor[numObjects]; | |
for (int i = 0; i < numObjects; i++) | |
{ | |
if (settingsArr[i] == null) | |
{ | |
settingsArr[i] = new Object(); | |
editorArr[i] = null; | |
} | |
} | |
} | |
private void RenderObjectNumberField() | |
{ | |
EditorGUI.BeginChangeCheck(); | |
numObjects = EditorGUILayout.IntField("Number of Objects", numObjects); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
SetObjectArray(); | |
} | |
} | |
private void RenderObjectFields() | |
{ | |
for (int i = 0; i < numObjects; i++) | |
{ | |
EditorGUI.BeginChangeCheck(); | |
settingsArr[i] = EditorGUILayout.ObjectField("Scriptable Object", settingsArr[i], typeof(Object), false); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
SetEditorPrefs(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does not scroll when I am editing a large scriptableobject.