Question by
Pixel24 · Mar 18, 2018 at 06:11 PM ·
scripting problemcrasheditor-scripting
My custom editor window script crashes the Editor.
Hi guys! So I have this script I want to use as a way to draw sprites in the editor to later turn these sprites into gameobjects. But now I'm trying to get the drag-and-dropping of sprites to work. That is, untill this morning when my script started to crash the Editor. I have tried to open the window in a new project, but it gave me the same results.
Code:
public class SpritePaintingTools : EditorWindow
{
#region Window
[MenuItem("Painting Tools/Sprite Painter", false, 2)]
public static void Init()
{
SpritePaintingTools window = (SpritePaintingTools)GetWindow(typeof(SpritePaintingTools));
window.Show();
window.minSize = new Vector2(200, 250);
}
#endregion
//An int to referance the button that is curantly pressed.
int toolInt;
//The text to fill the toolbar.
string[] toolNames = new string[] { "Paint", "Erase", "Edit Layout" };
//An rect to set the toolbar to.
static Rect toolbar;
//An rect to set the grid to.
static Rect gridBackground;
//Mouse offset
public static Vector2 mouseOffset = new Vector2(1000, 1000);
//Mouse zoom
static float zoom;
//The sprites in the curent editor.
public static List<SpriteData> sprites = new List<SpriteData>();
private void OnGUI()
{
#region Setting some settings
#region Setting the Rects
toolbar = new Rect(25, 25, position.width - 50, (position.width - 50) / 10);
gridBackground = new Rect(25, 50 + toolbar.height, position.width - 50, position.height - toolbar.height - 75);
#endregion
#region Setting the mouse offset
if (Event.current != null && Event.current.type == EventType.MouseDrag && Event.current.button == 1)
{
mouseOffset += Event.current.delta;
Repaint();
}
#endregion
#region Setting the mouse zoom
float _zoom = zoom;
if (Event.current.isScrollWheel)
{
_zoom += Event.current.delta.y;
}
#endregion
#endregion
toolInt = GUI.Toolbar(toolbar, toolInt, toolNames);
GLGrid.Generate(gridBackground, 1000, 10, 5, mouseOffset);
var objs = SpriteHandeling.DropZone(gridBackground);
if (objs != null)
{
foreach (var o in objs)
{
int i = 0;
if (o is Texture2D)
{
sprites.Add(new SpriteData(new Rect(50 * i, 0, 50, 50), o as Texture2D));
i++;
}
}
}
SpriteHandeling.DrawSprites(sprites.ToArray());
if (GUI.changed)
Repaint();
}
class GLGrid
{
static Color _background = new Color(.4f, .4f, .4f);
static Color _smallLine = new Color(.35f, .35f, .35f);
static Color _largeline = new Color(.3f, .3f, .3f);
public static void Generate(Rect rect, float windowSize, float smallTileSize, int subdivisions, Vector2 offset)
{
//Draw the background
EditorGUI.DrawRect(rect, _background);
// Calculate large tile size
float largeTileSize = smallTileSize * (float)subdivisions;
//Make the glitchy side effects not happen by not letting it go into the -.
if (offset.x < 0)
{
mouseOffset = new Vector2(0, offset.y);
}
if (offset.y < 0)
{
mouseOffset = new Vector2(offset.x, 0);
}
if (offset.x > windowSize)
{
mouseOffset = new Vector2(1000, offset.y);
}
if (offset.y > windowSize)
{
mouseOffset = new Vector2(offset.x, 1000);
}
GL.PushMatrix();
GL.LoadPixelMatrix();
// Vertical small lines
for (float x = 0 + offset.x % smallTileSize + 1; x < rect.width; x += smallTileSize * zoom)
{
GL.Begin(GL.LINES);
GL.Color(_smallLine);
GL.Vertex3(rect.x + x, rect.y + 1, 0);
GL.Vertex3(rect.x + x, rect.y + rect.height, 0);
GL.End();
}
// Horizontal small lines
for (float y = 0 + offset.y % smallTileSize + 1; y < rect.height; y += smallTileSize * zoom)
{
GL.Begin(GL.LINES);
GL.Color(_smallLine);
GL.Vertex3(rect.x, rect.y + y, 0);
GL.Vertex3(rect.x + rect.width, rect.y + y, 0);
GL.End();
}
// Vertical large lines
for (float x = 0 + offset.x % largeTileSize + 1; x < rect.width; x += largeTileSize * zoom)
{
GL.Begin(GL.LINES);
GL.Color(_largeline);
GL.Vertex3(rect.x + x, rect.y + 1, 0);
GL.Vertex3(rect.x + x, rect.y + rect.height, 0);
GL.End();
}
// Horizontal large lines
for (float y = 0 + offset.y % largeTileSize + 1; y < rect.height; y += largeTileSize * zoom)
{
GL.Begin(GL.LINES);
GL.Color(_largeline);
GL.Vertex3(rect.x, rect.y + y, 0);
GL.Vertex3(rect.x + rect.width, rect.y + y, 0);
GL.End();
}
GL.PopMatrix();
}
}
class SpriteHandeling
{
public static object[] DropZone(Rect rect)
{
Rect drop_area = rect;
var eventType = Event.current.type;
bool isAccepted = false;
if (drop_area.Contains(Event.current.mousePosition))
{
if (eventType == EventType.DragUpdated || eventType == EventType.DragPerform)
{
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (eventType == EventType.DragPerform)
{
DragAndDrop.AcceptDrag();
isAccepted = true;
}
Event.current.Use();
}
}
return isAccepted ? DragAndDrop.objectReferences : null;
}
public static void DrawSprites(SpriteData[] sprites)
{
if (sprites != null)
{
foreach (SpriteData DT in sprites)
{
Rect _location = DT.location;
_location.x += gridBackground.x + mouseOffset.x - 1000;
_location.y += gridBackground.y + mouseOffset.y - 1000;
GUI.DrawTexture(_location, DT.picture);
}
focusedWindow.Repaint();
}
}
}
public class SpriteData
{
public Rect location;
public Texture2D picture;
public SpriteData(Rect location, Texture2D pic)
{
this.location = location;
this.picture = pic;
}
}
}
If you guys know what I'm doing wrong I would very much like to know. I have been working on this for AGES...
Comment
Best Answer
Answer by Pixel24 · Mar 19, 2018 at 10:10 AM
I found the problem: I took out the "zoom" function and the window stopped freezing the editor.