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 /
avatar image
0
Question by steffen86 · Jan 16, 2018 at 09:48 PM · largemap-generationobjectpool

Object pooling - Show Objects if they are close to camera position,Object Pooling - Show objects if they have short distance to camera

Hi all,

i want to create a large map with many (thousands) objects (for example simple squares with specific position and scale).

What i have so far:

  1. I have a pre filled array (lets name it SquareDataArray) with 10.000 entries. Each entry has a position Vector2 and a scale float. So loading a big array on start seems to work fine. (Other ideas are welcome :) )

  2. Then i have a Square prefab Object-Pool array which has 20 instantiated objects.

Now i move the camera over the map by changing the x and y position. During this movements i want to check if one of my entries from SquareDataArray is close to the camera position. And if so i want to display one of my Square-Prefab at this position. But looping through an array with 10.000 objects and compare its entries (by position) in the Update method of my Script seems definitely not right to me.

Since i am very new to Unity and game development i have no idea how to solve this issue. Maybe someone has an idea :)

Best regards

Steffen

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
0
Best Answer

Answer by Buckslice · Jan 17, 2018 at 01:49 AM

Hello Steffen,

You can use some form of spatial partitioning (look up this term for info on other various techniques) to reduce the amount of distance checks you need to do each frame. Here is one way you could do it:

Instead of storing your 10,000 objects in one big array, instead store them in a 2D array. Each element (or bucket) in the array will be a list containing entries of your Vector2 position and float scale structs. Each bucket will represent an area of your game world. To insert data into this structure just compute which bucket in the array it should go by dividing the x and y position by the size you want each bucket to represent. Here is some example code to help you

 struct SquareData {
     public Vector2 pos;
     public float scale;
     public SquareData(Vector2 pos, float scale){
         this.pos = pos;
         this.scale = scale;
     }
 }
 
 List<SquareData>[,] dataArray;
 int numBuckets = 10; // number of buckets in each dimension
 float bucketSize = 10.0f // each slot in array represents this much area
 
 void Start(){
     dataArray = new List<SquareData>[numBuckets, numBuckets];
     // array covers area of 100x100 units (numBuckets * bucketSize)
     
     Insert(new SquareData(new Vector2(50.0f, 25.0f), 2.0f));
 }
 
 void Insert(SquareData data){
     int x = (int)data.pos.x / bucketSize;
     int y = (int)data.pos.y / bucketSize;
 
     // should add some bounds checking on your array here
     dataArray[x,y].Add(data);
 }

I haven't tested this code but should give you an idea of where you could start at least. Then once you sort all your square data items into your array, each frame you just check which bucket in the array the player position would be (just divide player pos by bucketSize) and then only search the array in those nearby buckets based on your display distance. So if your display distance is say 20 units, you would only need to check inside the buckets within 2 elements from the players bucket. Hope this helps!

Comment
Add comment · 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
0

Answer by steffen86 · Jan 17, 2018 at 08:37 PM

Thanks @Buckslice for your detailed answer! I alomost got it :). I am going to share my solution as soon as i finished.

Comment
Add comment · 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

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

74 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

Related Questions

Making a large scale map for Unity 3D? 1 Answer

Using Very Large Numbers and Prefixes // For an Idle Game e100,e1000, and more. 3 Answers

Find inactive game objects in object-pool by tag? 2 Answers

random generated map mobile game frame rate lag 1 Answer

Blurry Texture at large size. 2 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