Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 kurukshetran · Oct 26, 2015 at 06:55 PM · 2dgrid

Generating grid pattern 2d

I generated a grid, the row starts at bottom left to right and continues to generate as 5*5 grid. But i need a little help in generating grid where pattern should be as, start row at bottom left to right, right to left. Here is the link to visual, https://dl.dropboxusercontent.com/u/102812893/grid.png How to generate this pattern. Here is my code.

 void drawGrid() 
 {
     Vector3 offset = new Vector3(-col / 2.0f + 0.5f, -row / 2.0f + 0.5f, 0.0f); //(-2, -2)
     gameObj = new GameObject[row, col];

     int a = 1, b = 0;

     for(int i = 0; i < row; i++)
     {
         for(int j = 0; j < col; j++)
         {
             gameObj[i, j] = tile;
             b = a++;
             gameObj[i, j].name = b.ToString();
             Vector3 pos = new Vector3(j*0.905f, i*0.905f, 0f) + offset;
             boardPositions.Add(pos);
             //sr.WriteLine(i + "," + j + "=" +pos);
             Instantiate(gameObj[i, j], pos, Quaternion.identity);
         }
     }
 }
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
1
Best Answer

Answer by Socapex · Oct 26, 2015 at 07:28 PM

1 option) You could store your j ints in a List, and after iterating on them, reverseSort or Sort the list. This would probably require a bool that you set as bool = !bool to know which sorting you want (ascending or descending). With this in mind...

2) You could initiate 2 arrays of j ints, one ascending the other descending, use a bool to switch which one you loop on. This way, you don't apply a sorting algorithm (which is slow) every loop, but only once at the beginning.

Hope this helps.

Comment
Add comment · Show 5 · 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 kurukshetran · Oct 26, 2015 at 07:56 PM 0
Share

Actually i tried to solve. Here is the code. But the output is different, where start from bottom left to right, row 0, row 2, row4 is generated correctly. But row 1, row3 adds extra one gameobject. Here is the visual image. link text

Here is the code.

     void generateBoard()
 {
     Vector3 offset = new Vector3(-col / 2.0f + 0.5f, -row / 2.0f + 0.5f, 0.0f); //(-2, -2)
     gameObj = new GameObject[row, col];
     int a = 1, b = 0, c = 0;
     int $$anonymous$$AX = row * col;

     for(int i = 0; i < row; i++)
     {
         if(i % 2 == 0)
         {
             for (int j = 0; j < $$anonymous$$AX / row; j++) 
             {
                 boardTile.Add (tile);
                 c = a++;
                 boardTile[b].name = c.ToString();
                 Vector3 pos = new Vector3(j*0.905f, i*0.905f, 0f) + offset;
                 boardPositions.Add(pos);
                 Instantiate(boardTile[b], pos, Quaternion.identity);
                 b++;
             }
         }
         else
         {
             for (int j = $$anonymous$$AX / row; j >= 0; j--)
             {
                 boardTile.Add (tile);
                 c = a++;
                 boardTile[b].name = c.ToString();
                 Vector3 pos = new Vector3(j*0.905f, i*0.905f, 0f) + offset;
                 boardPositions.Add(pos);
                 Instantiate(boardTile[b], pos, Quaternion.identity);
                 b++;
             }
         }
     }
 }
avatar image Socapex kurukshetran · Oct 26, 2015 at 08:04 PM 0
Share

j >= 0?

;)

avatar image kurukshetran Socapex · Oct 26, 2015 at 08:08 PM 0
Share

yeah i changed that condition to j > 0. Please have a look at the result.

link text

Show more comments
Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Get position from Isometric TileMap 1 Answer

Block puzzle (1010) help 0 Answers

Creating a 2d array with the Array class 1 Answer

2D Grid Question 0 Answers

How to make 2d distortion? 2 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