Last active
June 21, 2022 05:15
-
-
Save NamekMaster/ec12fcf3db3cbe15ecfe75a00af9fc08 to your computer and use it in GitHub Desktop.
show file extensions in Unity hierarchy view, support one/two column and thumbnail
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 System; | |
using UnityEngine; | |
using UnityEditor; | |
using System.IO; | |
using System.Reflection; | |
using Object = UnityEngine.Object; | |
[InitializeOnLoad] | |
public static class FileExtensions | |
{ | |
public static Color color; | |
private static string prevGuid; | |
private static GUIStyle labelStyle; | |
private static bool isPro; | |
private static EditorWindow projectWindow; | |
private static float size = 2.0f; | |
private static float halfSize = 1.0f; | |
private static int _columnsCount = 0; | |
private static GUIStyle _style; | |
private static string _selectedGuid; | |
private static GUIContent _content; | |
const int ONE_COLUMN = 0; | |
const int TWO_COLUMNS = 1; | |
static FileExtensions() | |
{ | |
if (EditorGUIUtility.isProSkin) | |
{ | |
color = Color.grey; | |
} | |
else | |
{ | |
color = Color.white; | |
} | |
EditorApplication.projectWindowItemOnGUI += ListItemOnGUI; | |
Selection.selectionChanged += () => | |
{ | |
if (Selection.activeObject != null) | |
AssetDatabase.TryGetGUIDAndLocalFileIdentifier(Selection.activeObject, out _selectedGuid, out long id); | |
}; | |
} | |
private static void ListItemOnGUI(string guid, Rect rect) | |
{ | |
if (Event.current.type != EventType.Repaint) return; | |
if (prevGuid == guid) return; | |
prevGuid = guid; | |
labelStyle ??= new GUIStyle(EditorStyles.boldLabel) | |
{ | |
normal = | |
{ | |
textColor = color | |
} | |
}; | |
var assetPath = AssetDatabase.GUIDToAssetPath(guid); | |
var obj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath); | |
if (obj != null && AssetDatabase.IsMainAsset(obj) && | |
!(obj is DefaultAsset && !AssetDatabase.IsForeignAsset(obj))) | |
{ | |
string extension = Path.GetExtension(assetPath); | |
if (IsThumbnailsView) | |
{ | |
labelStyle.normal.textColor = Color.black; | |
rect.x -= halfSize; | |
EditorGUI.LabelField(rect, extension, labelStyle); | |
rect.x += size; | |
EditorGUI.LabelField(rect, extension, labelStyle); | |
rect.x -= halfSize; | |
rect.y -= halfSize; | |
EditorGUI.LabelField(rect, extension, labelStyle); | |
rect.y += size; | |
EditorGUI.LabelField(rect, extension, labelStyle); | |
rect.y -= halfSize; | |
labelStyle.normal.textColor = Color.white; | |
EditorGUI.LabelField(rect, extension, labelStyle); | |
labelStyle.normal.textColor = color; | |
} | |
else | |
{ | |
var fileName = Path.GetFileNameWithoutExtension(assetPath); | |
_style ??= new GUIStyle(EditorStyles.label); | |
var selected = _selectedGuid?.Length > 7 && guid?.Length > 7 && | |
string.Compare(guid, 0, _selectedGuid, 0, 6) == 0; | |
_style.normal.textColor = selected ? new Color32(255, 255, 255, 255) : new Color32(220, 220, 220, 220); | |
_content ??= new GUIContent(); | |
_content.text = extension; | |
var extSize = _style.CalcSize(_content); | |
_content.text = fileName; | |
var nameSize = _style.CalcSize(_content); | |
rect.x += nameSize.x + (IsTwoColumns ? 18 : 15); | |
rect.width = nameSize.x + 1 + extSize.x; | |
var offsetRect = new Rect(rect.position, rect.size); | |
EditorGUI.LabelField(offsetRect, extension, _style); | |
} | |
} | |
} | |
private static bool IsThumbnailsView | |
{ | |
get | |
{ | |
projectWindow = GetProjectWindow(); | |
PropertyInfo gridSize = projectWindow.GetType() | |
.GetProperty("listAreaGridSize", BindingFlags.Instance | BindingFlags.Public); | |
_columnsCount = (int)projectWindow.GetType() | |
.GetField("m_ViewMode", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(projectWindow); | |
return _columnsCount == TWO_COLUMNS && (float)gridSize.GetValue(projectWindow, null) > 16f; | |
} | |
} | |
private static bool IsTwoColumns | |
{ | |
get | |
{ | |
projectWindow = GetProjectWindow(); | |
PropertyInfo gridSize = projectWindow.GetType() | |
.GetProperty("listAreaGridSize", BindingFlags.Instance | BindingFlags.Public); | |
_columnsCount = (int)projectWindow.GetType() | |
.GetField("m_ViewMode", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(projectWindow); | |
return _columnsCount == TWO_COLUMNS; | |
} | |
} | |
private static EditorWindow GetProjectWindow() | |
{ | |
if (EditorWindow.focusedWindow != null && EditorWindow.focusedWindow.titleContent.text == "Project") | |
{ | |
return EditorWindow.focusedWindow; | |
} | |
return GetExistingWindowByName("Project"); | |
} | |
private static EditorWindow GetExistingWindowByName(string name) | |
{ | |
EditorWindow[] windows = Resources.FindObjectsOfTypeAll<EditorWindow>(); | |
foreach (EditorWindow item in windows) | |
{ | |
if (item.titleContent.text == name) | |
{ | |
return item; | |
} | |
} | |
return default(EditorWindow); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment