UnityEditor 文件夹注释

前提:项目里,大家一起写项目,各写各的,文件夹起的越来越多,都不知道干嘛,需要注释一下
设计:给文件夹一个说话的机会
GUI:
在这里插入图片描述
在这里插入图片描述

#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

猜你喜欢

转载自blog.csdn.net/qq_41179365/article/details/119410228
今日推荐