#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
UnityEditor-允许用户为文件夹添加备注
最新推荐文章于 2024-09-12 17:01:51 发布