Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 QuapPac · May 26, 2014 at 04:06 AM · gridchess

How to create a 14 by 12 grid, depending on screen size?

I am attempting to create a 14 x 12 grid with a 2 pixel space surrounding each grid "tile". The tile is just a primitive cube GameObject. This is a 2D project.

I've literally been attempting to do this all day, but I simply don't have the skill to do it. I have been attempting to use screen.width and screen.height and dividing the height by 14 and width by 12, but I cannot figure out how to place the tiles (cubes), and I have made such a mess of everything I will have to restart.

Can anyone tell me the best way to do this? Thank you for any help you can give me.

Edit: By grid, I'm referring to something like a chess board

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by robertbu · May 26, 2014 at 04:43 AM

Cubes are world objects. I find it best to place world objects on unit boundaries and adjust the camera to make things fit. Since a cube is 1x1x1, using unit boundaries will place each cube next to its neighbors. To create a bit of space, simple decrease the size of the prefab just a bit...like (0.95, 0.95, 0.95). It is difficult to precise pixel space between the objects. It involves mapping between world space and screen space, so if you can live with a world space solution, things are easier. Put this code on an empty gambe object, initialize the 'prefab' variable by drag and drop:

 #pragma strict
 
 var prefab : GameObject;
 var rows = 12;
 var cols = 14;
 
 function Start() {
     var offset = Vector3(-cols / 2.0 + 0.5, -rows / 2.0  + 0.5, 0.0 );
     
     for (var i = 0; i < rows; i++) {
         for (var j = 0; j < cols; j++) {
             Instantiate(prefab, Vector3(j, i, 0.0) + offset, Quaternion.identity);
         }
     }
 }
Comment
Add comment · Show 4 · 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
avatar image QuapPac · May 26, 2014 at 07:04 PM 0
Share

Hey, thanks for the help creating the grid. Are you sure there isn't a better way to create an even distance between each square? I don't need it to be measured in pixels, I just need a very slight break between each square.

Scaling down looks very bad, as you can see here:

http://gyazo.com/bd36d8525eadcaa0e8ea9bf4ce58b8aa

The distances are uneven, which is unacceptable.

avatar image robertbu · May 26, 2014 at 08:39 PM 0
Share

The distances are uneven because of pixel roundoff. That is, the world size of your object might be 54.23 pixels. So as the distances are rounded to whole pixels, sometimes the lines will one pixel bigger than other times. There is no easy fix for this problem. You have some choices:

  • Ins$$anonymous$$d of real world object, you could use GUI.DrawTexture() or GUI.Button() (with a custom style). GUI uses pixels.

  • You could dynamically size your objects so that the world units of each tile maps to precise number of pixels. This is easier to do with an Orthographic camera.

  • You could use a pixel perfect texture to draw the grid and size the world objects to fit the texture.

None are simple.

avatar image QuapPac · May 26, 2014 at 09:37 PM 0
Share

You've already helped me so much and I don't want to bother you any more, but would you know how to adjust an orthographic camera to show the full grid horizontally (vertically, there can be space above and below)? Since I'm making this game for Android, I'm assu$$anonymous$$g I can't just set the orthographic camera in the editor and expect it to work for all different Android devices. The camera is 720 x 1080 if it matters

Thanks

avatar image robertbu · May 27, 2014 at 04:46 PM 0
Share

Here is one link:

http://answers.unity3d.com/questions/671486/orthographic-camera-same-width-different-height.html

I wrote a better answer than this one in the last month, but a quick search did not turn it up.

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

20 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

Related Questions

A node in a childnode? 1 Answer

Mid point of a object 1 Answer

How to perform a task in a specified time 2 Answers

Create a factice/dummy 1 Answer

Unity not registering? 0 Answers


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