The Efficiency Test of using transform and gameObject directly in Unity, unitygameobject
It is better to believe in books than to have no books, not to mention the blogs of online developers. My blog is also included here. Sometimes I can look back and see what is wrong, but I am too lazy to change it. In many cases, the article we see is not necessarily the original address, but it is converted by seven to eight websites, making it even more impossible to ensure accuracy.
Here we tested this statement: "Do not directly use transform or gameObject in the script, but cache it as a member variable such as _ transform and _ gameObject at the beginning for access. Because the former needs to obtain the corresponding components each time it executes, the efficiency is low ."
This statement cannot be completely false. However, after testing and understanding the principle, you will have a clearer understanding of this problem and similar problems.
First, it is reasonable to use GetComponent to cache the member variables at the beginning when using components. However, if tansform and gameObject are such common objects, Unity should have better optimization. Otherwise, it seems too low. The same is true for my test results. If you don't want to look at the subsequent analysis, just look at the conclusion.
Conclusion: the efficiency of direct access to transform in Unity is a little slower than that of cached member variables, which is only reflected when the magnitude is very large. Unity has enough optimization to transform and gameObject. Do not simulate Unity write attributes on your own. In most cases, the efficiency is worse.
See:
The test code is a simple transform coordinate assignment with a cycle of times.
Among them, the Unity attribute is the time consumption for direct access to transform, and the cached variable is the member variable version. It can be seen that the member variable directly cached is indeed faster, but the performance difference is about 10% when it is executed times. I personally feel that this difference has little impact. Whether to directly use unity attributes or cache member variables is a habit.
Note that the following two attributes are used for judgment and the attributes are used to directly obtain the variables. If _ transform is empty, the corresponding component is obtained. Otherwise, the member variable is directly returned. This is the slowest "attribute determination". In this case, the attribute will execute relatively complex logic, and the slow speed is reasonable. If a property directly returns a member variable, the efficiency is the same as directly accessing the member variable.
The test code is as follows:
Using UnityEngine; using System. collections; public class TestTransform: MonoBehaviour {private GameObject _ go; private Transform _ tr; protected Transform SimpleTr {get {return _ tr ;}} protected Transform Tr {get {if (_ tr = null) {_ tr = transform;} return _ tr ;}} protected GameObject SimpleGo {get {return _ go ;}} protected GameObject Go {get {if (_ go = null) {_ go = gameObject;} return _ go ;}} void Start () {_ go = gameObject; _ tr = transform; DoTestTransform (); DoTestGameObject ();} public void DoTestTransform () {int count = 1000000; transform. position = Vector3.zero; int time = System. environment. tickCount; for (int I = 0; I <count; ++ I) {int index = I % 100; transform. position = Vector3.one * index;} Debug. log ("Unity attribute:" + (System. environment. tickCount-time) * 1000); _ tr. position = Vector3.zero; int time2 = System. environment. tickCount; for (int I = 0; I <count; ++ I) {int index = I % 100; _ tr. position = Vector3.one * index;} Debug. log ("cached variable:" + (System. environment. tickCount-time 2) * 1000); Tr. position = Vector3.zero; int time3 = System. environment. tickCount; for (int I = 0; I <count; ++ I) {int index = I % 100; Tr. position = Vector3.one * index;} Debug. log ("attribute judgment:" + (System. environment. tickCount-time3) * 1000); SimpleTr. position = Vector3.zero; int time4 = System. environment. tickCount; for (int I = 0; I <count; ++ I) {int index = I % 100; SimpleTr. position = Vector3.one * index;} Debug. log ("property directly obtains the variable:" + (System. environment. tickCount-time4) * 1000);} public void DoTestGameObject () {int count = 1000000; gameObject. transform. position = Vector3.zero; int time = System. environment. tickCount; for (int I = 0; I <count; ++ I) {int index = I % 100; gameObject. transform. position = Vector3.one * index;} Debug. log ("Unity attribute:" + (System. environment. tickCount-time) * 1000); _ go. transform. position = Vector3.zero; int time2 = System. environment. tickCount; for (int I = 0; I <count; ++ I) {int index = I % 100; _ go. transform. position = Vector3.one * index;} Debug. log ("cached variable:" + (System. environment. tickCount-time 2) * 1000); Go. transform. position = Vector3.zero; int time3 = System. environment. tickCount; for (int I = 0; I <count; ++ I) {int index = I % 100; Go. transform. position = Vector3.one * index;} Debug. log ("attribute judgment:" + (System. environment. tickCount-time3) * 1000); SimpleGo. transform. position = Vector3.zero; int time4 = System. environment. tickCount; for (int I = 0; I <count; ++ I) {int index = I % 100; SimpleGo. transform. position = Vector3.one * index;} Debug. log ("property directly obtains the variable:" + (System. environment. tickCount-time4) * 1000 );}}