Search Unity

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

Discussion in 'Editor & General Support' started by SolarianZ, Mar 18, 2022.

  1. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    238
    Hello guys, is there any way to add a custom button to the right of editor window's toolbar?
    (just like help url upload_2022-3-18_10-32-13.png )

    upload_2022-3-18_10-19-43.png
     
  2. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    238
    [Solved] There is a Unity message "ShowButton(Rect position)" that can be used to draw UI on EditorWindow's toolbar. This message was not mentioned in Unity document page.

    Code (CSharp):
    1. public class MyWindow : EditorWindow
    2. {
    3.     private GUIStyle _toolbarButtonStyle;
    4.  
    5.     [MenuItem("Test/My Window")]
    6.     public static void Open()
    7.     {
    8.         EditorWindow.GetWindow<MyWindow>();
    9.     }
    10.  
    11.     /// <summary>
    12.     /// Draw buttons on toolbar.
    13.     /// Automatically called by unity.
    14.     /// </summary>
    15.     /// <param name="position"></param>
    16.     private void ShowButton(Rect position)
    17.     {
    18.         // button style
    19.         if (_toolbarButtonStyle == null)
    20.         {
    21.             _toolbarButtonStyle = new GUIStyle(GUI.skin.button)
    22.             {
    23.                 padding = new RectOffset()
    24.             };
    25.         }
    26.  
    27.         // draw button
    28.         if (GUI.Button(position, EditorGUIUtility.IconContent("_Help"), _toolbarButtonStyle))
    29.         {
    30.             Application.OpenURL("https://docs.unity3d.com/Manual/index.html");
    31.         }
    32.     }
    33. }
     
  3. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    238
    upload_2022-3-18_16-55-47.png
     
    Novack likes this.
  4. DrMox

    DrMox

    Joined:
    Dec 30, 2013
    Posts:
    5
    Thanks! Very useful
     
  5. fcorbel

    fcorbel

    Joined:
    Oct 27, 2016
    Posts:
    7
    Hello there ! Does anyone know if an equivalent exists for component header toolbar ? :)

    upload_2022-12-6_16-49-14.png
     

    Attached Files:

  6. sidney7111

    sidney7111

    Joined:
    Jun 28, 2020
    Posts:
    8
    According to these codes, there are well done, although I used GuiLayout.Buttont(); but the style code and iconContent code are very helpful
     
  7. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    238
    Simplified code and corrected button style (removing the gray background of the button):
    Code (CSharp):
    1. private void ShowButton(Rect position)
    2. {
    3.     // draw button
    4.     if (GUI.Button(position, EditorGUIUtility.IconContent("_Help"), GUI.skin.FindStyle("IconButton")))
    5.     {
    6.         Application.OpenURL("https://docs.unity3d.com/Manual/index.html");
    7.     }
    8. }
     
    spiney199 likes this.
  8. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    238