- Home /
Always show Custom Editor GUI
I have a custom editor script with OnSceneGUI and some GUI labels and buttons which all work nicely. However when I select a different object in the scene the GUI is no longer visible. How can I keep it so the GUI is always visible on the sceneview?
To do this, you need the script to be separate from any inspector and register the event to the OnSceneGUIDelegate callback. Like this.
public class $$anonymous$$yCustomGUI
{
[$$anonymous$$enuItem("Tools/Show$$anonymous$$yCustomGUI")]
public static void InitGUI()
{
$$anonymous$$yCustomGUI $$anonymous$$CG = new $$anonymous$$yCustomGUI();
OnSceneGUIDelegate += $$anonymous$$CG.RenderSceneGUI;
}
public void RenderSceneGUI()
{
//Scene gui stuff
}
}
This worked! Thank you so much!
I had to change a couple of things for it to work though:
using UnityEditor;
public class $$anonymous$$yCustomGUI {
[$$anonymous$$enuItem("Tools/Show$$anonymous$$yCustomGUI")]
public static void InitGUI() {
$$anonymous$$yCustomGUI $$anonymous$$CG = new $$anonymous$$yCustomGUI();
SceneView.onSceneGUIDelegate += $$anonymous$$CG.RenderSceneGUI;
}
public void RenderSceneGUI(SceneView sceneview) {
//Scene gui stuff
}
}
Just a quick follow up question: how can I reference an instance of a different editor script(with an OnInspectorGUI) to, for instance, use in the RenderSceneDelegate?
FindObjectOfType doesn't seem to work on editor scripts?
Use singleton in the script you want to reference, so you can just access it like
$$anonymous$$yCustomGUI.Instance.someField = this.someOtherField;
You could also pass an action from your inspector to your delegate script like this;
public class $$anonymous$$yCustomGUI
{
[$$anonymous$$enuItem("Tools/Show$$anonymous$$yCustomGUI")]
public static void InitGUI() {
SceneView.onSceneGUIDelegate += Instance.RenderSceneGUI;
}
public static $$anonymous$$yCustomGUI Instance;
{
get
{
if (_instance == null)
{
_instance = new $$anonymous$$yCustomGUI();
}
return _instance;
}
private set {_instance = value;}
}
private static $$anonymous$$yCustomGUI _instance
private Action delegateAction;
public void RegisterAction(Action action)
{
delegateAction = action;
}
public void RenderSceneGUI(SceneView sceneview) {
if (delegateAction != null)
{
delegateAction.Invoke();
}
}
}
Answer by JDatUnity · Jul 06, 2017 at 01:00 PM
This is the answer, from the comments:
using UnityEditor;
public class MyCustomGUI {
[MenuItem("Tools/ShowMyCustomGUI")]
public static void InitGUI() {
MyCustomGUI MCG = new MyCustomGUI();
SceneView.onSceneGUIDelegate += MCG.RenderSceneGUI;
}
public void RenderSceneGUI(SceneView sceneview) {
//Scene gui stuff
}
}
Your answer
