Reference Lookup
Reference Lookup是非常重要的一個技巧,也是非常常用的技巧。
myTransform = transform;
public class Component : Object
{
public extern Transform transform
{
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}
}
Unity提供許多property供使用者存取,例如
gameObject, transform, collider, rigidbody等,這些property非常常用,但其中有一點效能陷阱。
Unity將C#作為引擎的Script,如果你有研究過他們是如何讓Script與引擎溝通的話,會發現最終幾乎都有標記
WrapperlessIcall或
MethodImpl等Attribute。實際在執行時,會去調用引擎的函式,Unmanaged與Managed之間的轉換勢必有一筆成本,其轉換代價相依於parameters。另外,我們不知道引擎內部實際上是如何運作的。(熟悉機械碼、組合語言與程式語言的原理架構等技術知識的話,倒是可能有辦法分析)
※詳細細節可以閱讀P/Invoke (Platform Invoke, Platform Invocation Services)、Marshaling等Native code (Unmanaged)與Managed之間的議題。
而在實測過程,直接存取Unity的property會比用Reference Lookup慢,而
GetComponent()、AddComponent()及FindObjectOfType()等系列函式更是緩慢,因此在程式最佳化階段有必要做Reference Lookup,而在設計底層系統時,是必須要考慮到這點的。
但要注意,此技巧不完全適用於編輯期操作Prefab。
許多Unity套件均有使用此技巧,NGUI便是著名的例子。
範例:
using UnityEngine;
public class Example : MonoBehaviour
{
GameObject _cachedGo;
Transform _cachedTransform;
public GameObject CachedGameObject
{
get
{
if (_cachedGo == null) _cachedGo = gameObject;
return _cachedGo;
}
}
public Transform CachedTransform
{
get
{
if (_cachedTransform == null) _cachedTransform= transform;
return _cachedTransform;
}
}
void Awake()
{
_cachedGo = gameObject;
_cachedTransform = transform;
}
}
使用時,
CachedGameObject取代gameObject,CachedTransform取代transform。
----------------------------------------
(本文可能会维护修改,最近更新于2014.10.12)
正常来说,大部分同学一般get transform都直接gameobject.transform使用。但往往,你会发现有些人会将transform引用保存起来,例如:
private Transform myTransform;
void Awake() {
}
然后使用myTransform替代this.transform。如果你不知道u3d内部实现获取方式你肯定会以为这人脑抽水了,有直接的不用,还自己保存起来。
this.transform并不是变量,而是一个get/set属性(property)。
using System;
using System.Runtime.CompilerServices;
using UnityEngineInternal;
namespace UnityEngine
{
}
调用this.transform实际上是一个调用intenal method的过程(这是用C/C++写的,不是MONO的)。值得注意的是这个调用方法略慢,因为你需要调用外部的CIL(aka interop),花费了额外的性能。
估计大概的效率(没测试,以后有时间再弄,大家可以参考下文章最后的链接):
GetComponent是this.transform的10倍消耗时间。
this.transform是保存了引用myTransform的1.5倍的消耗时间。(因为新版优化了不少)
实际上:
如果你是偶尔调用一下transform的话,那就不要保留它的引用了,直接this.transform。
如果是Update中,每一帧都要改变的话,还是保留一下this.transform的引用吧。毕竟倘若一大堆东西的话,能快不少呢。
详细参考:
http://forum.unity3d.com/threads/130359-How-does-caching-an-objects-transform-make-the-game-run-faster
http://forum.unity3d.com/threads/96908-Unity-should-internally-cache-transform-for-a-valid-speed-boost
http://forum.unity3d.com/threads/130365-CachedMB