- Home /
C#: UnityEditor using window editor functions across classes in ONGUI()
I am creating a large editor system for my game for Unity. It is within a single UnityWindow (OnGUI()) with buttons down the left handside for each of the different editor types. Upon clicking on an editor it opens a sub window (GUILayout.Window). I would like all the code for each of these sub windows to be in separate classes so I dont need a 10000 lines in a single file. However at the moment I have not found a way of doing this.
Code Outline
public class Designer : EditorWindow {
void OnGUI(){
BeginWindows();
GUILayout.BeginVertical();
switch(currentEditor){
case editors.GameLogic:
GUILayout.Window (1, editorRect, GameLogic, "Game Logic Editor");
break;
// ETC... case editors.Room:
default:
Debug.Log ("No Editor was selected");
break;
}
GUILayout.EndVertical();
EndWindows ();
} // End of OnGUI()
//------- This function is what I would like to separate into another class ------//
public void GameLogic(int windowID){
indexNew = EditorGUI.Popup(new Rect(startX, startY, 300, height), "Modify Room:", indexNew, RoomNameList);
//etc
}
// ------- -------------- ------------///
} // End of Designer Class
At the moment doing this is beyond my skills. Any pointers would be gratefully received.
Thank you
Answer by RudyTheDev · Jan 31, 2013 at 10:58 PM
You can make your class partial:
public partial class Designer : EditorWindow
{
void OnGUI()
{
...
GUILayout.Window(1, windowRect, GameLogicWindow, "Game Logic Window");
...
}
}
public partial class Designer : EditorWindow
{
private void GameLogicWindow()
{
...
}
}
Then put these in different files.
I would never recommend to use partial classes that way. See this Stackoverflow question
If they are all in the same folder, not mixed with other classes, each file has just the partial bit, and files are all named consistently, I don't see a problem. But it is a style preference (as that Stackoverflow question shows) that, I admit, can lead to bad decisions in unexperienced hands.
Answer by Bunny83 · Jan 31, 2013 at 11:38 PM
I've done thinge like this many times even at runtime. Just use "normal" classes and create an instance of it in your EditorWindow. I would start creating a window-baseclass like this:
public class CWindow
{
protected static int m_WindowIDCounter = 5555;
private int m_WindowID = m_WindowIDCounter++; // simple automatic id distribution
protected GUIContent m_Content;
protected GUIStyle m_Style;
protected Rect m_Position;
public int WindowID {get {return m_WindowID;}}
public CWindow(Rect aStartPos, GUIContent aContent, GUIStyle aStyle)
{
m_Content = aContent;
m_Style = aStyle;
m_Position = aStartPos;
}
public virtual void OnGUI()
{
m_Position = GUI.Window(WindowID, m_Position, Draw, m_Content, m_Style);
}
public virtual void Draw(int aID)
{ }
}
public class CLayoutWindow : CWindow
{
protected GUILayoutOption[] m_Options;
public CLayoutWindow(Rect aStartPos, GUIContent aContent, GUIStyle aStyle, params GUILayoutOption[] aOptions)
: base(aStartPos, aContent, aStyle)
{
m_Options = aOptions;
}
public override void OnGUI()
{
m_Position = GUILayout.Window(WindowID, m_Position, Draw, m_Content, m_Style, m_Options);
}
}
With those base classes you can create many different derived classes like this one:
public class CSampleWindow : CLayoutWindow
{
public string windowContent;
public CSampleWindow(Rect aStartPos, GUIContent aContent, GUIStyle aStyle, params GUILayoutOption[] aOptions)
: base(aStartPos, aContent, aStyle, aOptions)
{}
public override void Draw(int aID)
{
GUI.DragWindow();
GUILayout.Label("Some sample content");
GUILayout.Label(windowContent);
}
}
In your EditorWindow you can implement a Window management system or just use single windows like this:
public class MyEWindow : EditorWindow
{
CSampleWindow m_Window1 = null;
public void OnGUI()
{
GUI.enabled = m_Window1 == null;
if (GUILayout.Button("Open SampleWindow1"))
{
m_Window1 = new CSampleWindow(new Rect(10,10,200,200),new GUIContent("SampleWindowTitle"), "Window");
}
if (m_Window1 != null)
m_Window1.OnGUI();
}
}
Now you can extend even the base class to provide a way to mark a window as "closed" so it's not drawn anymore or even destroyed. You can also implement a whole window management class which holds a list of windows.
That's just a small example.