• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by Unity_N00b · Jan 22, 2013 at 12:09 PM · c#onguieditorguieditorguilayoutguilayout.window

C#: UnityEditor using window editor functions across classes in ONGUI()

I am creating a large editor system for my game for Unity. It is wit$$anonymous$$n 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 t$$anonymous$$s.

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()

 //------- T$$anonymous$$s 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 t$$anonymous$$s is beyond my skills. Any pointers would be gratefully received.

Thank you

Comment
Bunny83

People who like this

1 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

2 Replies

· Add your reply
  • Sort: 
avatar image

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.

Comment

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 Bunny83 · Jan 31, 2013 at 11:43 PM 0
Share

I would never recommend to use partial classes that way. See this Stackoverflow question

avatar image RudyTheDev · Feb 01, 2013 at 10:19 AM 0
Share

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.

avatar image

Answer by Bunny83 · Jan 31, 2013 at 11:38 PM

I've done t$$anonymous$$nge like t$$anonymous$$s 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 t$$anonymous$$s:

 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 t$$anonymous$$s 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 t$$anonymous$$s:

 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 w$$anonymous$$ch holds a list of windows.

That's just a small example.

Comment

People who like this

0 Show 0 · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

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

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

EditorGUILayout.TextField returning empty string 3 Answers

Custom Editor - Is there any way to detect whether the user is in Prefab editing mode? 1 Answer


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