咱们来详谈Unity的transform使用,这里所说的tansform不是类UnityEngine命名空间下的Transform,而是transform.
Transform 是Unity中最经常使用的类了。
其类的代码以下,代码贴出来太长也不是要说的重点:css
#region 程序集 UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null // H:\Unity\Project\VRThemePark_03\Library\UnityAssemblies\UnityEngine.dll #endregion using System; using System.Collections; using UnityEngine.Internal; namespace UnityEngine { // // 摘要: // /// // Position, rotation and scale of an object. // /// public class Transform : Component, IEnumerable { protected Transform(); // // 摘要: // /// // The number of children the Transform has. // /// public int childCount { get; } // // 摘要: // /// // The rotation as Euler angles in degrees. // /// public Vector3 eulerAngles { get; set; } // // 摘要: // /// // The blue axis of the transform in world space. // /// public Vector3 forward { get; set; } // // 摘要: // /// // Has the transform changed since the last time the flag was set to 'false'? // /// public bool hasChanged { get; set; } // // 摘要: // /// // The transform capacity of the transform's hierarchy data structure. // /// public int hierarchyCapacity { get; set; } // // 摘要: // /// // The number of transforms in the transform's hierarchy data structure. // /// public int hierarchyCount { get; } // // 摘要: // /// // The rotation as Euler angles in degrees relative to the parent transform's rotation. // /// public Vector3 localEulerAngles { get; set; } ..... public void Translate(float x, float y, float z, Transform relativeTo); } }
咱们所说的是经常使用的还有对象组件自身的transform,他里面包含了位置,旋转,缩放参数。
在经常使用组件Compnent的代码中:缓存
// // 摘要: // /// // The Transform attached to this GameObject (null if there is none attached). // /// public Transform transform { get; }
注意这个东西是属性,有get,没有set.
固然命名空间仍旧为UnityEngine。app
咱们先来看看,这个WrapperlessIcall ,它是unity中一个属性字段,他有什么用呢?
WrapperlessIcall 内部实现,非公开方法。
你们来看看以下代码:less
private Transform myTransform; void Awake() { myTransform = transform; }
看起来稀松日常,波澜不惊,可是下面水仍是蛮深的。
使用myTransform替代this.transform。若是你不知道u3d内部实现获取方式你确定会觉得这人脑抽水了,有直接的不用,还本身保存起来。
this.transform并非变量,而是一个get/set属性(property)
他是一个C++写的代码,在Mono中被调用。调用是intenal method的调用,其效率自己不是高。
好比,transform 常常须要保存在本地,而后在使用。函数
namespace UnityEngine
{
public class Component : Object { public extern Transform transform { [WrapperlessIcall] [MethodImpl(MethodImplOptions.InternalCall)] get; } } }
值得注意的是这个调用方法略慢,由于你须要调用外部的CIL(aka interop),花费了额外的性能。性能
我的以为这个是以前的unity版本的东西,可能效率和性能没有作优化。测试
WrapperlessIcall] [MethodImpl(MethodImplOptions.InternalCall)]
就这些属性来讲,有的是直接调用C++代码,有的则是调用.net的内部函数到Unity中。
对于新的版本是否是有的优化处理呢,本身作了测试:
先看看如今Compnent的代码:优化
// // 摘要: // /// // The Transform attached to this GameObject (null if there is none attached). // /// public Transform transform { get; }
而后是测试代码:this
using UnityEngine; using System.Collections; using System.Diagnostics; using System.Reflection; using System; public class CacheTest : MonoBehaviour { const int ITERATIONS = 1000000; // Use this for initialization Transform cached; IEnumerator Start() { cached = transform; UnityEngine.Debug.Log("test........."); while(true) { yield return null; if (Input.GetKeyDown(KeyCode.T)) break; var sw1 = Stopwatch.StartNew(); { Transform trans1; for (int i = 0; i < ITERATIONS; i++) trans1 = GetComponent<Transform>(); } sw1.Stop(); var sw2 = Stopwatch.StartNew(); { Transform trans2; for (int i = 0; i < ITERATIONS; i++) trans2 = transform; } sw2.Stop(); var sw3 = Stopwatch.StartNew(); { Transform trans3; for (int i = 0; i < ITERATIONS; i++) trans3 = cached; } sw3.Stop(); var sw4 = Stopwatch.StartNew(); { Transform trans4; for (int i = 0; i < ITERATIONS; i++) trans4 = this.transform; } sw4.Stop(); UnityEngine.Debug.Log(ITERATIONS + " iterations"); UnityEngine.Debug.Log("GetComponent " + sw1.ElapsedMilliseconds + "ms"); UnityEngine.Debug.Log("this.transform" + sw4.ElapsedMilliseconds + "ms"); UnityEngine.Debug.Log("CachedMB " + sw2.ElapsedMilliseconds + "ms"); UnityEngine.Debug.Log("Manual Cache " + sw3.ElapsedMilliseconds + "ms"); } } }
结果仍是同样的。仍是须要作处理的。spa
效率有手动cache (4ms)>>transform(20ms)>>this.tranform(22ms)>> GetComponent()(54ms)
可是原来的测试结果为:
1000000 次的Iterations ● GetComponent = 619ms ● Monobehaviour = 60ms ● CachedMB = 8ms ● Manual Cache = 3ms
看来这其中仍是有奥秘的。
我电脑配置目前还算能够,win7 64 位,I7-3770 + 960显卡+ 16G内存。
结果对比,相对与以前2012年Unity版本,可能mono作了很大的优化,固然咱们的电脑可能仍是不同,没有办法直接作对比,也只能猜想而已。
可是结论仍是同样的:
在之后的使用中,若大量使用,仍是需把transform给手动保存下来吧。
说明:
代码作了修改:
原来的代码中有这些:
1. Transform _transform; 2. public Transform transform 3. { 4. get { return _transform ?? (_transform = base.transform); } 5. } 6. 7. 8. //for testing 9. public Transform 10. { 11. get { return base.transform; } 12. }
我还想努力一把!!
让别人能够直接用,可是有不修改原有代码:
怎么办呢?
既然你们都要继承monobehaviour,那我就在他上面想办法。
public static class ExtendMono { public static Transform tranform(this MonoBehaviour tsf) { Transform _tsf; _tsf = tsf.transform; return _tsf; } }
可是这个扩展方法,必须静态的,因此没有办法作一个静态的临时变量啊,这个不靠谱啊。
若写成上面的代码效率并无太多提升。每次仍是须要赋值。
因此这个路走不通啊!!
来看方法二吧。
public class MonoBehaviour : UnityMonoBehaviour { Transform _transform; public Transform transform { get { return _transform ?? (_transform = base.transform); } } }
UnityMonoBehaviour 这个是啥呢?哈哈
看using UnityMonoBehaviour = UnityEngine.MonoBehaviour;
咱们来看看结果:
直接使用tranform 和this.tranform花费时间为9ms,比上面的20多ms,那是下降了不少。
可是,这个是结论啊,仍是没有手动缓存的效果高啊,依旧为4ms。
放出全部代码:
using UnityMonoBehaviour = UnityEngine.MonoBehaviour; using UnityEngine; using System.Collections; using System.Diagnostics; using System.Reflection; using System; namespace aa { public class CacheTest : MonoBehaviour { const int ITERATIONS = 1000000; // Use this for initialization Transform cached; IEnumerator Start() { cached = transform; UnityEngine.Debug.Log("test........."); while (true) { yield return null; if (Input.GetKeyDown(KeyCode.T)) break; var sw1 = Stopwatch.StartNew(); { Transform trans1; for (int i = 0; i < ITERATIONS; i++) trans1 = GetComponent<Transform>(); } sw1.Stop(); var sw2 = Stopwatch.StartNew(); { Transform trans2; for (int i = 0; i < ITERATIONS; i++) trans2 = transform; } sw2.Stop(); var sw3 = Stopwatch.StartNew(); { Transform trans3; for (int i = 0; i < ITERATIONS; i++) trans3 = cached; } sw3.Stop(); var sw4 = Stopwatch.StartNew(); { Transform trans4; for (int i = 0; i < ITERATIONS; i++) trans4 = this.transform; } sw4.Stop(); UnityEngine.Debug.Log(ITERATIONS + " iterations"); UnityEngine.Debug.Log("GetComponent " + sw1.ElapsedMilliseconds + "ms"); UnityEngine.Debug.Log("this.transform " + sw4.ElapsedMilliseconds + "ms"); UnityEngine.Debug.Log("CachedMB " + sw2.ElapsedMilliseconds + "ms"); UnityEngine.Debug.Log("Manual Cache " + sw3.ElapsedMilliseconds + "ms"); } } } public class MonoBehaviour : UnityMonoBehaviour { Transform _transform; public Transform transform { get { return _transform ?? (_transform = base.transform); } } } //public static class ExtendMono //{ // public static Transform tranform(this MonoBehaviour tsf) // { // Transform _tsf; // _tsf = tsf.transform; // return _tsf; // } //} }