Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 RedCubeMan · Jun 19, 2018 at 04:45 PM · unityeditortools

EditorWindow to slow

Hello and excuse my bad english. I'm making an editor with EditorWindow. My target is to create a map (scene) like RPG Maker. They take a color (that's a 3D prefabe ... a cube) and draw it from top to bottom in the editor (or like paint). The editor makes this picture to a map.

The problem is currently the EditorWindow update too slow. When I draw a line, it takes a few seconds for it to appear.

Is it possible that EditorWindow is updated more often? Or does jamand have another solution. My code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 public class WorldEditor : EditorWindow
 {
     const int maxSize = 30;
 
     Field[][] world = new Field[maxSize][];
     List<Field> drawList = new List<Field>();
     GameObject ground;
     GUIStyle style;
 
     class Field
     {
         public int x;
         public int y;
         public int height;
         GameObject ground;
         GameObject environment;
         GameObject learnSystem;
 
         public Field(int x, int y)
         {
             this.x = x;
             this.y = y;
         }
 
         public void Draw(GUIStyle style)
         {
             if(ground != null)
             {
                 Rect rect = new Rect(x, y, 20, 20);        
                 GUI.Button(rect, "", style);
             }
         }
 
         public void SetGround(GameObject ground)
         {
             this.ground = ground;          
         }
     }
 
    
 
     private void OnEnable()
     {   
         for(int x = 0; x < maxSize; x++)
         {
             world[x] = new Field[maxSize];
             for (int y = 0; y < maxSize; y++)
             {
                 world[x][y] = new Field(x*20,y*20);
             }
         }
       
         ground = EditorGUIUtility.Load("Assets/Resources/Prefabs/World/Ground Grass Long.prefab") as GameObject;
 
         style = new GUIStyle();
         style.normal.background = EditorGUIUtility.Load("Assets/Resources/Editor/DialogSystem.png") as Texture2D;
     }
 
     [MenuItem("Window/World Editor")]
     public static void OpenWindow()
     {
         WorldEditor window = GetWindow<WorldEditor>("World Editor");
         window.Show();
     }
 
     private void OnGUI()
     {
         DrawBG(20, 0.2f, Color.gray);
         DrawBG(100, 1.5f, Color.gray);
 
         DrawField();
         Mouse();
     }
 
     void DrawField()
     {
         /*
         for (int x = 0; x < maxSize; x++)
         {          
             for (int y = 0; y < maxSize; y++)
             {
                 world[x][y].Draw();
                
             }
         }
         */
 
         for (int i = 0; i < drawList.Count; i++)
         {         
             drawList[i].Draw( style);
         }
     }
 
     void Mouse()
     {
         Event e = Event.current;
 
         if (e.button == 0)
         {
             if (e.type == EventType.MouseDrag)
             {
                 world[(int)e.mousePosition.x / 20][(int)e.mousePosition.y / 20].SetGround(ground);
 
                 if (!drawList.Contains(world[(int)e.mousePosition.x / 20][(int)e.mousePosition.y / 20]))
                     drawList.Add(world[(int)e.mousePosition.x / 20][(int)e.mousePosition.y / 20]);
 
               
             }
         }
     }
 
     private void DrawBG(float space, float transparence, Color color)
     {
        
         int currentWindowSizeWidth = (int)position.width / (int)space;
         int currentWindowSizeHeight = (int)position.height / (int)space;
 
         Handles.BeginGUI();
 
         
         Handles.color = new Color(color.r, color.g, color.b, transparence);
 
         //Height
         for (int y = 0; y < currentWindowSizeHeight + 1; y++)
         {
             Handles.DrawLine(new Vector3(0, space * y, 0), new Vector3(position.width, space * y, 0));
         }
 
         //Width
         for (int x = 0; x < currentWindowSizeWidth + 1; x++)
         {
             Handles.DrawLine(new Vector3(space * x, 0, 0), new Vector3(space * x, position.height, 0));
         }
 
         Handles.EndGUI();
 
     }
 }
 

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
1

Answer by Bunny83 · Jun 19, 2018 at 07:34 PM

Editorwindows are not redrawn contantly. If you change something on your data model you have to request a redraw:

 Field field = world[(int)e.mousePosition.x / 20][(int)e.mousePosition.y / 20];
 field.SetGround(ground);
 if (!drawList.Contains(field))
 {
     drawList.Add(field);
     e.Use();
     Repaint();
 }


Apart from that there are several things that are poorly designed or wrong. First of all you have hardcoded the size of "20" inside your Field class, but when creating it or later handle mouse input you also hardcode the same value outside of the class. Since they are linked they should either be handled completely by your Field class internally or use a constant which everyone is using.


I would strongly recommend to create a GUISkin with all your styles you need inside the resources folder and just load that skin.


Don't use "GUI.Button" if you don't handle click events. You can simply draw your style yourself.


Note that "drawList.Contains" adds quite a bit of overhead as it has to iterate through all items in the list. Since it seems your "ground" variable inside the field represents the same state you may just want to check for this variable instead.


Finally you may want to do a range check for your mouse position. If your mouse drags outside of your grid range you get an index out of bounds exception.


Just for reference: my IMGUI crash course

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

Answer by RedCubeMan · Jun 19, 2018 at 07:10 PM

I found it!

     Repaint();

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

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

Related Questions

Wonky moving tool 0 Answers

How Can I Move an Object in the Editor by MousePosition? 0 Answers

Custom levels created by users 1 Answer

FSBTool64.exe crashed when import *.mp3 3 Answers

2D tilemap building game top-down,2D TopDown Building game with tilemap 0 Answers


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