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 keinabel · Jun 28, 2013 at 01:35 PM · terrainperformancetreeplacement

Remove Trees during runtime

Hi there,

I have a smiluation and want to build structures in-game. And I want the surrounding trees to be removed when placing a building nearby. Problem is that my current solution actually does remove the trees within a certain radius. But the game freezes for a few seconds, since there are so many trees.

The code looks similar to this:

 TreeInstance[] trees = Terrain.activeTerrain.terrainData.treeInstances;
 ArrayList newTrees = new ArrayList();
 Vector3 terrainDataSize = Terrain.activeTerrain.terrainData.size;
 Vector3 activeTerrainPosition = Terrain.activeTerrain.GetPosition();
 float distance;
 
 foreach (TreeInstance tree in trees )
 {
 
     distance = Vector3.Distance(Vector3.Scale(tree.position, terrainDataSize)
                     + activeTerrainPosition, currentObject.transform.position);
 
     if (distance > 20) {
         newTrees.Add(tree);
     }
 }
 Terrain.activeTerrain.terrainData.treeInstances = (TreeInstance[])newTrees.ToArray(typeof(TreeInstance));


do you have any suggestions how I might be able to do this faster?

edit: just realised, that when I restart the game after removing trees, they are still gone?! How can I prevent Unity from doing this?

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
0

Answer by Stormizin · Jun 28, 2013 at 06:31 PM

This really depends on the user's computer. It's too many objects getting destroyed at same time. Getting lots of memory free.

Did you tested with the scene already builded?

Comment
Add comment · Show 6 · 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 · Jun 29, 2013 at 11:44 AM 0
Share

Works better. At least on my machine. But is there no better-perfor$$anonymous$$g way to remove the trees? There has to be one..

avatar image Stormizin · Jun 29, 2013 at 03:50 PM 0
Share

All that i know is, that you can put the trees in memory and hold. Since you call the destroy method maybe the performance will improve.

Also you can try Builtin arrays, they are very fast. $$anonymous$$G:

 public class example : $$anonymous$$onoBehaviour {
     private Vector3[] positions;
     void Awake() {
         positions = new Vector3[100];
         int i = 0;
         while (i < 100) {
             positions[i] = Vector3.zero;
             i++;
         }
     }
 }
avatar image keinabel · Jun 30, 2013 at 10:58 AM 0
Share

Problem is, that I don't know the size the array will have. :/

avatar image Stormizin · Jun 30, 2013 at 01:41 PM 0
Share

Your code need to know how many trees will be destroyed. Use a var that will hold this count then execute the builtin array.

avatar image keinabel · Jun 30, 2013 at 02:25 PM 0
Share

Yea, but that's the point. $$anonymous$$y code won't know before he iterates over all the TreeInstances.

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

17 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

Related Questions

Why is my Tree Removal so low-performing when placing a GameObject nearby 2 Answers

Terrain Tree Placement 0 Answers

How to improve the Performance of Removing Trees during runtime? 1 Answer

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