• Unity3D 为什么保存Transform等引用效率会更高


    正常来说,大部分同学一般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. }
     
  • 相关阅读:
    bzoj 1093: [ZJOI2007]最大半连通子图
    bzoj 1266 1266: [AHOI2006]上学路线route
    poj 2104 K-th Number
    洛谷 P3313 [SDOI2014]旅行
    cogs 306. [SGOI] 糊涂的记者
    cogs 1164. 跑步
    洛谷 1821: [JSOI2010]Group 部落划分 Group
    洛谷 U3357 C2-走楼梯
    洛谷 P3014 [USACO11FEB]牛线Cow Line
    洛谷 P2982 [USACO10FEB]慢下来Slowing down
  • 原文地址:https://www.cnblogs.com/dabiaoge/p/4135744.html
Copyright © 2020-2023  润新知