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 /
  • Help Room /
avatar image
0
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
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
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.

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

202 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 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 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

Unity crashes because of a certain function 0 Answers

ScriptableObject not Serializing? 0 Answers

"The editor layout could not be fully loaded, this can happen when the layotu contains EditorWindows not available in the project" 1 Answer

How to disable editor file change detection temporarily? 0 Answers

Unity crashes when compiling this script. Where is the problem? 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