Unity3D 为什么保存Transform等引用效率会更高
正常来说,大部分同学一般get transform都直接gameobject.transform使用。但往往,你会发现有些人会将transform引用保存起来,例如:
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:
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:
- using UnityEngine;
- using System.Collections;
- public class EnemyAI : MonoBehaviour {
- public int moveSpeed;
- public int rotationSpeed;
- // Putting transform into variable
- private Transform myTransform;
- // This is called before anything else in script is called
- void Awake() {
- // Caching transform
- myTransform = transform;
- }
- // Use this for initialization
- void Start () {
- GameObject go = GameObject.FindGameObjectWithTag("Player");
- target = go.transform;
- }
- // Update is called once per frame
- void Update () {
- // Look at target (player)
- }
- }
Unity3D 为什么保存Transform等引用效率会更高的更多相关文章
- 《转》Unity3D研究院之UGUI一个优化效率小技巧
无意间发现了一个小技巧.如下图所示,可以发现UGUI的Image组件的RaycastTarget勾选以后会消耗一些效率,为了节省效率就不要勾选它了,不仅Image组件Text组件也有这样的问题. 一般 ...
- C# 文件选择对话框,Unity3d文件保存对话框
using OpenWinForm = System.Windows.Forms; 在unity3d中,使用FileDialog应该把System.Windows.Forms.dll拷贝到unity工 ...
- 【Unity3D】中的空引用 Null Reference Exception
Null Reference Exception : Object reference not set to an instance of an object. 异常:空引用,对象的引用未设置到对象的 ...
- 【Unity3D】Unity3D中Material与ShareMaterial引用的区别
我们在使用Unity引擎的时候,有时候需要去修改某个物体上的Material,在Unity的Renderer类里,提供了两个方法接口供我们使用. Renderer.material和Renderer. ...
- Unity3D 摄像机的Transform通过摇杆输出的方向
要解决的问题是:摄像机的方向不固定,当摇杆向前(0,1)推时,主角要往摄像机的朝向(忽略Y方向)走,当摇杆往右(1,0)推的时,主角朝摄像机的右方向 /// <summary> /// 摄 ...
- unity3d 自动保存
using UnityEngine; using UnityEditor; using System; public class AutoSave : EditorWindow { private b ...
- C++面向对象
此博文仅作为C++考研专业课的复习内容. 面向对象 构造函数 在对象被创建的时候将自动调用. 复制构造函数 形参是本类对象的引用.其作用是使用一个已经存在的对象,去初始化一个同类的新对象. 复制构造函 ...
- CPPFormatLibary提升效率的优化原理
CPPFormatLibary,以下简称FL,介绍:关于CPPFormatLibary. 与stringstream,甚至C库的sprintf系列想比,FL在速度上都有优势,而且是在支持.net格式化 ...
- Unity3D占用内存太大的解决方法
原地址:http://www.cnblogs.com/88999660/archive/2013/03/15/2961663.html 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大 ...
随机推荐
- java中打印变量地址
在java中打印变量的地址 这个代码是在startoverflow上看到的,跟大家分享一下. import sun.misc.Unsafe; import java.lang.reflect.Fiel ...
- ListView之头部浮动效果
ListView 中有时需要在顶部固定一个浮动栏,当向上滑动 ListView 时,浮动栏固定在顶部,当向下滑动 ListView 到其 HeaderView 可见时,浮动栏成为ListView的一部 ...
- CentOS 7 安装 vmware-tools
[原创]标题:<CentOS 7 安装 vmware-tools>:作者:肖雪峰,QQ:35360657. 用 VMware Workstation 11 新安装了CentOS 7虚拟机, ...
- Android--Alarm机制
1.Alarm 能够实现 Android 中的定时任务,它与 Java 中的 Timer类的区别在于,Android 系统在休眠状态下时 Timer 类中的定时任务可能无法正常运行,而 Alarm 机 ...
- ADO.NET Entity Framework
ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案, 早期被称为 ObjectSpace,现已经包含在 V ...
- LoadRunner参数化取值及连接数据库操作步骤
很多情况下,参数添加的数据不是十条二十条,也不是一百两百,对于这种大数量的数据我们可以通过数据库将数据导入: 选中要参数化的内容如下图一所示: 方法一,右键---[Replace with a new ...
- 关于 REST
REST(Representational State Transfer)是一种轻量级的 Web Service 架构风格,可以翻译成“表述性状态转移”,实现和操作明显比 SOAP 和 XML-RPC ...
- android 启动过程
android系统启动的时候首先会启动Linux的基础进程,加载Linux kernel启动初始化(init)进程. 接着,回启动Linux deamon(守护进程)会启动以下的内容: ①启动USBd ...
- Eclipse引入外部Jar在发布时没有自动带入,导致出现ClassNoFound错误
今天换了一台电脑重新配置环境调试老程序的时候出现链接数据库错误java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver提示. ...
- .NET JSON对象序列化和反序列化
class Program { static void Main(string[] args) { Console.WriteLine("========================== ...