Best way use Resources.Load for spcific type?

There are 3 ways using Resources.Load:

  1. Resources.Load<GameObject> ("objectPath");
  2. Resources.Load ("objectPath", typeof (GameObject));
  3. Resources.Load ("objectPath") as GameObject;

Are they all the same after compile? Or I think the first one have bit better performance (and easier to read anyway)?

The third one is not the same as the first two. 1 and 2 will attempt to load an object of type “GameObject” and return null if not found. #3 will load any object with that given name, regardless of the type, then try to cast it as GameObject, turning into null if the cast fails.

Without looking into Unity’s code is hard to tell, but I’d wager 1 and 2 look up the type before loading and prevent loading the wrong type of object (probably with the help of meta files), so 1 and 2 might be slightly better performing than 3.

1 and 2 are essentially the same. I prefer #1 for readability :smiley: