Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by RyuBlue · Jan 17, 2014 at 05:31 PM · sceneeditorwindowtilessceneviewrect

Draw Rect in Scene View from EditorWindow

Hi! I'm trying to create a basic level editor. The idea being a simple tile grid and when a user hovers their mouse over a tile in the scene view it will highlight.

I've got the grid to appear perfectly (see screenshot), however I can't figure out at all how I can draw a rect in the scene view. There doesn't seem to be a DrawRect in Handles and DrawRect in Gizmo's apparently cannot be used in OnSceneGUI.

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 public class LevelEditorWindow : EditorWindow {
 
     bool showGrid = true;
     Vector2 tiles = new Vector2(6,6);
 
     [MenuItem("Window/Level Editor")]
     static void ShowWindow ()
     {
         GetWindow(typeof(LevelEditorWindow), false, "Level Editor", true);
     }
     
     void OnFocus()
     {
         // Remove and re-add the sceneGUI delegate
         SceneView.onSceneGUIDelegate -= this.OnSceneGUI;
         SceneView.onSceneGUIDelegate += this.OnSceneGUI;
     }
     
     void OnDestroy()
     {
         SceneView.onSceneGUIDelegate -= this.OnSceneGUI;
     }
 
     private void OnGUI()
     {
         EditorGUILayout.LabelField("Level Editor");
 
         tiles = EditorGUILayout.Vector2Field("Tiles", tiles );
         showGrid = EditorGUILayout.Toggle("Show Grid", showGrid);
 
         EditorGUILayout.Space();
 
         if ( GUILayout.Button("Update Scene View") )
         {
             SceneView.lastActiveSceneView.Repaint();
         }
 
         EditorGUILayout.Space();
 
         EditorGUILayout.BeginHorizontal();
             GUILayout.Button("Load Level");
             GUILayout.Button("Save Level");
         EditorGUILayout.EndHorizontal();
 
     }
 
     void OnSceneGUI(SceneView sceneView)
     {
         float size_x = tiles.x;
         float size_y = tiles.y;
 
         if (showGrid)
         {
             // Draw all X grid lines
             for ( int i = 0; i <= size_x; i++ )
             {
                 // draw a line equal to x and the full height
                 Handles.DrawLine( new Vector3(i,0,0), new Vector3(i,size_y,0) );
             }
             
             // Draw all y grid lines
             for ( int i = 0; i <= size_y; i++ )
             {
                 // draw a line equal to x and the full height
                 Handles.DrawLine( new Vector3(0,i,0), new Vector3(size_x,i,0) );
             }
         }
 
         // check if mouse is over any tiles
         Vector3 mousePosition = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition).origin;
 
         if ( (mousePosition.x > 0 && mousePosition.x < size_x) && (mousePosition.y > 0 && mousePosition.y < size_y) )
         {
             // mouse is within a tile
             int hoverTile_x = (int)Mathf.Floor( mousePosition.x );
             int hoverTile_y = (int)Mathf.Floor( mousePosition.y );
 
             // DRAW RECT OVER TILE HERE
 
         }
 
     }
 
 
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

18 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

Related Questions

Hold Right-Click + "W" or "S" in Scene Editor not working properly!! 1 Answer

Unity Scene View zooming with scroll-wheel? 5 Answers

Custom scene view in editor window 2 Answers

The scene is empty 4 Answers

unity: scene shows nothing, PLEASE HELP 1 Answer


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