Unity Batch Loading Player Images

Demand background

The upper limit of the game room can have a maximum count of people. When the user enters the room, he needs to go to the remote server to obtain the clothing configuration of the count characters. After the acquisition is successful, load them one by one.
The current solution is to get the configuration of all characters at once, and then do it in a way that loads almost simultaneously.
In the worst case, this solution will have count Replacement tasks running at the same time, resulting in CPU Preemption between count tasks, and the result is that it looks stuck.
In order to make the performance of the game more smooth, it is necessary to design a plan for prioritized batch loading.

Expected effect

  • Whenever the user’s own image configuration is successfully obtained, it will be loaded first immediately.
  • Enter the queue after other user images are returned for batch loading.
  • Other users preferentially load images inside the visual area.
  • If both are inside the visible area, then prioritize images that are closer to the camera.
  • With dynamic adjustment capability, n images can be loaded at the same time at the beginning. If the average loading time is detected to be too long, dynamically lower the upper limit of concurrent tasks, with a minimum of not less than m. If the average time is within an acceptable range, try to increase the number of concurrency.

Implementation plan

The user’s own image loading is not subject to priority control and is loaded immediately after acquisition.

Priority queue

After analyzing the requirements and expected effects, we can easily think of the priority queue data structure.
Although the priority queue is called a queue, it can be implemented using various data structures. We can use heap, stack, and queue.
For our scene, our high-frequency use of operations in addition to push and pop, the most commonly used should be one-time out of the top n, from this point of view, we should use the queue way to achieve.
It just so happens that although C #does not have a default implementation of priority queues, there is a SortedList that can be used.

Priority algorithm

The most important point of the above algorithm is to calculate the priority.
Based on our considerations:

  • The internal priority of the visible area must be higher than that of the invisible area.
  • The distance between the characters will not exceed 1000.
    We can use the int type as Priority, calculate the distance between the character and the camera, and then use 1000 to subtract the distance as the initial priority, and then judge if it is in the but area, the priority is added to 1000, otherwise add 0;
    Calculating the distance between two points is simple, so the problem is how to determine whether an object is in the visible area.

How to determine if it is in the visible area

https://blog.csdn.net/cyf649669121/article/details/110580986
Since we need to judge which image to render first, we can only use coordinates to judge

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public bool IsVisableInCamera
{
get
{
Camera mCamera = Camera.main;
Vector3 pos = transform.position;
//Convert to viewing angle coordinates
Vector3 viewPos = mCamera.WorldToViewportPoint(pos);
//z < 0 means behind the camera
if (viewPos.z < 0) return false;
//Too far! Can't see it anymore!
if (viewPos.z > mCamera.farClipPlane)
return false;
//x, y values outside 0~ 1 time table outside the perspective range;
if (viewPos.x < 0 || viewPos.y < 0 || viewPos.x > 1 || viewPos.y > 1) return false;
return true;
}
}