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 /
  • Help Room /
avatar image
0
Question by awplays49 · Feb 19, 2016 at 03:46 PM · optimizationgenerationfor-loopforeachoptimize

How can I not load all this map generation at once?

I have a map generation system, but I want it to optimize so that maybe it doesn't load it all at once. For instance a render distance. Or maybe a flood algorithm that takes the players position, rounds it to the nearest chunk and expands. Im really not sure of the best way to do this, so Im open to suggestions. the way im doing it lags if the player falls or moves fast, because it's based on distance from each chunk.

Code In update:

 for (int x = 0; x < generatedChunks.GetLength (0); x ++)
         {    
             for (int y = 0; y < generatedChunks.GetLength (1); y ++)
             {
                 if (generatedChunks [x, y])
                 {
                     if (Vector3.Distance (player.transform.position, chunks [x, y].transform.position) <= renderDistance)
                     {        
                         chunks [x, y].SetActive (true);
                     }
                     else
                     {    
                         chunks [x, y].SetActive (false);
                     }
                 }
                 else
                 {
                     int tileX = chunkSize * chunkCountX;
                     int tileY = chunkSize * chunkCountY;
                     Vector3 chunkPos = new Vector3 (x * chunkSize - tileX / 2 + chunkSize / 2, y * chunkSize - tileY + chunkSize / 2, 0);
                     if (Vector3.Distance (player.transform.position, chunkPos) <= renderDistance)
                     {        
                         GenerateChunk (x, y, tileX, tileY, chunkPos, chunkContainer, true);
                     }
                 }
             }
         }
Comment
Add comment · Show 4
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 Khozo · Feb 19, 2016 at 03:51 PM 1
Share

$$anonymous$$aybe use yield to pause the code every now and then, so it'll only load a certain amount per frame ins$$anonymous$$d of everything in 1 frame.

avatar image awplays49 · Feb 19, 2016 at 04:12 PM 0
Share

Hi @$$anonymous$$hozo,

I see a few problems with this. If you could clarify how to overcome these problems Id be happy to accept an answer.

If you make a yield, at some times you may see an unrendered part of the world. However if you dont you see everything but it's very unoptimized.

I hope you can get back to me on this, thanks!

PS should I even bother with keeping the unrender part? should everything just stay when It loads? would it make a big difference?

avatar image Khozo awplays49 · Feb 19, 2016 at 04:25 PM 0
Share

Hi @awplays49,

$$anonymous$$ake it so it yields after it's looped a certain amount of time, for example

 if(y == $$anonymous$$athf.Round(y/30)*30)) {
 yield 0;
 }

This will yield the code every 30 chunks it's loaded. Change the number 30 to any number that suits the project. If there's still unrendered parts of the map then increase the number 30 to a bigger number.

avatar image awplays49 Khozo · Feb 19, 2016 at 05:14 PM 0
Share

I dont see a difference in the generation :/

 IEnumerator UpdateWorld () {
         for (int x = 0; x < generatedChunks.GetLength (0); x ++)
         {    
             if (x == $$anonymous$$athf.Round (x / 30) * 30)
             {
                 yield return 0;
             }
             for (int y = 0; y < generatedChunks.GetLength (1); y ++)
             {
                 if (y == $$anonymous$$athf.Round (y / 30) * 30)
                 {
                     yield return 0;
                 }
                 if (!generatedChunks [x, y])
                 {
                     int tileX = chunkSize * chunkCountX;
                     int tileY = chunkSize * chunkCountY;
                     Vector3 chunkPos = new Vector3 (x * chunkSize - tileX / 2 + chunkSize / 2, y * chunkSize - tileY + chunkSize / 2, 0);
                     if (Vector3.Distance (player.transform.position, chunkPos) <= renderDistance)
                     {        
                         GenerateChunk (x, y, tileX, tileY, chunkPos, chunkContainer, true);
                     }
                 }
             }
         }
         isUpdatingWorld = false;
     }

0 Replies

· Add your reply
  • Sort: 

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

35 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 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

URP post processing = fps drop (low and mid-end mobile devices) 1 Answer

How to optimize a PC game? 1 Answer

Comparing distance of vector 3's on a dynamic growing list 0 Answers

Optimization: how to do a checking less often than within an Update 2 Answers

Debris particles slowing down simulation 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