当前位置:网站首页>Unity performance optimization
Unity performance optimization
2022-07-03 07:57:00 【qq_ two billion three hundred and eighty-five million seven hun】
One 、 Optimization premise
1,unity Installation package , Operation card
Built in mono virtual machine
C# stay mono Running on a virtual machine ,mono Virtual machines are cross platform
2,DrallCall Draw calls , performance GPU,CPU, and GPU Rendering related
CPU The call of graphic drawing interface ,CPU Through the graphics library interface , command GPU Rendering
DrallCall The bigger it is , The more performance consumption , Cause game running card
CPU processor , Equivalent to our brains , Perform logical processing calculation
GPU Specially for game drawing , Scene character model , A lot of preparation will be done before each drawing
Reduce CPU Yes DrallCall Call to ,drallcall many ,CPU Be prepared for extra expenses
3,Profiler Viewing performance , stay Window In form
4, Statistics panel ,game Mode of stats
FPS frame per second , The larger the number of frames, the smoother , exceed 30 Frame the human eye can't feel the card
CPU, Time consumed per frame , One second is equal to 1000 millisecond
render thread Rendering is determined by the graphics card
Batches: The batch ,
Verts: Number of vertices in camera field
Tris : The number of triangles in the camera's field of view
setpasscall:Shader There will be many Pass,GPU Running pass Before , Will produce a SetPass
Two 、 resource optimization
1, Resource optimization criteria
mesh The number of patches of the dynamic model <3000, texture of material <3, Number of bones <50,
Static model : Number of vertices <500
Long time music ( The background music ) Compressed format mp3
Short music ( Sound effect ) Uncompressed format wav, No compression , Repeats, , decompression

Short music tick first , Long music tick the second
Texture The length and width of the map are less than 1024
shader Reduce complex operations , Reduce discard operation
2, Model optimization , Reduce the number of faces , Map optimization ( Map merging )
3, Reduce redundant and duplicate resources
A : Resource Resources in the directory, whether referenced or not , Will be packaged into the installation package
Don't put unused resources in Resource Under the table of contents
B : The same resource files in different directories , If they are all quoted , Will get into the bag
Guarantee the same Resource files are stored in only one directory location in the project
4, Detection and analysis of resources
UWA Tools , Optimize the size of the installation package , Optimize the performance of the installation package
3、 ... and 、GPU Optimize
Scene rendering , Light treatment , Rendering of model character scene
1, Level detail LOD Optimize , The closer you see, the finer , LOPGroup Components

2, Occlusion culling occlusionculling, Specific operation sikiA Plan optimized video 10 Class hour

3、 The light map , To be specific, see siki Optimize 11 Class hour
When there are many light sources , Calculate the points projected by multiple light sources in advance , Use map
Select all game objects

4、Mesh Merge
Combine several models into one model , Combine several scenes into a large model , Put the model into the scene
void MeshCombine()
{
MeshFilter[] filters = GetComponentsInChildren<MeshFilter>();
// A specially merged class
CombineInstance[] combiners = new CombineInstance[filters.Length];
for(int i = 0; i < filters.Length; i++)
{
combiners[i].mesh = filters[i].sharedMesh;
// Change coordinates
combiners[i].transform = filters[i].transform.localToWorldMatrix;
}
// After the merger mesh
Mesh finalMesh = new Mesh();
// Merge
finalMesh.CombineMeshes(combiners);
GetComponent<MeshFilter>().sharedMesh = finalMesh;
}Four 、CPU Optimize
1, Numerical operation , damage , random number , The enemy AI, Dealing with logic
If there are many small objects in the game , Try to combine these small objects into one mesh, It also reduces the number of materials
Multiple small objects CUP I will prepare many times
Use fewer materials , Combine multiple maps into a large map
When merging objects , These objects need to use the same material to have an effect
Merge complete mesh, If the number of materials used does not decrease , It's no use ,mesh And maps should be merged
2, Object pool , You need to create a pool of destroyed objects frequently
Reduce instantiation , The process of destruction , Disable it when not in use ;
// Bullet resource pool
public class BulletPool : MonoBehaviour {
public int poolCount = 30;
public GameObject bulletPrefab;
private List<GameObject> bulletList = new List<GameObject>();
private void Start()
{
InitPool();
}
void InitPool()
{
for(int i = 0; i < poolCount; i++)
{
GameObject go = GameObject.Instantiate(bulletPrefab);
bulletList.Add(go);
go.SetActive(false);
go.transform.parent = this.transform;
}
}
public GameObject GetBullet()
{
foreach(GameObject go in bulletList)
{
if (go.activeInHierarchy == false)
{
go.SetActive(true);
return go;
}
}
return null;
}
}// The firing of a bullet
public class Shoot : MonoBehaviour {
public GameObject bulletPrefab;
private BulletPool bulletPool;
// Use this for initialization
void Start () {
bulletPool = GetComponent<BulletPool>();
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
//GameObject go = GameObject.Instantiate(bulletPrefab,transform.position,transform.rotation);
GameObject go = bulletPool.GetBullet();
go.transform.position = transform.position;
go.GetComponent<Rigidbody>().velocity = transform.forward * 50;
//Destroy(go, 3);
StartCoroutine(DestroyBullet(go));
}
}
IEnumerator DestroyBullet(GameObject go)
{
yield return new WaitForSeconds(3);
go.SetActive(false);
}
}3, Optimize compilation performance , Improve compilation speed
Put the code of the framework class that does not need to be changed frequently Plugins Under the folder ,
as long as Plugins If the code under does not change, it will not be recompiled firstPass.dll
After changing the code, it will only compile Editor and Plugin Code outside
边栏推荐
猜你喜欢

Go language foundation ----- 04 ----- closure, array slice, map, package
![[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18](/img/9b/db5fe1a37e0de5ba363f9e108310a5.png)
[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18

Technical dry goods Shengsi mindspire dynamic transformer with variable sequence length has been released!

Go language foundation ----- 18 ----- collaboration security, mutex lock, read-write lock, anonymous lock, sync Once

Zohocrm deluge function application time verification

VMware virtual machine configuration static IP

【LeetCode】3. Merge two sorted lists · merge two ordered linked lists

STM32F103 SPI (pit Diary)

Redis batch startup and shutdown script

haproxy+keepalived集群搭建02
随机推荐
PHP常用排序算法
How to configure GDAL under idea
MAE
register关键字
[cocos creator] Click the button to switch the interface
华为交换机配置ssh登录远程管理交换机
Research shows that breast cancer cells are more likely to enter the blood when patients sleep
Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
Introduction of novel RNA based cancer therapies
【LeetCode】4. Best time to buy and sell stock
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
static关键字
jsutlis
Are you still watching the weather forecast on TV?
华为交换机:配置telnet和ssh、web访问
OSPF protocol summary
experiment.........
HDMI2.1与HDMI2.0的区别以及转换PD信号。
华为交换机Console密码重置、设备初始化、默认密码
[at] ABC 258g - Triangle triples reachable - violence