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*.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();

}
}

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

I found it!

    Repaint();