1. When using an array or arraylist object, note that
[CSHARP]View plaincopy
- Length = myarray. length;
- For (INT I = 0; I <length; I ++)
- {
- }
Avoid
[CSHARP]View plaincopy
- For (INT I = 0; I <myarray. length; I ++)
- {
- }
2. If you do not need to process each frame, you can process it every few frames.
[CSHARP]View plaincopy
- Void Update () {If (time. framecount % 6 = 0) {dosomething ();}}
3. You can use the invokerepeating function to implement regular and repeated calls. For example, you can run the dosomething function once every 1 second after 0.5 seconds:
[CSHARP]View plaincopy
- Void start ()
- {
- Invokerepeating ("dosomething", 0.5f, 1.0f );
- }
4. Use less temporary variables, especially in functions called in real time such as update ongui.
[CSHARP]View plaincopy
- Void Update ()
- {
- Vector3 Pos;
- Pos = transform. position;
- }
You can change it:
[CSHARP]View plaincopy
- Private vector3 Pos;
- Void Update ()
- {
- Pos = transform. position;
- }
5. Take the initiative to recycle garbage
[CSHARP]View plaincopy
- Void Update ()
- {
- If (time. framecount % 50 = 0)
- {
- System. gc. Collection ();
- }
- }
6. Optimize mathematical operations to avoid float and use int, especially in mobile games. Use as few complex mathematical functions as possible, such as sin and cos. Change Division/to multiplication. For example, use X * 0.5f instead of X/2.0f.
7. compress the mesh
After importing a 3D model, it is best to enable Mesh Compression without affecting the display effect. Off, low, medium, and high options can be selected as appropriate. It is best to use one material for a single mesh.
8. Minimize Tris and draw CILS during runtime
During preview, you can click stats to view the overhead of graphic rendering. Pay special attention to the Tris and draw CILS parameters. In general, Tris should be kept below 7.5 kb, and draw cballs should be kept below 35.
9. Avoid extensive use of built-in mesh such as sphere provided by unity.
In the built-in mesh of unity, the number of polygon is relatively large. if the object is not particularly smooth, you can import other simple 3D models instead.
10. If possible, disable unnecessary scripts on the gameobject. If you have a big scene in your game and the enemy's location is several kilometers away, you can disable your enemy's AI scripts until they are close to the camera. A good way to enable or disable gameobject is to use setactiverecursively (false) and set the spherical or box-type collision to trigger.
11. Delete the empty update method. When a new script is created through the Assets Directory, the script contains an update method, which is deleted when you do not use it.
12. reference the most logical component of a game object. Someone may write somegameobject like this. transform, gameobject. rigidbody. transform. gameobject. rigidbody. transform, but it does some unnecessary work. You can reference it at the beginning, like this:
Privatetransform mytrans;
Void start ()
{
Mytrans = transform;
}
13. Collaboration is a good method. You can use a program to replace the method that does not have to be executed every frame. (The invokerepeating method is also a good method to replace update ).
14. Try not to use the search method (such as gameobject. Find () in update or fixedupdate. You can get it in the Start method as before.
15. Do not use sendmessage or other methods. It is 100 times slower than the direct call method. You can directly call the method or use the C # Delegate.
16. When using JavaScript or boo language, you 'd better determine the type of the variable instead of dynamic type, which will reduce the efficiency. You can use # pragmastrict at the beginning of the script to check the efficiency, in this way, when you compile your game, there will be no inexplicable errors.