How to add a custom button to the top right corner of the project window?

Hello guys.

I want to add a custom button to the top right corner of the regular project window.

I know it’s possible because when I was working in an old job, there was an in-house extension which does that but I never checked its code so I don’t have any idea about how to do it.

Can you guys think of a proper way for that?

Where, exactly? On the menu bar, where “File” is on the left hand side? And what Unity version?

This kind of stuff will be a lot easier with Unity versions that use UI Toolkit for UI, since that allows you to reflect in, find the VisualElement you want to edit, and put something there. If it works with imgui, it’s going to be harder.

Thank you for the detailed info.

I’m on 2020.1.6 and I want to draw the button right here…

It’s not perfect but I achieved what I almost wanted by using EditorApplication.projectWindowItemOnGUI event.

6368493--708798--OnlyShowGameFiles.png

using UnityEditor;
using UnityEngine;

namespace Xtro
{
    [InitializeOnLoad]
    class OnlyShowGameFiles
    {
        static readonly Color32 Color = new Color32(56, 56, 56, 255);

        static bool Enabled;

        static OnlyShowGameFiles()
        {
            Enabled = EditorPrefs.GetBool(nameof(OnlyShowGameFiles), true);

            EditorApplication.projectWindowItemOnGUI += EditorApplication_ProjectWindowItemOnGUI;
        }

        static void EditorApplication_ProjectWindowItemOnGUI(string Guid, Rect Rect)
        {
            var ItemIsAssets = AssetDatabase.GUIDToAssetPath(Guid) == "Assets";

            var ToggleRect = Rect;
            ToggleRect.x += 200;

            if (Enabled && !ItemIsAssets && !AssetDatabase.GUIDToAssetPath(Guid).StartsWith("Assets/Game"))
            {
                Rect.x -= 100;
                Rect.width += 100;

                GUI.Button(Rect, "");

                var OldColor = GUI.color;
                GUI.color = Color;
                GUI.DrawTexture(Rect, Texture2D.whiteTexture);
                GUI.color = OldColor;
            }

            if (ItemIsAssets)
            {
                var NewValue = GUI.Toggle(ToggleRect, Enabled, "Game Only");

                if (NewValue != Enabled)
                {
                    Enabled = NewValue;
                    EditorPrefs.SetBool(nameof(OnlyShowGameFiles), Enabled);
                }
            }
        }
    }
}
1 Like