Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by tatelax · Jul 09, 2013 at 05:01 PM · editorwindowcreatenodebased

"Instantiating" GUI.Window in Editor

Hi All,

I'm creating a node based editor within Unity and I'm trying to figure out how to create a new window. What I want to do is create a new window, when a button is clicked. So far I've only been able to make "preset" windows. You can see my code here,

Thanks!

 using UnityEngine;
 using UnityEditor;
 
 
 public class NodeEditor : EditorWindow {
 
     Rect window1;
     Rect window2;
 
 
     [MenuItem("Window/Node editor")]
     static void ShowEditor() {
         NodeEditor editor = EditorWindow.GetWindow<NodeEditor>();
         editor.Init();
     }
 
 
     public void Init() {
         window1 = new Rect(10, 10, 100, 100);
         window2 = new Rect(210, 210, 100, 100);
     }
 
 
     void OnGUI() {
         DrawNodeCurve(window1, window2); // Here the curve is drawn under the windows
         BeginWindows();
 
         window1 = GUI.Window(1, window1, DrawNodeWindow, "Window 1");   // Updates the Rect's when these are dragged
         window2 = GUI.Window(2, window2, DrawNodeWindow, "Window 2");
 
         EndWindows();
     }
 
 
     void DrawNodeWindow(int id) {
         GUI.DragWindow();
     }
 
 
     void DrawNodeCurve(Rect start, Rect end) {
         Vector3 startPos = new Vector3(start.x + start.width, start.y + start.height / 2, 0);
         Vector3 endPos = new Vector3(end.x, end.y + end.height / 2, 0);
         Vector3 startTan = startPos + Vector3.right * 50;
         Vector3 endTan = endPos + Vector3.left * 50;
         Color shadowCol = new Color(0, 0, 0, 0.06f);
 
         for (int i = 0; i < 3; i++) {// Draw a shadow
             Handles.DrawBezier(startPos, endPos, startTan, endTan, shadowCol, null, (i + 1) * 5);
         }
 
         Handles.DrawBezier(startPos, endPos, startTan, endTan, Color.black, null, 1);
     }
 }
Comment
Add comment
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
0
Best Answer

Answer by tatelax · Jul 09, 2013 at 05:33 PM

Got it working using lists.

 using UnityEngine;
 using UnityEditor;
 using System.Collections.Generic;
 
 
 public class NodeEditor : EditorWindow {
 
     List<Rect> windows = new List<Rect>();
 
 
     [MenuItem("Window/Node editor")]
     static void ShowEditor() {
         NodeEditor editor = EditorWindow.GetWindow<NodeEditor>();
         editor.Init();
     }
 
 
     public void Init() {
         for (int i = 0; i < windows.Count; i++ ) {
             windows[i] = new Rect(10, 10, 100, 100);
         }
     }
 
 
     void OnGUI() {
         //DrawNodeCurve(window1, window2); // Here the curve is drawn under the windows
         BeginWindows();
 
         if (GUILayout.Button("Create window")) {
             Debug.Log(windows.Count);
             windows.Add(new Rect(10, 10, 100, 100));
         }
 
         for (int i = 0; i < windows.Count; i++) {
             windows[i] = GUI.Window(i, windows[i], DrawNodeWindow, "Window " + i);
         }
 
         EndWindows();
     }
 
 
     void DrawNodeWindow(int id) {
         GUI.DragWindow();
     }
 
 
     void DrawNodeCurve(Rect start, Rect end) {
         Vector3 startPos = new Vector3(start.x + start.width, start.y + start.height / 2, 0);
         Vector3 endPos = new Vector3(end.x, end.y + end.height / 2, 0);
         Vector3 startTan = startPos + Vector3.right * 50;
         Vector3 endTan = endPos + Vector3.left * 50;
         Color shadowCol = new Color(0, 0, 0, 0.06f);
 
         for (int i = 0; i < 3; i++) {// Draw a shadow
             Handles.DrawBezier(startPos, endPos, startTan, endTan, shadowCol, null, (i + 1) * 5);
         }
 
         Handles.DrawBezier(startPos, endPos, startTan, endTan, Color.black, null, 1);
     }
 }
Comment
Add comment · Show 1 · 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 Jamora · Jul 10, 2013 at 12:30 AM 0
Share

Glad I could be of assistance...

avatar image
0

Answer by Jamora · Jul 09, 2013 at 05:28 PM

You can dynamically add windows by storing the data and then calling it. First thing that pops to my mind is use a Dictionary< int,WindowData >, where WindowData is either a struct or a class that contains the Rect, windowFunction and window name. If your windowfunctions are different, you might have to consider creating an interface and then a class that implements it for each window. Based on your code, you don't have to worry.

To draw the windows, iterate through the dictionary.

 foreach(KeyValuePair<int,WindowData> kvp in windowDictionary){
     kvp.Value.winRect = GUI.Window(kvp.Key, kvp.Value.winRect, kvp.Value.winFunc, kvp.Value.winName);
 }

N.B. when using foreach, make sure you don't change the dictionary while iterating through it. Changes could be done, e.g. in a coroutine that yield return new WaitForEndOfFrame(); in the beginning.

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

16 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Draw inspector for SerializedObject within EditorWindow? 1 Answer

How to open the Animation window from an editor script ? 1 Answer

How to make a node based editor in Unity 3 Answers

What does "?" and ":" mean 1 Answer

Prevent Unity from blocking task bar? 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