UnityEditor-允许用户为文件夹添加备注

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值