Search Unity

Drawing a 2D grid on a 3D plane... UnityEngine.UI?

Discussion in 'Scripting' started by Deleted User, Oct 23, 2017.

  1. Deleted User

    Deleted User

    Guest

    I am attempting to draw a 2D grid on a plane (which is my "floor") as a sort of game board. I have made a script to generate a texture to render:
    Code (CSharp):
    1. void GenerateGrid()
    2.     {
    3.         Color gridColor = Color.cyan;
    4.         Color borderColor = Color.black;
    5.         Collider floorCollider = floor.GetComponent<Collider>();
    6.         Vector3 foorSize = new Vector3(floorCollider.bounds.size.x, floorCollider.bounds.size.z);
    7.         for (int x = 0; x < gridImage.width; x++)
    8.         {
    9.             for (int y = 0; y < gridImage.height; y++)
    10.             {
    11.                 if (x < borderSize || x > gridImage.width - borderSize || y < borderSize || y > gridImage.height - borderSize)
    12.                 {
    13.                     gridImage.SetPixel(x, y, new Color(borderColor.r, borderColor.g, borderColor.b, 50));
    14.                 }
    15.                 else gridImage.SetPixel(x, y, new Color(gridColor.r, gridColor.g, gridColor.b, 50));
    16.             }
    17.             gridImage.Apply();
    18.         }
    19.  
    20.     }
    However, I cannot for the life of me think of any way to draw this texture multiple times in a grid format onto the plane in question. I have checked the documentation and was trying to see if I could use Graphics.DrawTexture() or anything of the like that would help me. Maybe I was using it wrong. I really don't want to instanciate hundreds of gameobjects in the shape of a grid though... any advice?
     
  2. Deleted User

    Deleted User

    Guest

    Welp, answered my own question. For anybody else's reference i just did this:
    Code (CSharp):
    1. gridImage.wrapMode = TextureWrapMode.Repeat;
    2.         floor.GetComponent<MeshRenderer>().material.SetTexture(1, gridImage);
    3.         floor.GetComponent<MeshRenderer>().material.SetTextureScale(1, new Vector2(floorCollider.bounds.size.x, floorCollider.bounds.size.z));
     
    PatDough likes this.
  3. Deleted User

    Deleted User

    Guest

    Just kidding. I'm getting a weird issue here: grid shows beautifully in the editor while running. However: it doesn't show in my build version

    editor:
    : upload_2017-10-23_15-3-37.png

    build:
    upload_2017-10-23_15-4-19.png
     
  4. Deleted User

    Deleted User

    Guest

    Welp, I figured that but out as well. Code update is:

    Code (CSharp):
    1. MeshRenderer floorRenderer = floor.GetComponent<MeshRenderer>();
    2.         floorRenderer.material.mainTexture = gridImage;
    3.         floorRenderer.material.mainTextureScale = new Vector2(floorCollider.bounds.size.x, floorCollider.bounds.size.z);
    4.         floorRenderer.material.mainTextureOffset = new Vector2(.5f, .5f);
    I suppose I should spend more time debugging than browsing the forums ;). At least this will help out others who want to do the same!

    EDIT: the problem was that i was adding a new texture to the material instead of setting the MainTexture. Code works perfectly now :)
     
  5. dARkStorN-

    dARkStorN-

    Joined:
    Dec 21, 2018
    Posts:
    5
    @didwell94 Hey man, I am trying to achieve the same thing you did but when i copy your code and add my own texture and floor game object; Unity just crashes on play. Any ideas how can i fix this? Or can you share the latest working code so i can learn?
     
    Valkyrje likes this.