- Home /
Duplicate inspector with some controls as in game GUI
Is it possible to have a replication of the Unity inspector with some components such as the physics controls and menu items as a GUI in game. I doubt it but worth a shot to ask. I noticed a function called:
Editor.DrawDefaultInspector *bool DrawDefaultInspector(); Description Draw the built-in inspector.
Call this function from inside OnInspectorGUI method to draw the automatic inspector. It is useful you don't want to redo the entire inspector, but you want to add a few buttons to it.*
So that would create a custom inspector but how to add the components and how to put that into the game? It would be a huge bonus if the controls actually could take in data and act like they do in Unity but I very much doubt that. But it would be a start and save a lot of GUI work it think. So any ideas that would be great and any scripting examples please use C#.
Most likely I'll have to do it the normal way and create a custom GUI I have access to iGUI and NGUI. I've looked at some tutorials on basic controls examples (iGUI site) but I'm interested in all the physics controls which I get lost on how to do. So if someone can show the scripting steps how to do just one of the physics components (say the joints) that would be great. If in iGUI that would be great but the scripting will be very similar no matter the way it's made so I'll take any kind of example. Cheers
You would have to use .NET Reflection and your own Inspector system to be able to replicate the behaviour.
The Editor module is not accessible outside of an editor script, which only work in the editor :)
I once started writing a reflection-based inspector. It ran in the editor, not the game, but the code would work in the game too. I'm not working on it any more, but if you're curious, the code is available here.
Thanks for the info. I've been researching and trying things but just don't understand how you suggest .NET Reflection would work. Can more info be provided for just how to go about creating a physics editor for the gamer. Yoyo thanks for the code example can you expand on how you would make it work so the gamer can use it and maybe some specifics from the code I can use to do what I want. Don't mean to be a bother but I've been at this for a long time and getting nowhere. $$anonymous$$y email is andrey0002@gmail.com for any code examples.
@$$anonymous$$ega$$anonymous$$d, all the reflection API's are available in-game, but none of the Unity SerializedProperty API (which I think I also use in that project). You'll need a way to select an object for inspection -- probably by walking the transform hierarchy of the scene -- then you can get each component on each game object, and for each component use Type.GetFields to find all the fields and then FieldInfo.GetValue to display and FieldInfo.SetValue to edit.
Answer by Bunny83 · Aug 21, 2013 at 02:04 AM
You can't use any editor functionality in game. The whole UnityEditor namespace simply doesn't exist at runtime. So whenever you found a function that might be useful, first check to which namespace it belongs. UnityEngine --> ok UnityEditor --> forget it.
edit
Ok just to explain that a bit more in detail:
Unity uses Mono as scripting environment. Mono is an open source implementation of ".NET". You have almost the whole .NET framework avaliable. If you want to know what classes / methods of the .NET framework are available check the MonoCompatibility page (this site is huge, so wait until it's loaded).
Unity(the engine) itself also provides classes and functions which you can use in your game. If you take a look at the scripting reference you can see a dropdown menu at the top left. All categories starting with "Runtime" can be used in game. Those classes are inside the UnityEngine namespace.
Unity(the editor application) provides it's own set of classes which can be used by editor scripts to extend the editors functionality. All categories starting with "Editor" can only be used in editor scripts inside the editor. Those classes belong to the UnityEditor namespace which doesn't exist at runtime, so they can't be used in your game.
The reflection system, which got mentioned by Ben and yoyo is part of the Mono/.NET framework and allows type inspection of any class / method / field / property at runtime. Reflection is quite slow compared to "normal" class usage, but allows at access things in a more generic way and even to access private members which you normally couldn't access.
The reflection system provides a lot classes which are used to describe other types / classes / methods / ...
The most important class is the System.Type class. The system has an System.Type instance for every type that exists in your runtime. You can get this type object by using the typeof operator or using the GetType function of any variable at runtime. In UnityScript there is no typeof operator. You can directly use a type to get it's type object.
The Type object provides a lot properties which describe what kind of type it is, it's name and everything you can actually know about the type ;). You can use GetFields / GetMethods / GetProperties / ... To get a special class instance which describe all the fields / methods / properties which are defined by this class / struct / type.
The system is designed to be as versatile as possible. Since .NET / Mono has quite a few complex language constructs, like generics, nested classes and indexers, the system is often a pain to use. It will take you a while until you get behind how it works and how to use it.
At this point i want to stress this a bit more: Reflection shouldn't be used for the "normal" programming stuff. It actually breaks OOP encapsulation and can be really slow. It's not the preferred way to do something. However in "some" cases it's the only way.
As for the GUI side of your question, you have to program it yourself. You can either use the Unity GUI system like the editor does, or if you have other GUI frameworks like NGUI / EzGUI / ... you can roll your own solution.
Finally a last hint which should be used with caution. You can use ILSpy, which is an opensource .NET reflector which allows you to look "into" .NET DLLs and decompile them to IL or C#. That way you can "see" how the UnityEditor works behind the scene. Be warned that a lot methods are linked to native code which can't be seen or decompiled. However a lot of the EditorGUI stuff would also work at runtime. The inspector code would not since it relies heavily on the editors functionality.
I have no clue what's the legal border if you would use code snippets from the UnityEditor.dll in your game code since that wasn't designed/ment to be included in game code. So it's up to you if you simply copy & paste some of the editor code or roll your own version (inspired by the editor code ;)).
All in all to mimic the inspectors functionality at runtime requires a lot work. In most cases it makes more sense to just build some of the required GUI controls like you would for a settings screen.
Thank you. I've tried to find which namespaces are ok to use but the links that I've found are broken. Can you suggest how to do this? And how would you go about finding info in $$anonymous$$ono Develop on a specific method or class say Reflection and how to use it. I just use the search box but I think I am doing something wrong because sometimes I can find things but more often I have to do a google search. I also look in the Unity manual, reference, and scripting but often don't find what I am looking for or not enough info. Like this time for Reflection I got no help from the search in $$anonymous$$ono Develop. So how does a pro developer go from ok I have to do this and I don't know how but I'll look here and here and there and now I know what I'm doing and my tools are setup lol.
@$$anonymous$$ega$$anonymous$$d: I've edited my answer ;)
@$$anonymous$$ega$$anonymous$$d, you can use the $$anonymous$$icrosoft $$anonymous$$SDN docs, $$anonymous$$ono is usually very compliant, though I suggest looking at .NET version 3.5 rather than the very latest.
@Bunny83, ILSpy is one option, but using $$anonymous$$onoDevelop is even simpler. Just right-click on any Unity class or method mentioned in your source code and "Go to declaration" to open the Assembly Browser, which can show decompiled IL. $$anonymous$$ake sure to set the $$anonymous$$onoDevelop Assembly Folders option, as described in http://answers.unity3d.com/questions/32964/monodevelop-go-to-declaration-bug.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613112637im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Detecting GUI.changed using DrawDefaultInspector 3 Answers
How to plot velocity of vehicle wheel collider on realtime graph 0 Answers
How to show an array of custom objects in Inspector? 2 Answers
Is this a problem of unity 3 or i am doing something wrong. 1 Answer
OnInspectorGUI - Using the default Object Selection popup. 1 Answer