Search Unity

A grid for both pc and mobile - and max objects for mobile performance?

Discussion in 'Scripting' started by RalliantoDeLaVega, Nov 20, 2014.

  1. RalliantoDeLaVega

    RalliantoDeLaVega

    Joined:
    Jan 30, 2014
    Posts:
    45
    I'm working on a 3D game, movement only in x and y axis (2.5D style game), and I want it to be grid based, and I want levels to be created in runtime by both me and the player himself. Until now I've made a grid of cube-prefabs which chance texture on OnMouseOver to a specific texture according to which item that have been chosen from the GUI menu, and instantiate a cube at that position on OnMouseDown. And this works, but a level is 100 x 100 cubes, and this means 10.000 cubes when the editor scene loads up.

    So i have two questions:

    1. Is it inappropriate to instantiate 10.000 game objects thats not necessarily going to be used (When run on a mobile phone)?

    2. Isn't it more appropriate for me (or for any) to make a grid that doesn't have to be rendered, something like this https://www.assetstore.unity3d.com/en/#!/content/4004

    I've looked into http://docs.unity3d.com/ScriptReference/Handles.DrawLine.html to draw lines, but I want the lines to be visible both in scene view and in play mode. Basically I want a 2D grid that can be visible when I choose, and I want it to detect both mouse and the position at the player at runtime. Any ideas how to approach this?
     
  2. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    There's lots of other approaches. I wouldn't suggest the approach you're using. Like you said, rendering 10,000 cubes is a lot. At the very least, you could use planes instead.

    Check out the linerenderer component, it's one of many possibilities.
     
  3. RalliantoDeLaVega

    RalliantoDeLaVega

    Joined:
    Jan 30, 2014
    Posts:
    45
    The documentation says If you need to draw two or more completely separate lines, you should use multiple GameObjects, each with its own Line Renderer. so this doesn't seem as a good approach either
     
  4. RalliantoDeLaVega

    RalliantoDeLaVega

    Joined:
    Jan 30, 2014
    Posts:
    45
    Im looking for something like this

    A grid is drawn (simple lines), but they can detect the mouse, and a desired game object can be placed. How is these lines drawn and how does the grid detect the mouse?
     
  5. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    You might try RaycastHit possibilities, which can give you the world point at a ray's hit (in your case, where your grid detects the mouse), and can also tell you the texture coordinates at the hit. So you can then place a game object at the point, change the texture at that coordinate in some way, or both.

    http://docs.unity3d.com/ScriptReference/RaycastHit-point.html

    http://docs.unity3d.com/ScriptReference/RaycastHit-textureCoord.html

    Just a thought...as mentioned, I'm sure there are many ways to do this. But this would be pretty mild processor-wise, as your grid would be one plane object (needs collider for RayCast).
     
  6. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    It's only a 2d line, far less processor intensive, and you wouldn't need one for each square on the grid. It'd bring you down to 200 objects to render your 100x100 grid. Really, it's the only way I know of to easily render a line in game. It's not a matter of how many game objects you have, it's how processor intensive they are.

    Finding out if something is in a grid area is a matter of creating a List of Rect objects, and assigning each one appropriate x and y values (not forgetting the width and height). Then, when somebody clicks, you do a raycast, and loop through your Rect List, using Rect.Contains() to check if the X and Y coordinates from the raycast results are in each rect. If they are, you know where in your grid the player clicked.
     
  7. RalliantoDeLaVega

    RalliantoDeLaVega

    Joined:
    Jan 30, 2014
    Posts:
    45
    Thank you dameon, so i will be able to draw 200 lines using LineRenderer, and im being able to detect the mouse something like
    Code (CSharp):
    1. Rect rect = new Rect(0, 0, 150, 150);
    2. if (rect.Contains(Input.mousePosition))
    3.   print("Inside");
    but how will i be able to detect the player (and other things), since the rect doesn't have box collider or rigidbody?
     
  8. SeriousBusinessFace

    SeriousBusinessFace

    Joined:
    May 10, 2014
    Posts:
    127
    Hmm...A thought: You essentially want to place items where you click.

    What I would do (and you don't have to, this is just the solution that occurs to me) is make an invisible plane and a semi-transparent cube. The cube moves with the mouse using the raycast solution suggested by seejayjames, and clicking instantiates the object at the cube, or deletes it if it's already there.

    Hopefully I've explained that well enough, and answered the right question.