Unity3d why saving transform and other citations is more efficient

Source: Internet
Author: User

Normally, most of the students get transform directly gameobject.transform use. But often, you will find that some people will save transform references, for example:

 private Transform mytransform;void Awake () {    mytransform = Transform;}   then use Mytransform instead of this.transform. If you do not know the U3D internal implementation of the access method you will certainly think that the brain pumping, there is no direct, but also save themselves.  this.transform is not a variable, but a 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;        }     }}  call This.transform is actually a process that calls Intenal method (this is written in C + +, not mono). It is important to note that this call method is slightly slower because you need to call the external CIL(aka Interop), which costs additional performance.   estimate the approximate efficiency (no test, later time to make, you can refer to the last link of the article):  getcomponent is the this.transform 10 times times the time spent. This.transform is 1.5 times times the consumed time saved by the reference Mytransform. (Because the new version is optimized a lot)   actually:  if you are occasionally called transform, then do not keep its references, direct this.transform.   If it's an update, change every frame, or keep a reference to This.transform. After all, if a lot of things, can be a lot faster. The original    is as follows:   i has a question from this Burgzerg tutorial http://www.youtube.com/watch?v= O8-ozfi4uty

I Don ' t understand why Pete says to the 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. This is called before anything else in script is called
    4. void Awake () {
    5. Caching Transform
    6. Mytransform = transform;
    7. }
I understand that awake () was called before anything else in the entire script and how to does that help cache the transform O F The object, this script is attached to? Why isn't just use Update () to constantly make calls to the object ' s transform? After all, the transform are 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. public class Enemyai:monobehaviour {
  4. public Transform target;
  5. public int movespeed;
  6. public int rotationspeed;
  7. Putting transform into variable
  8. Private Transform Mytransform;
  9. This is called before anything else in script is called
  10. void Awake () {
  11. Caching Transform
  12. Mytransform = transform;
  13. }
  14. Use this for initialization
  15. void Start () {
  16. Gameobject go = Gameobject.findgameobjectwithtag ("Player");
  17. target = Go.transform;
  18. }
  19. Update is called once per frame
  20. void Update () {
  21. Debug.drawline (Target.position, mytransform.position, Color.yellow);
  22. Look at Target (player)
  23. Mytransform.rotation = Quaternion.slerp
  24. }
  25. }

Unity3d why saving transform and other citations is more efficient

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.