1) How to locate the hot problem of the game 2)Unity Get the reference object of the specified script 3) How to know a when packing Shader How many variants 4) How to optimize Font.CacheFontForText Time consuming peaks caused by frequency
This is the first 300 piece UWA Push of technical knowledge sharing . Today we continue to select and develop a number of 、 Optimization related issues , Suggested reading time 10 minute , If you read it carefully, you will get something .
UWA Question answering community :answer.uwa4d.com UWA QQ Group 2:793972859( The original group is full )
Performance
Q: The fever problem of the current project is a headache ,2D game , be based on TileMap、SpriteRenderer and UGUI Rendering of , Vertical synchronization is turned off ,TargetFrameRate Set to 60.Android and iOS The fever is very serious , And in better models ( such as iPhone 12 such ), Fever is even more obvious .
Unlike most cases , Heating doesn't affect frame rate much , On most models , Frame rate is not a problem , even iPhone 8 Model , can 60 Frame full .
stay Unity Profile and Xcode Performance analysis has been carried out ,CPU The most obvious hotspot function is Spine Bone animation update calculation for . However, to a scene without bone animation , The fever is a little better , But it is still much hotter than expected ( In addition to the ground and some static maps in the scene , There are not many things ).
Another popular hot spot is network testing , After shutting down the network in a certain scenario , Fever is still serious . Even start the game , Stay in the login screen for a while , Fever is more obvious than other games .
Guess if it is caused by too many vertices per frame , In the game Unity Of Status panel , Number of vertices Verts Reached 40KB, triangle Tris There are also almost 20KB. It looks like a lot , But I don't know the level of current mainstream games . and , The number of vertices and triangles is hard to explain, and the login interface is still hot , After all, the login interface can not be very high .
I have also done a lot of tests , I really don't know what the problem is ,Unity Is there anything else that needs to be optimized , For hot spots ?
A1: It can be used Xcode Grab the frame to see the bandwidth ,Load Store Action Is it reasonable? .
iPhone 8 Running full 60 frame , prove CPU、GPU Have not reached the bottleneck , Consumption in a more reasonable range . Recommended in the Unity Profiler to glance at CPU End consumption , And check it out DrawCall Number .
Recommended FrameDebugger, See if there are redundant objects or post-processing in rendering , In particular, you said that the startup interface is hot .
If all the low-level errors have been checked , It is suggested to check whether the native plug-ins are used .
Whether the rendering resolution has been adjusted ,RenderScale The value of and FrameDebugger You can find the resolution .
Thank you zhangzhendong @UWA The Q & a community provides answers
A3: About fever , Usually we should check from several angles :CPU pressure ( Time consuming )、GPU pressure ( Time and bandwidth , Consider reducing the resolution to see if the heating problem will be improved ) and IO Wait a few angles . From the point of view of the subject , Time consuming should be no problem , Can run full frames ( Of course 60 The frame itself has a great influence on the heating , Look at the limitations 30 Will the frame heat up or drop ), So let's see if there is a problem with some invisible things . For example, bandwidth , It can be used Snapdragon Run on Qualcomm mobile phones . If the bandwidth is high , See if some of the texture settings are reasonable , For example, whether to compress 、 Open or not Mipmap, These two items usually need to be set to on . You can also see if there is any unnecessary BlitCopy operation , stay URP It is easy to appear in the project Copy Color and Copy Depth waste . about IO, You need to check whether there are sub threads that have frequent IO The phenomenon of .
Q:Unity Get the reference object of the specified script : One GameObject A... Is mounted on the Script, This Script A lot of resources are cited . How to just get this Script The resources cited ?AssetDatabase Will get all the references , But there is no distinction .
A1: Yes Guid You can use the following code to load : var assetPath = AssetDatabase.GUIDToAssetPath(guid); var texture2D = AssetDatabase.LoadAssetAtPath(assetPath);
Thank you Xiao Xiaojun @UWA The Q & a community provides answers
A2: adopt SerializedObject Got all the ObjectReference, then AssetDatabase.GetAssetPath Get the corresponding path :
var assetObj = AssetDatabase.LoadAssetAtPath<Object>(path); if (assetObj != null) { GameObject gameObj = assetObj as GameObject; if (gameObj != null) { MeshRendererTextureStreamingData streamingOBJ = gameObj.GetComponent<“taget component”>(); if (streamingOBJ != null) { SerializedObject so = new SerializedObject(streamingOBJ); SerializedProperty it = so.GetIterator(); bool first = true; while (it.Next(true)) { if (it.propertyType == SerializedPropertyType.ObjectReference && it.objectReferenceValue!=null) { // Debug.LogError(it.objectReferenceValue); var depPath = AssetDatabase.GetAssetPath(it.objectReferenceValue); streamingAssets.Add(depPath); } } } } }
Q: Is there any way to know one of them when packing Shader How many variants ?
A: Use it directly Unity The callback function provided , This function will be in Shader Packed ( Whether it's fighting AssetBundle Package or Build When ) Automatically call .
The three parameters shown in the figure :“shader” Indicates that the callback function is currently processing Shader resources ,”data” Is a list of variants involved in compilation . therefore data.count() It's the time to Shader Resources ( Those involved in compiling ) The number of variants . therefore , Just put these two parameters in the callback function logic Log Just output .
Font.CacheFontForText Is in Text When rendering , When the corresponding font FontTexture When no character to render is found , It will regenerate FontTexture,FontTexture The bigger the size , Regenerate this FontTexture The more time it takes .
Therefore, in order to reduce the time-consuming peak caused by this function during operation , You can render the required characters in advance , Avoid regenerations at run time FontTexture.
Use UWA Of GOT Online The tool validates the solution mentioned above , The results are compared as follows :
Before optimization
After optimization
There are still some problems after optimization Font.CacheFontForText Time consuming peak , It's because before the post test rendering FontTexture Characters not in .
Today's sharing is here . Of course , There is no end to life but to know . In the long development cycle , The problems you see may be just the tip of the iceberg , We are already in UWA The Q & a website has more technical topics waiting for you to explore and share together . You are welcome to join us , Maybe your method can solve other people's urgent needs ; And he's a mountain “ stone ”, Can also attack you “ jade ”.