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 TomatJuice · Aug 05, 2020 at 12:19 AM · 2drenderingperformanceperformance optimization

How To Disable Gameobjects When Not In View, Then Enable When In View

I am working on a 2D game that generates a world and then generate foliage on top of the world that can be broken. Since the world is 1000 by 1000 tiles there is a lot of foliage to be spawned. It slows down the frame rate by a lot even with the SpriteRender turned off. If there a performance efficient way to load nearby foliage while disabling ones out of view then your answer will be much appreciated. I have already tried placing each gameobject in a list then checking the distance but that's a lot of objects to process in a foreach loop.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by davidcox70 · Aug 05, 2020 at 08:12 PM

I would probably make my tiles in a 2 dimensional array, which then makes it easy to reference them. I would also keep track of the tiles I had made visible by putting them in a list. Something like:

 public class makeSomeTiles : MonoBehaviour
 {
     private GameObject[,] tiles;
     public GameObject tileObject; //drag a base tile object into here in the editor
     public int numTilesWide = 100; //the width of world in tiles
     public int numTilesHigh = 100; // the height of our world in tiles.
     private List<GameObject> activeTiles;
 
 
 
     void Start()
     {
         // create an array big enough to take your entire scene
         tiles = new GameObject[numTilesWide, numTilesHigh];
 
         // hide the source original tile
         tileObject.SetActive(false);
 
         // we'll use this list later to hold activated tiles
         activeTiles = new List<GameObject>();
 
         // run the  make tiles routine
         makeTiles(numTilesWide, numTilesHigh);

         // just activate the ones we want - say 5 x 5 around the tile located at 60,30
         Vector2 playerTile = new Vector2(60, 30);

         // and do this whenever we detect the player is on a new tile.
         activateArea(playerTile, 2);
     }
 
 
     void makeTiles(int width, int height)
     {
         // count through the rows and columns
         for (int y = 0; y < height; y++)
         {
             for (int x = 0; x < width; x++)
             {
                 // create copies of the base tile template
                 tiles[x, y] = GameObject.Instantiate(tileObject);
 
                 // name the tile, just because :-)
                 tiles[x, y].name = "Tile #" + ((y * width) + x).ToString();
 
                 //position the tile
                 tiles[x, y].transform.position = new Vector2(x, y);
             }
         }
 

     }
 
 
     void activateArea(Vector2 centerTile, int tileRange)
     {
 
         //clear the old active tiles (if any)
         foreach (GameObject tileRef in activeTiles)
         {
             tileRef.SetActive(false);
         }
 
         //clear the active tiles list
         activeTiles.Clear();
 
         // activate the tiles around the center tile
         for (int x = -tileRange; x <= tileRange; x++)
         {
             for (int y = -tileRange; y <= tileRange; y++)
             {
                 // offset to center tile
                 Vector2 tileToActivate = centerTile + new Vector2(x, y);
 
                 // limit the output, because we'll get an error if we are at the edge of the world
                 int xCoordinate = (int)Mathf.Clamp(tileToActivate.x, 0, numTilesWide - 1);
                 int yCoordinate = (int)Mathf.Clamp(tileToActivate.y, 0, numTilesHigh - 1);
 
                 // make the tile visible
                 tiles[xCoordinate, yCoordinate].SetActive(true);
 
                 // add the tile to a list which we can use to hide the tile later.
                 activeTiles.Add(tiles[xCoordinate, yCoordinate]);
 
             }
         }
     }
 
 
 }



This script makes all the tiles at the start. You might prefer to make them as the user approaches. It depends how long you want to wait for startup.

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 TomatJuice · Aug 05, 2020 at 09:46 PM 0
Share

On the world size that i wanted 1000 by 1000(1,000,000 tiles) it was running at 3 frames but with your amazing system that have everything i needed it was running at 200 frames. Thank you for helping me make my game run faster

avatar image davidcox70 TomatJuice · Aug 05, 2020 at 10:37 PM 0
Share

Glad to hear it :-)

avatar image
0

Answer by Ymrasu · Aug 05, 2020 at 05:38 AM

MonoBehaviour objects have some messages that you could use: OnBecameVisible() and OnBecameInvisible(). So you can add something like this to a script:

 void OnBecameVisible()
 {
     enabled = true;
 }
 
 void OnBecameInvisible()
 {
     enabled = false;
 }

The problem is that it counts the editor Scene camera too. So it could throw you off while testing.

Comment
Add comment · Show 11 · 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 TomatJuice · Aug 05, 2020 at 12:54 PM 0
Share

Each plant doesn't have a mono behavior script as that would be a lot of mono behaviors. I looked in the profiler and can see that rendering the sprites is causing the frame drops

avatar image Ymrasu TomatJuice · Aug 05, 2020 at 01:12 PM 0
Share

It would just be one small script that you could attach as a component to anything you want to enable and disable itself when in and out of view. I think it would help more than it would hurt performance.

avatar image TomatJuice Ymrasu · Aug 05, 2020 at 01:34 PM 0
Share

I ran a test by generate a 200 by 200 tile world with the same seed. The first test had the mono behavior on each plant and was averaging around 370fps. The second test took off the mono behavior on each plant and was averaging around 430fps. Also the frames are high right now only because the world is really small.

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

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

slow rendering and frozen frames in unity 2019.4.16f 3 Answers

Do non-rendered gameobjects use memory? 1 Answer

Is there a way to disable the IK Manager on playtime without impairing the animation? 0 Answers

gfx.waitforpresent took up to 40ms even without any gameobject running 0 Answers

Android & IOS Performance Difference 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