• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by DrDecipher · Aug 11, 2014 at 02:47 AM · c#guisceneeditor-scripting

Add GUI elements to Scene View?

To clarify: How do you add GUI components to the Scene View, not in the Game View, that are persistent?

I've searched the web $$anonymous$$ and low for the answer to this and found that the question is often misunderstood or people simply say it can't be done. The closest thing I found to a solution is a convoluted method of using a Script attached to an Object in the Scene that a CustomEditor points to and then it only displays when that object is selected.

Good news, it can be done. I'll post the solution below so others can use this.

Comment
mrtkhosravi
Ice_MJ
spilat12
zORg_alex

People who like this

4 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

  • Sort: 
avatar image

Answer by DrDecipher · Aug 11, 2014 at 06:14 AM

Here is the solution I came up with.

This method is very slick and piggy backs on the SceneView Object. This way you don't have to have anything selected.

Cheers, Doc

 using UnityEditor;
 using UnityEngine;
  
 public class SceneGUI : EditorWindow
 {
     [MenuItem("Window/Scene GUI/Enable")]
     public static void Enable()
     {
         SceneView.onSceneGUIDelegate += OnScene;
         Debug.Log("Scene GUI : Enabled");
     }
  
     [MenuItem("Window/Scene GUI/Disable")]
     public static void Disable()
     {
         SceneView.onSceneGUIDelegate -= OnScene;
         Debug.Log("Scene GUI : Disabled");
     }
  
     private static void OnScene(SceneView sceneview)
     {
         Handles.BeginGUI();
         if (GUILayout.Button("Press Me"))
             Debug.Log("Got it to work.");
  
         Handles.EndGUI();
     }
 }
Comment
Michael-Ryan
Dean-Kuai
idurvesh
mrtkhosravi
Soraphis
AlexanderKaverin
HenryStrattonFW
AntFitch
hengeworks
FlaSh-G
Quatum1000
Khena_B
Horschty
Garrettec
concordDevelopment
And 5 more...

People who like this

18 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image idurvesh · Jan 04, 2016 at 08:36 AM 3
Share

You beauty...thanks so much for share

avatar image NoDumbQuestion · Apr 03, 2018 at 05:11 AM 0
Share

The solution above broke Unity when you try to access any types of Object in Scene.

Use solution below if you want to change GameObject value in scene.

avatar image

Answer by DrDecipher · Aug 11, 2014 at 06:14 AM

Here is the solution. You can place this script anywhere it does not need to be in the editor folder. The reason this works is that the CustomEditor is looking for typeof 'GameObject'. So it is now displayed when anything is selected in the scene, hierarchy or project.

Cheers, Doc

 using UnityEditor;
 using UnityEngine;
  
 [CustomEditor(typeof(GameObject))]
 [CanEditMultipleObjects]
 public class funBoy : Editor
 {
     public void OnSceneGUI()
     {
         Handles.BeginGUI();
  
         if (GUILayout.Button("Press Me"))
             Debug.Log("Got it to work.");
  
         Handles.EndGUI();
     }
 }
Comment
Michael-Ryan
hengeworks
NoDumbQuestion
spilat12

People who like this

4 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoDumbQuestion · Apr 03, 2018 at 05:13 AM 0
Share

Use SceneView.onSceneGUIDelegate is not realiable in Unity 5.6.4. Editor crash when trying to access or change value of Object inside scene.

Use extend Editor for mono object is the best.

avatar image

Answer by CodeMasterMike · Aug 11, 2014 at 05:21 AM

I don't think you can actually do that with the current GUI system. As far as I know the scene view doesn't render any GUI objects. It will probably work with the new GUI system that comes out later though.

What I did to do what you want, was to create a seperate window, storing all GUI objects in a list and display them in the seperate window (rendering them in the OnGUI method). And if I wanted to see what a camera was seeing from the scene view, I attached a Render Texture and drew the cameras view on that (requires pro though).

 using UnityEditor;
 using UnityEngine;
 using System;
 using System.Reflection;
 using System.Collections;
 using System.Collections.Generic;
 public class GuiEditor : Editor
 {
         private Camera m_renderFromThisCamera                    = Camera.main;
         private RenderTexture m_RenderTexture                    = null;
         
         /// <summary>
         /// Initializes the GuiEditor window.
         /// </summary>
         public static void init()
         {
             //Show existing window instance. If one doesn't exist, make one.
             EditorWindow editorWindow = EditorWindow.GetWindow(typeof(GuiEditor));
             editorWindow.autoRepaintOnSceneChange = true;
             editorWindow.Show();
         }
 
         void OnDisable() 
         {
             if(m_RenderTexture != null)
                 m_RenderTexture.Release();
         }
 
         /// <summary>
         /// Is called only once.
         /// </summary>
         public void Awake ()
         {
             m_RenderTexture = new RenderTexture((int)position.width, (int)position.height, (int)RenderTextureFormat.ARGB32);
         }
 
         /// <summary>
         /// Updates the camera image if active.
         /// This function is not used when in playmode.
         /// </summary>
         public void Update()
         {
             if (m_renderFromThisCamera != null)
             {
                 m_renderFromThisCamera.targetTexture = m_RenderTexture;
                 m_renderFromThisCamera.Render();
                 m_renderFromThisCamera.targetTexture = null;
             }
             if(m_RenderTexture == null)
                 CreateRenderTexture();
             else if (m_RenderTexture.width != position.width || m_RenderTexture.height != position.height)
                 CreateRenderTexture();
         }
 
         private void CreateRenderTexture()
         {
             if(m_RenderTexture != null)
                 m_RenderTexture.Release();
 
             m_RenderTexture = new RenderTexture((int)position.width, (int)position.height, (int)RenderTextureFormat.ARGB32);
         }
         
         /// <summary>
         /// Renders all GuiObjects.
         /// </summary>
         void OnGUI()
         {
             // Render all GUI objects.
             
             // Render the Camera view here.
             if(m_RenderTexture == null)
                 CreateRenderTexture();
             else
                 GUI.DrawTexture(customWindowRect, m_RenderTexture, GuiEditorSettings.Instance.CameraScaleMode);
         }
     }
 

Its a VERY simple pseudo code of how I did it. It probably won't work or even compile, so don't just copy paste it, but it maybe gives you an idea how it can work.

I hope this gives you some help. Good luck!

Comment
ocimum
Khena_B
Quatum1000
MarcaPlays

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Quatum1000 · Oct 20, 2017 at 12:22 PM 0
Share

alt text Please, don't post any stupid stuff anymore and please do not reward yourself!

avatar image ImTheOne · Jun 24, 2021 at 07:09 AM 0
Share

i wouldnt use this long code for the purpose. thumb down

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Inspector removing MeshRenderer generates error 2 Answers

How can I make function run in the editor only when I press a button in the inspector? 1 Answer

How do I add something to the OnClick of a UI Button from an Editor-Script? 0 Answers

Z sorting of Handles 0 Answers

TexturePropertySingleLine in Editor class 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges