Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by lihail123 · Mar 26, 2020 at 04:29 PM · 2dtilemapgridscene view

Is there a way to see coordinates on a tilemap in the scene view?

I have a grid game object, and a tilemap under the grid, and I would like to see the x,y coordinates on that grid/tilemap in scene view. Is this possible?

The current way I see my 5x5 tilemap (which is currently empty): alt text

How i'd want to see it (for example): alt text

This is the same as this question, in which they suggested clicking on an object on the tilemap and look at its x,y coordinates in the inspector. However 10 years has passed so hopefully there's a better option nowadays.

a.png (12.9 kB)
b.png (13.5 kB)
Comment
Add comment · Show 1
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
avatar image MiguelCoK · May 11 at 02:46 PM 0
Share

Any solution yet?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MiguelCoK · May 21 at 05:25 PM

You can use this gizmo. It looks like this. More info in this tweet

alt text

 using System.Collections.Generic;
 using System.Linq;
 using UnityEditor;
 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 namespace Utils.Editor.Gizmos
 {
     public static class GridCoordinatesGizmo
     {
         [DrawGizmo(GizmoType.Selected)]
         public static void DrawGizmo(Grid grid, GizmoType gizmoType)
         {
             Draw(SceneView.currentDrawingSceneView, grid);
         }
         
         [DrawGizmo(GizmoType.Selected)]
         public static void DrawGizmo(Tilemap tilemap, GizmoType gizmoType)
         {
             Draw(SceneView.currentDrawingSceneView, tilemap.layoutGrid);
         }
         
         private static void Draw(SceneView sceneView, Grid grid)
         {
             var camera = sceneView.camera;
             var bottomLeftWorld = camera.ViewportToWorldPoint(Vector3.zero);
             var topRightWorld = camera.ViewportToWorldPoint(Vector3.one);
             
             var labelStyle = new GUIStyle(GUI.skin.label)
             {
                 fontStyle = FontStyle.Bold,
                 alignment = TextAnchor.UpperLeft
             };
             var tmpTextSize = labelStyle.CalcSize(new GUIContent("0"));
             float textHeightWorld = GetVerticalScreenToWorldSize(camera, tmpTextSize.y);
             float textHalfHeightWorld = textHeightWorld * 0.5f;
             float offset = textHeightWorld * 2;
 
             // compute horizontal variables
             int minCellX = grid.WorldToCell(bottomLeftWorld).x;
             int maxCellX = grid.WorldToCell(topRightWorld).x;
             float minCellXTextSize = GetTextWidthWorld($"{minCellX}", labelStyle, camera);
             float maxCellXTextSize = GetTextWidthWorld($"{maxCellX}", labelStyle, camera);
             float maxWidthSize = Mathf.Max(minCellXTextSize, maxCellXTextSize);
 
             IEnumerable<int> xCells;
             if (maxWidthSize >= grid.cellSize.x * 0.8f)
                 xCells = MathUtils.SplitNicely(minCellX, maxCellX).Select(x => (int) x);
             else
                 xCells = Enumerable.Range(minCellX, maxCellX - minCellX + 1);
             
             // compute vertical variables
             int minCellY = grid.WorldToCell(bottomLeftWorld).y;
             int maxCellY = grid.WorldToCell(topRightWorld).y;
             IEnumerable<int> yCells;
             if (textHeightWorld >= grid.cellSize.y * 0.8f)
                 yCells = MathUtils.SplitNicely(minCellY, maxCellY).Select(y => (int) y);
             else
                 yCells = Enumerable.Range(minCellY, maxCellY - minCellY + 1);
 
             var xCellsList = xCells.ToList();
             var yCellsList = yCells.ToList();
 
             string cellText = "XXX ; XXX";
             if (GetTextWidthWorld(cellText, GUI.skin.label, camera) > grid.cellSize.x * 0.7f)
                 DrawInAxis(grid, xCellsList, labelStyle, camera, bottomLeftWorld, offset, yCellsList, textHalfHeightWorld);
             else
                 DrawInCells(grid, xCellsList, yCellsList, textHeightWorld);
         }
 
         private static void DrawInAxis(Grid grid, IEnumerable<int> xCells, GUIStyle labelStyle, Camera camera,
             Vector3 bottomLeftWorld, float offset, IEnumerable<int> yCells, float textHalfHeightWorld)
         {
             foreach (var x in xCells)
             {
                 string text = $"{x}";
                 float textHalfWidthWorld = GetTextWidthWorld(text, labelStyle, camera) * 0.5f;
                 
                 float xCenter = grid.GetCellCenterWorld(new Vector3Int(x, 0, 0)).x;
                 float xText = xCenter - textHalfWidthWorld;
                 float yText = bottomLeftWorld.y + offset;
                 var handlePos = new Vector3(xText, yText, 0);
                 Handles.Label(handlePos, text, labelStyle);
             }
 
             foreach (var y in yCells)
             {
                 string text = $"{y}";
                 float yCenter = grid.GetCellCenterWorld(new Vector3Int(0, y, 0)).y;
                 float xText = bottomLeftWorld.x + offset;
                 float yText = yCenter + textHalfHeightWorld;
                 var handlePos = new Vector3(xText, yText, 0);
                 Handles.Label(handlePos, text, labelStyle);
             }
         }
         
         private static void DrawInCells(Grid grid, IEnumerable<int> xCells, IEnumerable<int> yCells, float textHeight)
         {
             var xCellsList = xCells.ToList();
             var yCellsList = yCells.ToList();
             foreach (var x in xCellsList)
             {
                 foreach (var y in yCellsList)
                 {
                     string text = $"{x} ; {y}";
                     var handlePos = grid.CellToWorld(new Vector3Int(x, y, 0));
                     handlePos.y = handlePos.y + textHeight;
                     Handles.Label(handlePos, text, GUI.skin.label);
                 }
             }
         }
 
         private static float GetTextWidthWorld(string text, GUIStyle style, Camera camera)
         {
             var textSize = style.CalcSize(new GUIContent(text));
             float textWidthWorld = GetHorizontalScreenToWorldSize(camera, textSize.x);
             return textWidthWorld;
         }
         
         private static float GetHorizontalScreenToWorldSize(Camera camera, float size)
         {
             float xZeroWorld = camera.ScreenToWorldPoint(Vector3.zero).x;
             float xRightWorld = camera.ScreenToWorldPoint(new Vector3(size, 0, 0)).x;
             return xRightWorld - xZeroWorld;
         }
         
         private static float GetVerticalScreenToWorldSize(Camera camera, float size)
         {
             float yZeroWorld = camera.ScreenToWorldPoint(Vector3.zero).y;
             float yRightWorld = camera.ScreenToWorldPoint(new Vector3(0, size, 0)).y;
             return yRightWorld - yZeroWorld;
         }
     }
 }


22-05-21-grid-coordinates-gizmomp4-potplayer-2.jpg (37.4 kB)
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

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

Get position from Isometric TileMap 1 Answer

How to use the same tilemaps among multiple scenes (e.g. making it a prefab)? 0 Answers

How can i make the Unity2d grid use rounded numbers? 1 Answer

Unity2D Grid disappears when zooming out 0 Answers

Tilemap Grid Broken? 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