Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make buttons in scene view?

Discussion in 'Scripting' started by igorbukur2, Nov 21, 2020.

  1. igorbukur2

    igorbukur2

    Joined:
    Jan 1, 2020
    Posts:
    9
    I want to make buttons on scene view. But i don't find how...
    Like this:
     
  2. diego-giacomelli

    diego-giacomelli

    Joined:
    Oct 27, 2012
    Posts:
    26
    Eristen likes this.
  3. igorbukur2

    igorbukur2

    Joined:
    Jan 1, 2020
    Posts:
    9
    This is the button associated with the sprite. But I want to draw buttons on top of "Scene view" like on second screenshots. (Me need buttons for my custom editor, if i click to buttons i take the tool for spawning object).
     
  4. IcePhoenix_0101

    IcePhoenix_0101

    Joined:
    Sep 9, 2017
    Posts:
    32
    Try this, You will just have a option to enable/disable your toolbar it in Windows Menu.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public class CustomTools : EditorWindow
    5. {
    6.     [MenuItem("Window/Custom Tools/Enable")]
    7.     public static void Enable()
    8.     {
    9.         SceneView.duringSceneGui += OnSceneGUI;
    10.     }
    11.  
    12.     [MenuItem("Window/Custom Tools/Disable")]
    13.     public static void Disable()
    14.     {
    15.         SceneView.duringSceneGui -= OnSceneGUI;
    16.     }
    17.  
    18.     private static void OnSceneGUI(SceneView sceneview)
    19.     {
    20.         Handles.BeginGUI();
    21.  
    22.         if (GUILayout.Button("Button"))
    23.             Debug.Log("Button Clicked");
    24.  
    25.         Handles.EndGUI();
    26.     }
    27. }
     
    Eristen likes this.
  5. igorbukur2

    igorbukur2

    Joined:
    Jan 1, 2020
    Posts:
    9
    In this video his have custom button look at 10:00 in video, how it's working?:
     
    STmihan likes this.
  6. igorbukur2

    igorbukur2

    Joined:
    Jan 1, 2020
    Posts:
    9
    Ok this looks interesting
    THX.
     
    Last edited: Nov 23, 2020
    STmihan likes this.
  7. STmihan

    STmihan

    Joined:
    Oct 21, 2019
    Posts:
    1
    You need just use [InitializeOnLoad] attribute on a inherited from UnityEngine.Editor class, and in static constructor subscribe on a SceneView.duringSceneGui event.

    Some example with a toggle:
    Code (CSharp):
    1.  
    2.     [InitializeOnLoad]
    3.     public class PointLabelHandlersEditor : UnityEditor.Editor
    4.     {
    5.         private static bool _someToggle;
    6.  
    7.         static PointLabelHandlersEditor()
    8.         {
    9.             SceneView.duringSceneGui -= OnDuringSceneGui;
    10.             SceneView.duringSceneGui += OnDuringSceneGui;
    11.         }
    12.  
    13.         private static void OnDuringSceneGui(SceneView obj)
    14.         {
    15.             Handles.BeginGUI();
    16.             {
    17.                 GUILayout.BeginArea(new Rect(10,10, 200, 70));
    18.                 {
    19.                     _someToggle= GUILayout.Toggle(_someToggle, "Some toggle!");
    20.                 }
    21.                 GUILayout.EndArea();
    22.             }
    23.             Handles.EndGUI();
    24.      
    25.             if (_showLabels)
    26.             {
    27.                 DoSomeStaff();
    28.             }
    29.         }
    30.     }

    And I highly recommend check out this video:
     
    Last edited: Aug 1, 2022
    Eristen likes this.