当前位置:网站首页>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
边栏推荐
- WorldView卫星遥感影像数据/米级分辨率遥感影像
- haproxy+keepalived搭建01
- An intern's journey to cnosdb
- go语言-循环语句
- 华为S5700交换机初始化和配置SSH和TELNET远程登录方法
- Pat grade a 1027 colors in Mars
- Free use until 2015 -- viz artist multi touch plug-in package
- Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
- 一条通往服务器所有端口的隧道
- Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
猜你喜欢
方正锐利重磅升级到12.0版本,包装印前处理更加便捷、高效!
Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
Free use until 2015 -- viz artist multi touch plug-in package
Oracle queries grouped by time
[cocos creator] Click the button to switch the interface
[MySQL 14] use dbeaver tool to remotely backup and restore MySQL database (Linux Environment)
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
超限黑客认知
Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
*p++、*++p、++*p、(*p)++
随机推荐
什么是定义?什么是声明?它们有何区别?
Product creation and commercial realization of chat robot (according to La Ma Bang - Dr. Wang Jingjing - speech)
Pat class a 1031 Hello world for u
[at] ABC 258g - Triangle triples reachable - violence
Pat class a 1030 travel plan
idea取消引用显示效果
华为交换机基础配置(telnet/ssh登录)
experiment.........
OSPF experiment
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
【cocos creator】获取资源uuid
What is definition? What is a statement? What is the difference between them?
璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
【LeetCode】4. Best time to buy and sell stock
P2704 [NOI2001] 炮兵阵地(状压dp)
Redis batch startup and shutdown script
VMware virtual machine configuration static IP
MAE
华为交换机配置ssh登录远程管理交换机