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
2
Question by keinabel · Jul 09, 2013 at 06:35 PM · terrainperformancelisttreeperformance optimization

How to improve the Performance of Removing Trees during runtime?

Hi there,

do you see any way how to improve the performance of this method? The moment it reaches the data.treeInstances = _treeList.ToArray(); the game freezes for 1-3 seconds. And I don't want to imagine how long it will freeze on my team member's PCs...

Some explanaition:

  • there are around 7000 trees, created with help of the terrain tool

  • the trees that shall be removed were calculated in another method and added to the list of the area included the trees.

    public static IEnumerator RemoveTrees() {

       TerrainData data = Terrain.activeTerrain.terrainData;
         TreeInstance[] treeInstances = data.treeInstances;
     
         List<TreeInstance> treesForRemoval = new List<TreeInstance>();
     
         foreach (BuildingArea area in _buildingAreas)
         {
             yield return new WaitForEndOfFrame();
             if (area.IsBought == true)
             {
                 if (area.HasTrees == true)
                 {
                     treesForRemoval.AddRange(area.treeRemovalList.ToArray());
                     area.HasTrees = false;
                 }
             }
         }
         // remove trees
         _treeList.RemoveAll(x => treesForRemoval.Contains(x));
         yield return new WaitForEndOfFrame();
     
         // replace trees on terrain
         data.treeInstances = _treeList.ToArray();
     
         yield return null;
     }
    
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 SchecterKing · Jul 09, 2013 at 07:08 PM

Isn't that what billboarding is for? I can't recall the name of the article on wiki that describes this, but try this: Divide your map into 4 sections using cubes with a transparent texture on them. Add a script to it and use the "OnTriggerEnter()" "OnTriggerExit()" functions in the unity API after selecting the cubes as triggers in the inspector panel. Continue to divide each cube inside itself by 4 sections. You can also use a memory profiling tool to determine where the cubes need to go based on memory usage of your map in those locations.

OnTriggerEnter: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html OnTriggerExit: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerExit.html

Comment
Add comment · Show 3 · 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 keinabel · Jul 10, 2013 at 11:07 AM 0
Share

actually I do not know what you are telling me :x

avatar image AlucardJay · Jul 10, 2013 at 12:55 PM 1
Share

That comment made me smile =]

I think there is some confusion here, so here's what I think the question is :

Imagine the terrain as a forest. Then someone comes along and chops a group of trees down to build a settlement. So the trees are removed from the terrain data. The problem I think is the above code is taking a long time to remove the tree instances from the terrain data.

$$anonymous$$y guess is the problem is arising from the loop that is rewriting the whole tree instance

 _treeList.RemoveAll(x => treesForRemoval.Contains(x));

As a test, you could put a debug before every yield to see where it starts to lag.

I have no desire to downvote this answer, whoever upvoted has also misconstrued (unless I've got it wrong), but this is just to express this is an answer to totally a different question =]

avatar image keinabel · Jul 10, 2013 at 03:57 PM 0
Share

measuring time didn't help me at all :(

 time = Time.time; // debug
 
             // actually replace trees
             data.treeInstances = _treeList.ToArray();
 
             Debug.Log("remove end " + (Time.time - time)); // debug

all the 4 loop results are around 0.29 seconds. The one with the _treeList.RemoveAll(x => treesForRemoval.Contains(x)); was 0.33 (at least console says so...) and the code section above is 0

Since I commented the section above out, and the lag still happened, I think you might be right. The reason for this lag is probably the RemoveAll thing. But do you have any suggestion how to speed 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

18 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

Related Questions

Remove Trees during runtime 1 Answer

To what extent can the tree system be used instead of the details system 0 Answers

List Performance Question 2 Answers

Replacing terrain trees with prefabs 0 Answers

Tree Billboards 1 Answer


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