浏览代码

Improve SOsQuickAccessToolWindow and rename to ScriptableObjectBrowser. (#387)

* Improve SOsQuickAccessToolWindow and rename to ScriptableObjectBrowser.

* Change button styles to have left text alignment for better readability.

* Implement alphabetize type sorting.

* Replace unnecessary TryGetValue() with ContainsKey().

* Add custom GUIStyle and double click to ping function.
/main
GitHub 4 年前
当前提交
d8811c5e
共有 4 个文件被更改,包括 201 次插入144 次删除
  1. 2
      UOP1_Project/Assets/Scripts/Editor/ScriptableObjectBrowser.cs.meta
  2. 200
      UOP1_Project/Assets/Scripts/Editor/ScriptableObjectBrowser.cs
  3. 143
      UOP1_Project/Assets/Scripts/Editor/SOsQuickAccessToolWindow.cs
  4. 0
      /UOP1_Project/Assets/Scripts/Editor/ScriptableObjectBrowser.cs.meta

2
UOP1_Project/Assets/Scripts/Editor/ScriptableObjectBrowser.cs.meta


fileFormatVersion: 2
guid: ec8c662b7f1823b4799529408de198f3
guid: 33a6360e58915984bb1ecb57201acbea
MonoImporter:
externalObjects: {}
serializedVersion: 2

200
UOP1_Project/Assets/Scripts/Editor/ScriptableObjectBrowser.cs


using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
/// <summary>
/// This editor window allows you to quickly locate and edit any ScriptableObject assets in the project folder.
/// </summary>
class ScriptableObjectBrowser : EditorWindow
{
private SortedDictionary<string, System.Type> _types = new SortedDictionary<string, System.Type>();
private Dictionary<string, ScriptableObject> _assets = new Dictionary<string, ScriptableObject>();
private Vector2 _typeScrollViewPosition;
private Vector2 _assetScrollViewPosition;
private int _typeIndex;
private int _lastAssetIndex;
private bool _showingTypes = true;
private static GUIStyle _buttonStyle;
public static GUIStyle ButtonStyle
{
get
{
if (_buttonStyle == null)
{
_buttonStyle = EditorStyles.toolbarButton;
_buttonStyle.alignment = TextAnchor.MiddleLeft;
}
return _buttonStyle;
}
}
private void OnEnable()
{
LoadData();
}
private void OnFocus()
{
LoadData();
}
private void LoadData()
{
if (_showingTypes)
{
GetTypes();
}
else
{
GetAssets();
}
}
[MenuItem("ChopChop/Scriptable Object Browser")]
private static void ShowWindow()
{
GetWindow<ScriptableObjectBrowser>("Scriptable Objects");
}
private void OnGUI()
{
if (_showingTypes)
{
GUILayout.Label("Scriptable Object Types", EditorStyles.largeLabel);
if (GUILayout.Button("Refresh List"))
{
GetTypes();
}
DrawTypeButtons();
}
else
{
GUILayout.Label(GetNiceName(_types.ElementAt(_typeIndex).Key), EditorStyles.largeLabel);
GUILayout.BeginHorizontal();
if (GUILayout.Button("Refresh List"))
{
GetAssets();
}
if (GUILayout.Button("Back to Types"))
{
GetTypes();
_showingTypes = true;
}
GUILayout.EndHorizontal();
DrawAssetButtons();
}
}
/// <summary>
/// Draws a scroll view list of Buttons for each ScriptableObject type.
/// </summary>
private void DrawTypeButtons()
{
_typeScrollViewPosition = GUILayout.BeginScrollView(_typeScrollViewPosition);
for (int i = 0; i < _types.Count; i++)
{
if (GUILayout.Button(GetNiceName(_types.ElementAt(i).Key), EditorStyles.foldout))
{
_typeIndex = i;
GetAssets();
_showingTypes = false;
}
}
GUILayout.EndScrollView();
}
/// <summary>
/// Draws a scroll view list of Buttons for each ScriptableObject asset file of selected type.
/// </summary>
private void DrawAssetButtons()
{
_assetScrollViewPosition = GUILayout.BeginScrollView(_assetScrollViewPosition);
for (int i = 0; i < _assets.Count; i++)
{
if (GUILayout.Button(GetNiceName(_assets.ElementAt(i).Value.name), ButtonStyle))
{
Selection.activeObject = _assets.ElementAt(i).Value;
if (_lastAssetIndex == i)
{
EditorGUIUtility.PingObject(_assets.ElementAt(i).Value);
}
_lastAssetIndex = i;
}
}
GUILayout.EndScrollView();
}
/// <summary>
/// Gets all ScriptableObject types in project.
/// </summary>
private void GetTypes()
{
string[] GUIDs = AssetDatabase.FindAssets("t:ScriptableObject",
new string[] { "Assets/ScriptableObjects" });
ScriptableObject[] SOs = new ScriptableObject[GUIDs.Length];
for (int i = 0; i < GUIDs.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(GUIDs[i]);
SOs[i] = (ScriptableObject)AssetDatabase.LoadAssetAtPath(path, typeof(ScriptableObject));
}
_types.Clear();
for (int i = 0; i < SOs.Length; i++)
{
string typeKey = SOs[i].GetType().Name;
if (!_types.ContainsKey(typeKey))
{
_types.Add(typeKey, SOs[i].GetType());
}
}
}
/// <summary>
/// Gets all ScriptableObject asset files of selected type.
/// </summary>
private void GetAssets()
{
string[] GUIDs = AssetDatabase.FindAssets("t:" + _types.ElementAt(_typeIndex).Value.FullName);
_assets.Clear();
for (int i = 0; i < GUIDs.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(GUIDs[i]);
var SO = AssetDatabase.LoadAssetAtPath(path, typeof(ScriptableObject)) as ScriptableObject;
_assets.Add(path, SO);
}
}
/// <summary>
/// Formats string of text to look prettier and more readable.
/// </summary>
private string GetNiceName(string text)
{
string niceText = text;
niceText = ObjectNames.NicifyVariableName(niceText);
niceText = niceText.Replace(" SO", "");
niceText = niceText.Replace("-", " ");
niceText = niceText.Replace("_", " ");
return niceText;
}
}

143
UOP1_Project/Assets/Scripts/Editor/SOsQuickAccessToolWindow.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
class SOsQuickAccessToolWindow : EditorWindow
{
[Header("Editor Window Related")]
Vector2 scroll;
int selected;
[Header("SOs related")]
string[] assetSearchFolders;
List<string> SOTypes;
string[] objectsGUIDs;
string[] objectsPaths;
ScriptableObject[] objects;
string[] displayObjectsGUIDs;
List<string> displayObjectsPaths;
List<ScriptableObject> displayObjects;
private void OnEnable()
{
assetSearchFolders = new string[1];
assetSearchFolders[0] = "Assets/ScriptableObjects";
FindAllSOs();
FindDisplaySOs();
}
void OnFocus()
{
FindAllSOs();
FindDisplaySOs();
}
[MenuItem("Tools/SOs Quick Access Tool")]
private static void ShowWindow()
{
GetWindow<SOsQuickAccessToolWindow>("SOs Quick Access Tool");
}
void OnGUI()
{
GUILayout.Space(EditorGUIUtility.singleLineHeight * 0.5f);
GUILayout.BeginHorizontal();
DrawSOsPicker();
if (GUILayout.Button("Refresh All"))
{
FindAllSOs();
FindDisplaySOs();
}
GUILayout.EndHorizontal();
DrawSOsList();
}
void DrawSOsPicker()
{
EditorGUI.BeginChangeCheck();
selected = EditorGUILayout.Popup(GUIContent.none, selected, SOTypes.ToArray());
if (EditorGUI.EndChangeCheck())
{
FindDisplaySOs();
}
}
void DrawSOsList()
{
scroll = GUILayout.BeginScrollView(scroll);
for (int i = 0; i < displayObjectsGUIDs.Length; i++)
{
GUILayout.Label(i + 1 + ". " + displayObjects[i].name);
if (GUILayout.Button("Locate Quickly"))
{
EditorUtility.FocusProjectWindow();
EditorGUIUtility.PingObject(displayObjects[i]);
}
GUILayout.Space(EditorGUIUtility.singleLineHeight);
}
GUILayout.EndScrollView();
}
void FindAllSOs()
{
objectsGUIDs = AssetDatabase.FindAssets("t:ScriptableObject", assetSearchFolders) as string[];
objectsPaths = new string[objectsGUIDs.Length];
objects = new ScriptableObject[objectsGUIDs.Length];
SOTypes = new List<string>();
for (int i = 0; i < objectsGUIDs.Length; i++)
{
objectsPaths[i] = AssetDatabase.GUIDToAssetPath(objectsGUIDs[i]);
objects[i] = (ScriptableObject)AssetDatabase.LoadAssetAtPath(objectsPaths[i], typeof(ScriptableObject));
//Debug.Log(objectsGUIDs[i] + ": " + objectsPaths[i] + " - " + i);
}
for (int i = 0; i < objects.Length; i++)
{
if (SOTypes.IndexOf(objects[i].GetType().ToString()) == -1)
{
SOTypes.Add(objects[i].GetType().ToString());
}
}
}
void FindDisplaySOs()
{
if (displayObjects != null)
{
displayObjects.Clear();
}
if (displayObjectsPaths != null)
{
displayObjectsPaths.Clear();
}
string type = SOTypes[selected];
string queryString = "t:" + type;
displayObjectsGUIDs = AssetDatabase.FindAssets(queryString);
displayObjectsPaths = new List<string>(displayObjectsGUIDs.Length);
displayObjects = new List<ScriptableObject>(displayObjectsGUIDs.Length);
for (int i = 0; i < displayObjectsGUIDs.Length; i++)
{
displayObjectsPaths.Add(AssetDatabase.GUIDToAssetPath(displayObjectsGUIDs[i]));
displayObjects.Add(AssetDatabase.LoadAssetAtPath(displayObjectsPaths[i], typeof(ScriptableObject)) as ScriptableObject);
}
}
}

/UOP1_Project/Assets/Scripts/Editor/SOsQuickAccessToolWindow.cs.meta → /UOP1_Project/Assets/Scripts/Editor/ScriptableObjectBrowser.cs.meta

正在加载...
取消
保存