正常来说,大部分同学一般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.position, Color.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. String.Format格式说明——复制于DotNet笔记

    String.Format格式说明   C#格式化数值结果表 字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 st ...

  2. Windows下mysql忘记root密码的解决方法

    1. 首先检查mysql服务是否启动,若已启动则先将其停止服务,可在开始菜单的运行,使用命令: net stop mysql 打开第一个cmd窗口,切换到mysql的bin目录,运行命令: mysql ...

  3. docker pull certification error

    export DOMAIN_NAME=<my-dtr-domain> echo -n | openssl s_client -showcerts -connect itapregistry ...

  4. JavaScript中String的math方法与RegExp的exec方法的区别

    1.exec是正则表达式的方法,方法参数为字符串.match为字符串的方法,参数为正则表达式对象. 2.match与exec都返回数组.如果调用exec方法的正则表达式没有分组内容,则返回第一个匹配的 ...

  5. [poi2010]Hamsters

    题意:Tz养了一群仓鼠,他们都有英文小写的名字,现在Tz想用一个字母序列来表示他们的名字,只要他们的名字是字母序列中的一个子串就算,出现多次可以重复计算.现在Tz想好了要出现多少个名字,请你求出最短的 ...

  6. JAVA学习博客---2015-6

    JAVA核心技术卷一第一遍看得差不多了,应该是五月初开始看的,用了两个月的中午时间看完的,一共七百多页,接下来还是需要再看一遍,不懂的还是有很多. JAVA和C++一样是面向对象OOP的语言,不同于命 ...

  7. ado.net 向sql中插入新数据的同时获取自增重的id值

    两种方法都可以实现: 要获取的自增长列为phonebookID 方法一: sql = "insert into phonebook (mobile,peoplename) output in ...

  8. 阿里云Linux安装软件镜像源

    阿里云Linux安装软件镜像源 阿里云是最近新出的一个镜像源.得益与阿里云的高速发展,这么大的需求,肯定会推出自己的镜像源.阿里云Linux安装镜像源地址:http://mirrors.aliyun. ...

  9. 你以为的ASP.NET文件上传大小限制是你以为的吗

    我们以为的文件大小限制 我们大家都知道ASP.NET为我们提供了文件上传服务器控件FileUpload,默认情况下可上传的最大文件为4M,如果要改变可上传文件大小限制,那么我们可以在web.confi ...

  10. sql server 写性能优化

    潜在的方案是: (1)2014版本的内存表 (2)表分区,将表数据存放在多个硬盘上. (3)采用SSD磁盘阵列. (4)不重要的表关闭事务日志.??