正常来说,大部分同学一般get transform都直接gameobject.transform使用。但往往,你会发现有些人会将transform引用保存起来,例如:

 
private Transform myTransform;
void Awake() {
    myTransform = transform;
}
 
然后使用myTransform替代this.transform。如果你不知道u3d内部实现获取方式你肯定会以为这人脑抽水了,有直接的不用,还自己保存起来。
 
this.transform并不是变量,而是一个get/set属性(property)。
 
using System;
using System.Runtime.CompilerServices;
using UnityEngineInternal;
namespace UnityEngine
{
    public class Component : Object
    {
        public extern Transform transform
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }
    }
}
 
调用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的引用吧。毕竟倘若一大堆东西的话,能快不少呢。
 
 
原文如下:
 
 
I have a question from this Burgzerg tutorial http://www.youtube.com/watch?v=O8-oZfi4utY

I don't understand why Pete says to cache the transform by defining the myTransform variable at the top of the script and then assigning it a transform value in the Awake() function like as follows:

Code (csharp):
  1. // Putting transform into variable
  2.     private Transform myTransform;
  3.    
  4.     // This is called before anything else in script is called
  5.     void Awake() {
  6.         // Caching transform
  7.         myTransform = transform;
  8.     }

I understand that Awake() is called before anything else in the entire script but how does that help cache the transform of the object that this script is attached to? Why not just use Update() to constantly make calls to the object's transform? After all, the transform is most likely going to be constantly changing.

The full code from the video is below:

Code (csharp):
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EnemyAI : MonoBehaviour {
  5.     public Transform target;
  6.     public int moveSpeed;
  7.     public int rotationSpeed;
  8.    
  9.     // Putting transform into variable
  10.     private Transform myTransform;
  11.    
  12.     // This is called before anything else in script is called
  13.     void Awake() {
  14.         // Caching transform
  15.         myTransform = transform;
  16.     }
  17.  
  18.     // Use this for initialization
  19.     void Start () {
  20.         GameObject go = GameObject.FindGameObjectWithTag("Player");
  21.        
  22.         target = go.transform;
  23.     }
  24.    
  25.     // Update is called once per frame
  26.     void Update () {
  27.         Debug.DrawLine(target.position, myTransform.positionColor.yellow);   
  28.        
  29.         // Look at target (player)
  30.         myTransform.rotation = Quaternion.Slerp
  31.     }
  32. }
 

Unity3D 为什么保存Transform等引用效率会更高的更多相关文章

  1. 《转》Unity3D研究院之UGUI一个优化效率小技巧

    无意间发现了一个小技巧.如下图所示,可以发现UGUI的Image组件的RaycastTarget勾选以后会消耗一些效率,为了节省效率就不要勾选它了,不仅Image组件Text组件也有这样的问题. 一般 ...

  2. C# 文件选择对话框,Unity3d文件保存对话框

    using OpenWinForm = System.Windows.Forms; 在unity3d中,使用FileDialog应该把System.Windows.Forms.dll拷贝到unity工 ...

  3. 【Unity3D】中的空引用 Null Reference Exception

    Null Reference Exception : Object reference not set to an instance of an object. 异常:空引用,对象的引用未设置到对象的 ...

  4. 【Unity3D】Unity3D中Material与ShareMaterial引用的区别

    我们在使用Unity引擎的时候,有时候需要去修改某个物体上的Material,在Unity的Renderer类里,提供了两个方法接口供我们使用. Renderer.material和Renderer. ...

  5. Unity3D 摄像机的Transform通过摇杆输出的方向

    要解决的问题是:摄像机的方向不固定,当摇杆向前(0,1)推时,主角要往摄像机的朝向(忽略Y方向)走,当摇杆往右(1,0)推的时,主角朝摄像机的右方向 /// <summary> /// 摄 ...

  6. unity3d 自动保存

    using UnityEngine; using UnityEditor; using System; public class AutoSave : EditorWindow { private b ...

  7. C++面向对象

    此博文仅作为C++考研专业课的复习内容. 面向对象 构造函数 在对象被创建的时候将自动调用. 复制构造函数 形参是本类对象的引用.其作用是使用一个已经存在的对象,去初始化一个同类的新对象. 复制构造函 ...

  8. CPPFormatLibary提升效率的优化原理

    CPPFormatLibary,以下简称FL,介绍:关于CPPFormatLibary. 与stringstream,甚至C库的sprintf系列想比,FL在速度上都有优势,而且是在支持.net格式化 ...

  9. Unity3D占用内存太大的解决方法

    原地址:http://www.cnblogs.com/88999660/archive/2013/03/15/2961663.html 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大 ...

随机推荐

  1. MongoDB学习笔记-06 数据库命令、固定集合、GridFS、javascript脚本

    介绍MongoDB支持的一些高级功能: 数据库命令 固定大小的集合 GridFS存储大文件 MongoDB对服务端JavaScript的支持 数据库命令 命令的原理 MongoDB中的命令其实是作为一 ...

  2. 文本框如果不输入任何内容提交过后是一个空字符串还是null

    1.在表单不填就提交的情况下,text类型和textarea类型的表单域,提交到服务端为空 2.checkbox.readio.select等表单域在为不填情况下不会提交到服务器,也就是说服务器接收不 ...

  3. BZOJ2888 资源运输(LCT启发式合并)

    这道题目太神啦! 我们考虑他的每一次合并操作,为了维护两棵树合并后树的重心,我们只好一个一个的把节点加进去.那么这样一来看上去似乎就是一次操作O(nlogn),但是我们拥有数据结构的合并利器--启发式 ...

  4. c# 根据文件流查看文件真实格式

    今天在做图片注册的功能的时候,测试提出一个问题:将随便一个非图片文件将后缀名改为jpg或其他,上传时应检验图片合法性.然后同事给提供了根据文件流前两个字节判断文件真实格式的思路,代码如下: publi ...

  5. Drupal资源

    以下是一些Drupal的常用资源. www.drupal.org:Drupal官网,拥有最全 www.acquia.com:Drupal奠基人Dries主导的专业网站,有著名的Aquia平台,功能类似 ...

  6. ELb表达式

    主要用于servlet的4个作用域取值:pageScope.requestScope.sessionScope.applicationScope 取值顺序依次从小到大.取值方式如:操作javabean ...

  7. hdu 5945 Fxx and game

    青年理论计算机科学家Fxx给的学生设计了一款数字游戏. 一开始你将会得到一个数X,每次游戏将给定两个参数x,k,t, 任意时刻你可以对你的数执行下面两个步骤之一: .X=X−i(<=i<= ...

  8. [1002]prime

    输入一个数,判断其是否为素数: 本题有多组测试样例. 输入规则如下: 第一行为一个整数,样例组数T: 第二至第t+1行每行都有一个整数a(a >= 2),表示需要处理的数: 如果是素数则输出“y ...

  9. jpa更新数据出错

    在更新表的一条记录时出错,错误为“java.sql.SQLException: Field 'is_del' doesn't have a default value”,表有一个属性isDel为Boo ...

  10. 介绍Oedis - Redis OH/RM

    作死造轮子 Oedis是近段时间为了解决日志型数据如何与Entity Framework的查询整合的问题写的一个Redis的OH /RM.虽然Redis出来蛮久了,各路高手也都提出了实践方案,但是或许 ...