美文网首页Unity 工具集Unity技术分享征服Unity3d
最简单的代码实现Unity项目资源标记

最简单的代码实现Unity项目资源标记

作者: 2b75747cf703 | 来源:发表于2016-08-25 09:48 被阅读444次

利用此方法可以扩展做资源收藏夹、按渠道打包对应的资源等。

Paste_Image.png
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using Babybus.Framework.Serialization;

[InitializeOnLoad]
public class ProjectWindowEditor
{
    private const string path = "Assets/BabyFrameWork/EditorPrefs/ProjectWindowEditor";

    private static Dictionary<string, bool> selectedDictionary;

    private static Texture2D emptyTexture2D, yellowTexture2D;

    static ProjectWindowEditor()
    {
        selectedDictionary = BinaryUtility.DeserializeBinary<Dictionary<string, bool>>(path);
        if (selectedDictionary == null)
            selectedDictionary = new Dictionary<string, bool>();

        EditorApplication.projectWindowItemOnGUI += ProjectWindowItemCallback;

        emptyTexture2D = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/BabyFrameWork/Editor/Textures/EmptyStar.png");
        yellowTexture2D = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/BabyFrameWork/Editor/Textures/YellowStar.png");
    }

    static void ProjectWindowItemCallback(string guid, Rect selectionRect)
    {
        var rect = selectionRect;
        rect.x = rect.xMax - 18f;
        rect.width = 17f;
        rect.height = 16f;

        var selected = false;
        if(!selectedDictionary.TryGetValue(guid, out selected))
        {
            if (EditorWindow.mouseOverWindow == null || !selectionRect.Contains(Event.current.mousePosition))
                return;
        }

        var image = emptyTexture2D;
        if (selected)
            image = yellowTexture2D;

        if (GUI.Button(rect, image, GUIStyle.none))
        {
            if (Event.current.button != 0)
                return;

            selectedDictionary[guid] = !selected;

            BinaryUtility.SerializeBinary(path, selectedDictionary);
        }
    }
}

相关文章

网友评论

    本文标题:最简单的代码实现Unity项目资源标记

    本文链接:https://www.haomeiwen.com/subject/tuoosttx.html