Theabove demonstrates how you can get simple scripts running during edit-time, however this alone does not allow you to create your own Editor tools. The next step is to create a Custom Editor for the script you just created.
When you create a script in Unity, by default it inherits from MonoBehaviour, and therefore is a component that you can attach to a GameObject. When you place a component on a GameObject, the Inspector displays a default interface that you can use to view and edit every public variable, for example: an integer, a float, or a string.
This class must inherit from Editor. The CustomEditor attribute informs Unity which component it should act as an editor for. The CanEditMultipleObjects attribute tells Unity that you can select multiple objects with this editor and change them all at the same time.
Unity executes the code in OnInspectorGUI it displays the editor in the Inspector. You can put any GUI code in here and it works in the same way as OnGUI does, but runs inside the Inspector. Editor defines the target property that you can use to access the GameObject you are inspecting.
You have full access to all the IMGUI commands to draw any type of interface, including rendering Scenes using a CameraA component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary within Editor windows.
You can add extra code to the Scene ViewAn interactive view into the world you are creating. You use the Scene View to select and position scenery, characters, cameras, lights, and all other types of Game Object. More info
See in Glossary. To do this, implement OnSceneGUI in your custom editor.
OnSceneGUI works just like OnInspectorGUI except it runs in the Scene view. To help you make your own editing controls, you can use the functions defined in the Handles class. All functions in there are designed for working in 3D Scene views.
I am creating a custom editor. Variables do not seem to save when entering play mode or when recompiling scripts. The unity documentation is very unclear and solutions on the web have been pointing in many directions. Can someone please give us a simple and up to date answer on how to save custom editor variables?
In the latter case, you need to actually write the changes back to the serialized object- use EditorGUI.BeginChangeCheck(), then do your property fields, then EditorGUI.EndChangeCheck() and serializedObject.ApplyModifiedProperties() if anything was changed. ApplyModifiedProperties is what actually writes the changes back to the serialized object.
It seems to be permanently saving the the variables now. Thanks alot! There is tons of confusion on how to save variables for custom editors. This thread should be pinned so people can see the clearest solution.
When I click on my Object in the Scene Hierarchy I can see the Custom Inspector displaying properly and I can click on the Game Object Field and select something or drag a game object to it. This all works correctly.
When you hit the play button in the editor, all the objects in the active scene are serialized and saved, so that unity can deserialize and return them to their original state when you stop the execution in the editor. Unity also creates copies of all objects in the scene, so the changes you do during play mode change the copies, not the original objects in the scene. During this copy process it deserializes the objects with the data it saved just before copying, so no visible change is done on the objects. But not in your case!
All the scripts which inherit from MonoBehaviour are serializable, but your custom classes are not. To inform unity that you want your class to be serialized you have to use the [System.Serializable] attribute:
Just wanted to say thank you to kingcoyote, I have been messing around with custom inspectors and have been having the same problem for hours. I have read similar answers on other threads but for some reason as soon as I read your post I got everything working in a few minutes. Thanks!
Though instead of checkboxes I need ints, and a full grid (not the triangle shape.) I can't seem to figure out how to do this though... I can get a custom editor, though making the grid fails. So, is there any way I can see the code of the Physics Manager's editor (the Layer Collision Matrix is in there) or maybe someone knows a good way to do this?
Create a GUIArea where your special editor tool will be. Inside it, place afunction call that will then call the other rendering functions. Isuspect you'll want to do some encapsulation here. Encapsulating the gui in another function will allow you to duplicate its functionality (within the limits of your abstraction) and move things around on screen more easily.
Design three anchor points (Vector2). They should each represent the top-leftcoord of your row labels, column labels, and data field. Note thatthe columns anchor needs to be directly above (same x-value as) therow anchor, since rotating (next step) will transform the anchor.
Write a long GUI.Label (or several of them) for your labels.Anchor them at the column anchor. Per the image above, your labelstring could read something like"Default\nTransparentFX\nIgnoreRaycast\nWater" where the \n createsa newline.
Rotate again, -90 back to the original matrix. Alternatively, youcan copy GUI.matrix prior to step 3, then assign it back for aguaranteed matrix reset. Rotating back and forth may have some error due to floating-point and other imprecision.
PiotrK's answer included a link to an outdated version of the Unity LayerCollisionMatrix setting. However as mentioned in the Comments the provided link is not valid anymore.I'm not able to post in the comments so I needed to post this as new answer:
Surprisingly it is using GUILayout.BeginScrollView with calculated height, so it is compatible with EditorGUILayout style and utilize GUI.matrix for text rotation. I will not go thought entire code (lines 40 to 95), but I recommend checking it out.
What I would like to know is if there is a easy way using OnInspectorGUI, to replace the inspector with one that includes just one or two additional fields (so that it looks identical to the non-custom editor, except for the addition of one or two fields at the end). It seems like such a bother to recreate every field manually when I just want to add one little thing - so perhaps this is already a thing.
Or is there any way to easily tell unity to do it's default drawing for a specific type of field? When I was playing with this before, I had to commit to exactly what was to be shown, and I would need to manually write each field out --- which is bothersome if the public fields in my class(s) change - and they do frequently during development.
Odin Inspector is a plugin for Unity that lets you enjoy all the workflow benefits of having a powerful, customized and user-friendly editor, without ever having to write a single line of custom editor code.
The above demonstrates how you can get simple scripts running during edit-time, however this alone does not allow you to create your own editor tools. The next step is to create a Custom Editor for script we just created.
However now that you have control over how the inspector is displayed in an Editor script, you can use any code you like to lay out the inspector fields, allow the user to adjust the values, and even display graphics or other visual elements. In fact all of the inspectors you see within the Unity Editor including the more complex inspectors such as the terrain system and animation import settings, are all made using the same API that you have access to when creating your own custom Editors.
This is just scratching the surface of what you can do with Editor scripting. You have full access to all the IMGUI commands to draw any type of interface, including rendering scenes using a camera within editor windows.
OnSceneGUI works just like OnInspectorGUI - except it gets run in the scene view. To help you make your own editing controls in the scene view, you can use the functions defined in Handles class. All functions in there are designed for working in 3D Scene views.
Please note that should the CharacterStats be ever inherited from, the child scripts will not apply any GUI modifications we make here. We can easily change that by setting the second argument of the attribute to true.
When you now inspect the script in the editor you will notice that all the fields are gone! The reason for this is that we have replaced the standard drawing of these elements with our own custom implementation. That said, we can easily restore their default drawing by calling the method from parent (base) class.
This method requires more work but it also provides most flexibility when it comes to organizing your UI elements in inspector panel. As I mentioned before, while using this method you have to create two additional files: one defining the layout of the panel and one for styling it. Web developers will be familiar with this process as it closely resembles designing and coding a website.
Pav Creations is an independent personal blog about Games Development and ComputerGraphics. The contents of the website are primarily focused on creating various gamesmechanics and assets. You may also occasionally find articles about solving particular problems thatare related to data visualization, simulations and even web design. The common theme of all postscirculates around Computer Science and Graphics, which are the areas I'm mostly interested in. Hello,I'm Pav and these are my Creations. Please enjoy your stay!
3a8082e126