- Home /
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:
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 :) )
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
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!
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.