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 KaspaRozmer · May 18, 2020 at 07:59 PM · listlagobject pooliterate

need help stopping these lag spikes when my list iterates

Hey guys, I'm having trouble with something. I'm making a huge tile based procedural world, and using object pooling to keep the performance up. Now it works pretty well, but I have a script that iterates through every point and checks to see if it should place a tile at that spot. Here is what it looks like right now:

     IEnumerator CheckActivation()
     {
 
         
         if (activatorItems.Count > 0)
         {
             for(int i=0;i<activatorItems.Count;i++)
             {
 
                 if (Vector3.Distance(plrtransform.position, activatorItems[i].itemPos) < distanceFromPlayer)
                 {
                    
 
                     if (activatorItems[i].itemName.Contains("grasstile"))
                     {
                         objectPooler.Instance.SpawnFromPool("GrassTile", activatorItems[i].itemPos, Quaternion.identity);
                     }
                     else if (activatorItems[i].itemName.Contains("sandtile"))
                     {
                         objectPooler.Instance.SpawnFromPool("SandTile", activatorItems[i].itemPos, Quaternion.identity);
                     }
                     else if (activatorItems[i].itemName.Contains("watertile"))
                     {
                         objectPooler.Instance.SpawnFromPool("WaterTile", activatorItems[i].itemPos, Quaternion.identity);
                     }
 
 
                 }
             }
 
         }
         yield return new WaitForSecondsRealtime(1f);
         StartCoroutine("CheckActivation");
 
     }
 }


So lets say I have a 1000x1000 tiles, so its iterating over a million tiles. And every single iteration I get a lag spike. I'm just not sure how I could clean this up to make it more performance friendly

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

Answer by Hellium · May 18, 2020 at 08:35 PM

So lets say I have a 1000x1000 tiles, so its iterating over a million tiles

Are you saying that activatorItems.Count is equal to 1,000,000 ? (⊙_⊙')


A very first step would be to store the correct IDs once and reuse them in the coroutine.

 private string[] spawnIDs;
 
 private void Awake()
 {
     FillSpawnIDs();
 }
 
 void FillSpawnIDs()
 {
     if(activatorItems.Count == 0)
         return ;
 
     spawnIDs = new string[activatorItems.Count];
     for(int i = 0 ; i < activatorItems.Count ; i++)
     {
         if (activatorItems[i].itemName.Contains("grasstile"))       spawnIDs[i] = "GrassTile";
         else if (activatorItems[i].itemName.Contains("sandtile"))   spawnIDs[i] = "SandTile";
         else if (activatorItems[i].itemName.Contains("watertile"))  spawnIDs[i] = "WaterTile";
     }
 }
 
 IEnumerator CheckActivation()
 {
     if(activatorItems.Count == 0)
         return ;
 
     WaitForSecondsRealtime wait = new WaitForSecondsRealtime(1f);
     float sqrDistance = distanceFromPlayer * distanceFromPlayer;
 
     while(true)
     {
         for(int i = 0 ; i < activatorItems.Count ; i++)
         {
             if ((plrtransform.position - activatorItems[i].itemPos).sqrDistance < sqrDistance)
                 objectPooler.Instance.SpawnFromPool(spawnIDs[i], activatorItems[i].itemPos, Quaternion.identity);
         }
         yield return wait;
     }
 }


But even with this small optimisation, you will have lags with 1 million objects to process. Either do the computation over several frames, or try to implement an Octree to reduce the number of tiles to loop over.

Comment
Add comment · Show 2 · 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 KaspaRozmer · May 18, 2020 at 09:14 PM 0
Share

Thanks! How do you think I would split it up over several frames?

avatar image Hellium KaspaRozmer · May 18, 2020 at 10:02 PM 0
Share

Disclaimer: I haven't tried the following code:

 IEnumerator CheckActivation()
 {
     if(activatorItems.Count == 0)
         return ;
 
     float sqrDistance = distanceFromPlayer * distanceFromPlayer;
     int processedAmountPerSecond = activatorItems.Count; // Or replace by a fixed amount such as 1000 for instance
     int stopIndex = $$anonymous$$athf.CeilToInt(Time.deltaTime * processedAmountPerSecond);
     while(true)
     {
         for(int i = 0 ; i < activatorItems.Count ; i++)
         {
             if(i % stopIndex == 0)
             {
                 yield return null;
                 stopIndex += $$anonymous$$athf.CeilToInt(Time.deltaTime * processedAmountPerSecond);
             }
             if ((plrtransform.position - activatorItems[i].itemPos).sqrDistance < sqrDistance)
                 objectPooler.Instance.SpawnFromPool(spawnIDs[i], activatorItems[i].itemPos, Quaternion.identity);
         }
     }
 }

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

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

All GameObjects list to a GameObject? 0 Answers

Make simple(or not) score list 1 Answer

List give a count of zero when it have 1 element 1 Answer

Trying to make a list that stores multiple types of data 1 Answer

How I randomise a list of signals in Unity and remove it from the list once the clip is finished? 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