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