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):
- Putting transform into variable
- Private Transform Mytransform;
- This is called before anything else in script is called
- void Awake () {
- Caching Transform
- Mytransform = transform;
- }
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):
- Using Unityengine;
- Using System.Collections;
- public class Enemyai:monobehaviour {
- public Transform target;
- 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 () {
- Debug.drawline (Target.position, mytransform.position, Color.yellow);
- Look at Target (player)
- Mytransform.rotation = Quaternion.slerp
- }
- }
Unity3d why saving transform and other citations is more efficient