Last active
July 13, 2022 20:17
-
-
Save MattRix/c66de4ffe222ccf67462baddfb01e302 to your computer and use it in GitHub Desktop.
FolderNoteEditor.cs - put notes on any folder in Unity.
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
#if UNITY_EDITOR //this allows the user to put it in a non-editor folder if they want, since it's not accessing anything else | |
using UnityEditor; | |
using UnityEngine; | |
[CustomEditor(typeof(DefaultAsset))] | |
public class FolderNoteEditor : Editor | |
{ | |
bool isFolder; | |
string path; | |
AssetImporter importer; | |
public void OnEnable() | |
{ | |
path = AssetDatabase.GetAssetPath(target); | |
isFolder = AssetDatabase.IsValidFolder(path); | |
importer = AssetImporter.GetAtPath(path); | |
} | |
public override void OnInspectorGUI() | |
{ | |
if (!isFolder) return; | |
GUI.enabled = true; | |
EditorGUILayout.Space(); | |
string newText = EditorGUILayout.TextArea(importer.userData); | |
EditorGUILayout.Space(); | |
if (newText != importer.userData) | |
{ | |
importer.userData = newText; | |
AssetDatabase.WriteImportSettingsIfDirty(path); | |
} | |
} | |
//Custom icon stuff begins here. We show an icon on any folder that has notes in it. | |
[InitializeOnLoadMethod] | |
static void Init() | |
{ | |
EditorApplication.projectWindowItemOnGUI += ProjectItemOnGUI; //this callback lets us draw stuff on top of items in the project panel | |
} | |
static Texture2D iconTex; | |
static void ProjectItemOnGUI(string guid, Rect rect) | |
{ | |
var path = AssetDatabase.GUIDToAssetPath(guid); | |
if (!AssetDatabase.IsValidFolder(path)) return; | |
if (string.IsNullOrWhiteSpace(AssetImporter.GetAtPath(path).userData)) return; //there's no note | |
if (iconTex == null) //create the icon on first use | |
{ | |
var iconIndices = new [] {1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0,1}; | |
var iconColors = new [] {Color.clear, new Color(1f, 0.8f, 0.2f), new Color(0.3f, 0.25f, 0.1f)}; | |
iconTex = new Texture2D(8,8); | |
iconTex.filterMode = FilterMode.Point; | |
for(int p = 0; p<64; p++) iconTex.SetPixel(p%8, p/8, iconColors[iconIndices[p]]); | |
iconTex.Apply(); | |
} | |
float scale = EditorGUIUtility.pixelsPerPoint < 2.0f ? (1f/EditorGUIUtility.pixelsPerPoint) : 1.0f; //draw it pixel perfect when below 2.0 scale | |
var iconRect = new Rect(rect.x, rect.y, 8f * scale , 8f * scale); | |
GUI.DrawTexture(iconRect,iconTex); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment