Obtaining Components using the fastest method

There are several variations of the GetComponent() method, and it becomes prudent to call the fastest possible version of this method. The three overloads available are GetComponent(string), GetComponent<T>(), and GetComponent(typeof(T)). It turns out that the fastest version depends on which version of Unity we are running.

In Unity 4, the GetComponent(typeof(T)) method is the fastest of the available options by a reasonable margin. Let's prove this with some simple testing:

int numTests = 1000000;
TestComponent test;

using (new CustomTimer("GetComponent(string)", numTests)) {
  for (var i = 0; i < numTests; ++i) {
    test = (TestComponent)GetComponent("TestComponent");
  }
}

using (new CustomTimer("GetComponent<ComponentName>", numTests)) {
  for (var i = 0; i < numTests; ++i) {
    test = GetComponent<TestComponent>();
  }
}

using (new CustomTimer("GetComponent(typeof(ComponentName))", numTests))  {
  for (var i = 0; i < numTests; ++i) {
    test = (TestComponent)GetComponent(typeof(TestComponent));
  }
}

This code tests each of the GetComponent() overloads a million times. This is far more tests than would be sensible for a typical project, but it is enough tests to prove the point.

Here is the result we get when the tests complete:

Obtaining Components using the fastest method

As you can see, GetComponent(typeof(T)) is significantly faster than GetComponent<T>(), which is around five times faster than GetComponent(string). This test was performed against Unity 4.5.5, but the behavior should be equivalent all the way back to Unity 3.x.

Note

GetComponent(string) should not be used, since it is notoriously slow and is only included for completeness.

These results change when we run the exact same test in Unity 5. Unity Technologies made some performance enhancements to how System.Type references are passed around in Unity 5.0, and, as a result, GetComponent<T>() and GetComponent(typeof(T)) become essentially equivalent:

Obtaining Components using the fastest method

As you can see, the GetComponent<T>() method is only a tiny fraction faster than GetComponent(typeof(T)), while GetComponent(string) is now around 30 times slower than the alternatives (interestingly, it became even slower than it was in Unity 4). Multiple tests will probably yield small variations in these results, but ultimately we can favor either of the type-based versions of GetComponent() when we're working in Unity 5, and the outcome will be about the same.

There is one caveat, however. If we're running Unity 4, then we still have access to a variety of quick accessor properties such as collider, rigidbody, camera, and so on. These properties behave like precached Component member variables, which are significantly faster than all of the traditional GetComponent() methods:

int numTests = 1000000;
Rigidbody test;

using (new CustomTimer("Cached reference", numTests))
{
  for (var i = 0; i < numTests; ++i) {
    test = gameObject.rigidbody;
  }
}

Note

Note that this code is intended for Unity 4, and will not compile in Unity 5 due to the removal of the rigidbody property.

Running this test in Unity 4 gives us the following result:

Obtaining Components using the fastest method

In an effort to reduce dependencies and improve code modularization in the engine's backend, Unity Technologies has deprecated all of these quick accessor variables in Unity 5. Only the transform property remains.

Tip

Unity 4 users considering an upgrade to Unity 5 should know that upgrading will automatically modify any of these properties to use the GetComponent<T>() method. However, this will result in uncached GetComponent<T>() calls scattered throughout your code, possibly requiring you to revisit the techniques introduced in the earlier section, entitled Cache Component References.

The moral of the story is that if we are running Unity 4, and the required Component is one of GameObject's built-in accessor properties, then we should use that version. If not, then we should favor GetComponent(typeof(T)). Meanwhile, if we're running Unity 5, then we can favor either of the type-based versions: GetComponent<T>() or GetComponent(typeof(T)).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.62.168