Skip to content

Instantly share code, notes, and snippets.

@PROGrand
Last active July 6, 2024 02:17
Show Gist options
  • Save PROGrand/917e4663f161d6f48104f8cd9808cb30 to your computer and use it in GitHub Desktop.
Save PROGrand/917e4663f161d6f48104f8cd9808cb30 to your computer and use it in GitHub Desktop.
Enable file extensions view in Unity Project window.
using UnityEditor;
using UnityEngine;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
[InitializeOnLoad]
public class FileExtensionGUI
{
private static GUIStyle _style;
private static readonly StringBuilder sb = new();
private static string _selectedGuid;
private static readonly HashSet<string> ShowExtExclude = new()
{
".asmdef"
};
static FileExtensionGUI()
{
EditorApplication.projectWindowItemOnGUI += HandleOnGUI;
Selection.selectionChanged += () =>
{
if (Selection.activeObject != null)
AssetDatabase.TryGetGUIDAndLocalFileIdentifier(Selection.activeObject, out _selectedGuid, out long id);
};
}
private static bool ValidString(string str)
{
return !string.IsNullOrEmpty(str) && str.Length > 7;
}
private static string _lastGUID = string.Empty;
private static void HandleOnGUI(string guid, Rect selectionRect)
{
if (guid == _lastGUID)
return;
else
_lastGUID = guid;
if (IsThumbnailsView)
return;
var path = AssetDatabase.GUIDToAssetPath(guid);
if (0 >= path.Length)
return;
var attr = File.GetAttributes(path);
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
return;
var nameRaw = Path.GetFileNameWithoutExtension(path);
var extRaw = Path.GetExtension(path);
if (ShowExtExclude.Contains(extRaw))
return;
var selected = false;
if (ValidString(guid) && ValidString(_selectedGuid))
selected = string.Compare(guid, 0, _selectedGuid, 0, 6) == 0;
sb.Clear().Append(extRaw);
var ext = sb.ToString();
_style ??= new GUIStyle(EditorStyles.label);
_style.normal.textColor = selected ? new Color32(255, 255, 255, 255) : new Color32(220, 220, 220, 220);
var extSize = _style.CalcSize(new GUIContent(ext));
var nameSize = _style.CalcSize(new GUIContent(nameRaw));
selectionRect.x += nameSize.x + (IsSingleColumnView ? 15 : 18);
selectionRect.width = nameSize.x + 1 + extSize.x;
var offsetRect = new Rect(selectionRect.position, selectionRect.size);
EditorGUI.LabelField(offsetRect, ext, _style);
}
private static bool IsThumbnailsView {
get {
var projectWindow = GetProjectWindow();
var gridSize = projectWindow.GetType().GetProperty("listAreaGridSize", BindingFlags.Instance | BindingFlags.Public);
var columnsCount = (int) projectWindow.GetType().GetField("m_ViewMode", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(projectWindow);
return columnsCount == 1 && (float)gridSize.GetValue(projectWindow, null) > 16f;
}
}
private static bool IsSingleColumnView {
get {
var projectWindow = GetProjectWindow();
var columnsCount = (int) projectWindow.GetType().GetField("m_ViewMode", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(projectWindow);
return columnsCount == 0;
}
}
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);
}
}
@ShinuToki
Copy link

Hey, it works perfect when you are using the "one column layout", is there any way to enable it to the "two column layout" as well?

@nikpapa3010
Copy link

Just wanted to comment that it works great! cool little Quality of life improving tool. thanks for sharing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment