Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Mimic mecanim's grid in background

Discussion in 'Immediate Mode GUI (IMGUI)' started by Breyer, Mar 24, 2014.

  1. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    I try to reproduce mecanim's background for my tool. I know i should use Handles.DrawLine or similar but i wonder on size behaviour (flat pixel? or % of screen resolution?) small and big square and its color/alpha and width line


    there any suggestion or Unity people can answer to my specific question? I would be very grateful for that ;)
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  3. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,613
    Wouldn't it be easier to use a tiled texture?
     
  4. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Here's some c#. Drop this grid.cs onto a camera. Think this is what you'll need.

    Code (csharp):
    1. //cs example
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class grid : MonoBehaviour {
    6.  
    7.     private Rect m_LastGraphExtents;
    8.         private static readonly Color kGridMinorColorDark  = new Color (0f, 0f, 0f, 0.18f);
    9.         private static readonly Color kGridMajorColorDark  = new Color (0f, 0f, 0f, 0.28f);
    10.  
    11.     void Start () {
    12.         CreateLineMaterial();
    13.     }
    14.    
    15.     void OnGUI()
    16.     {
    17.         DrawGrid();
    18.     }
    19.    
    20.     void DrawGrid ()
    21.     {
    22.         if (Event.current.type != EventType.Repaint)
    23.             return;
    24.  
    25.         lineMaterial.SetPass(0);
    26.        
    27.         GL.PushMatrix ();
    28.         GL.Begin (GL.LINES);
    29.        
    30.         DrawGridLines (10.0f, kGridMinorColorDark);
    31.        
    32.         DrawGridLines (50.0f, kGridMajorColorDark);
    33.            
    34.         GL.End ();
    35.         GL.PopMatrix ();
    36.        
    37.     }
    38.    
    39.     private void DrawGridLines (float gridSize, Color gridColor)
    40.     {
    41.         GL.Color (gridColor);
    42.         for (float x = 0.0f; x < Screen.width; x += gridSize)
    43.             DrawLine (new Vector2 (x, 0.0f), new Vector2 (x, Screen.height));
    44.         GL.Color (gridColor);
    45.         for (float y = 0.0f; y < Screen.height; y += gridSize)
    46.             DrawLine (new Vector2 (0.0f, y), new Vector2 (Screen.width, y));
    47.     }
    48.    
    49.     private void DrawLine (Vector2 p1, Vector2 p2)
    50.     {
    51.         GL.Vertex (p1);
    52.         GL.Vertex (p2);
    53.     }
    54.  
    55.  
    56.     private Material lineMaterial;
    57.    
    58.     private void CreateLineMaterial()
    59.     {
    60.         if( !lineMaterial ) {
    61.             lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
    62.                 "SubShader { Pass { " +
    63.                 "    Blend SrcAlpha OneMinusSrcAlpha " +
    64.                 "    ZWrite Off Cull Off Fog { Mode Off } " +
    65.                 "    BindChannels {" +
    66.                 "      Bind \"vertex\", vertex Bind \"color\", color }" +
    67.                 "} } }" );
    68.             lineMaterial.hideFlags = HideFlags.HideAndDontSave;
    69.             lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
    70.         }
    71.     }
    72.  
    73. }
     
  5. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    Thank you very much!