|
| 1 | +// draws grid lines in the game view (useful for seeing the resolution of ui elements in the game view) |
| 2 | +// usage: attach to a game object in the scene, set gameobject tag to "EditorOnly" to remove from builds |
| 3 | + |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace UnityLibrary.EditorTools |
| 7 | +{ |
| 8 | + [ExecuteAlways] |
| 9 | + public class GameViewGridOverlay : MonoBehaviour |
| 10 | + { |
| 11 | +#if UNITY_EDITOR |
| 12 | + public bool drawGrid = true; |
| 13 | + |
| 14 | + public int gridSpacingX = 64; |
| 15 | + public int gridSpacingY = 64; |
| 16 | + |
| 17 | + public int startOffsetX = 0; |
| 18 | + public int startOffsetY = 0; |
| 19 | + |
| 20 | + public Color gridColor = new Color(1f, 1f, 1f, 0.5f); |
| 21 | + |
| 22 | + private void OnGUI() |
| 23 | + { |
| 24 | + if (!drawGrid || Application.isPlaying) return; |
| 25 | + |
| 26 | + Color oldColor = GUI.color; |
| 27 | + GUI.color = gridColor; |
| 28 | + |
| 29 | + // Horizontal lines |
| 30 | + for (int y = startOffsetX; y < Screen.height; y += gridSpacingY) |
| 31 | + { |
| 32 | + GUI.DrawTexture(new Rect(0, y, Screen.width, 1), Texture2D.whiteTexture); |
| 33 | + } |
| 34 | + |
| 35 | + // Vertical lines |
| 36 | + for (int x = startOffsetY; x < Screen.width; x += gridSpacingX) |
| 37 | + { |
| 38 | + GUI.DrawTexture(new Rect(x, 0, 1, Screen.height), Texture2D.whiteTexture); |
| 39 | + } |
| 40 | + |
| 41 | + GUI.color = oldColor; |
| 42 | + } |
| 43 | +#endif |
| 44 | + } |
| 45 | +} |
0 commit comments