unity源码解析Transform

Transform这个类继承自Component, IEnumerable。我们在开发的过程中之所以可以对Transform进行foreach,就是因为它实现了IEnumerable抽象方法GetEnumerator,该方法返回一个IEnumerator,而通过重写了IEnumerator中的Current,可以依次获得子对象。废话不多说了

using System;
using System.Collections;
using System.Runtime.CompilerServices;
using UnityEngine.Internal;

namespace UnityEngine
{
    public class Transform : Component, IEnumerable
    {
        private sealed class Enumerator : IEnumerator
        {
            private Transform outer;

            private int currentIndex = -1;

            public object Current
            {
                get
                {
                    return this.outer.GetChild(this.currentIndex);
                }
            }

            internal Enumerator(Transform outer)
            {
                this.outer = outer;
            }

            public bool MoveNext()
            {
                int childCount =this.outer.childCount;
                return ++this.currentIndex < childCount;
            }

            public void Reset()
            {
                this.currentIndex = -1;
            }
        }

        public Vector3 position
        {
            get
            {
                Vector3 result;
                this.INTERNAL_get_position(out result);
                return result;
            }
            set
            {
                this.INTERNAL_set_position(refvalue);
            }
        }

        public Vector3 localPosition
        {
            get
            {
                Vector3 result;
                this.INTERNAL_get_localPosition(out result);
                return result;
            }
            set
            {
                this.INTERNAL_set_localPosition(refvalue);
            }
        }

        public Vector3 eulerAngles
        {
            get
            {
                return this.rotation.eulerAngles;
            }
            set
            {
                this.rotation = Quaternion.Euler(value);
            }
        }

        public Vector3 localEulerAngles
        {
            get
            {
                Vector3 result;
                this.INTERNAL_get_localEulerAngles(out result);
                return result;
            }
            set
            {
                this.INTERNAL_set_localEulerAngles(refvalue);
            }
        }

        public Vector3 right
        {
            get
            {
                return this.rotation * Vector3.right;
            }
            set
            {
                this.rotation = Quaternion.FromToRotation(Vector3.right,value);
            }
        }

        public Vector3 up
        {
            get
            {
                return this.rotation * Vector3.up;
            }
            set
            {
                this.rotation = Quaternion.FromToRotation(Vector3.up,value);
            }
        }

        public Vector3 forward
        {
            get
            {
                return this.rotation * Vector3.forward;
            }
            set
            {
                this.rotation = Quaternion.LookRotation(value);
            }
        }

        public Quaternion rotation
        {
            get
            {
                Quaternion result;
                this.INTERNAL_get_rotation(out result);
                return result;
            }
            set
            {
                this.INTERNAL_set_rotation(refvalue);
            }
        }

        public Quaternion localRotation
        {
            get
            {
                Quaternion result;
                this.INTERNAL_get_localRotation(out result);
                return result;
            }
            set
            {
                this.INTERNAL_set_localRotation(refvalue);
            }
        }

        public Vector3 localScale
        {
            get
            {
                Vector3 result;
                this.INTERNAL_get_localScale(out result);
                return result;
            }
            set
            {
                this.INTERNAL_set_localScale(refvalue);
            }
        }

        public Transform parent
        {
            get
            {
                return this.parentInternal;
            }
            set
            {
                if (thisis RectTransform)
                {
                    Debug.LogWarning("Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale, which can prevent common UI scaling issues.",this);
                }
                this.parentInternal = value;
            }
        }

        internal extern Transform parentInternal
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            set;
        }

        public Matrix4x4 worldToLocalMatrix
        {
            get
            {
                Matrix4x4 result;
                this.INTERNAL_get_worldToLocalMatrix(out result);
                return result;
            }
        }

        public Matrix4x4 localToWorldMatrix
        {
            get
            {
                Matrix4x4 result;
                this.INTERNAL_get_localToWorldMatrix(out result);
                return result;
            }
        }

        public extern Transform root
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }

        public extern int childCount
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
        }

        public Vector3 lossyScale
        {
            get
            {
                Vector3 result;
                this.INTERNAL_get_lossyScale(out result);
                return result;
            }
        }

        public extern bool hasChanged
        {
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            get;
            [WrapperlessIcall]
            [MethodImpl(MethodImplOptions.InternalCall)]
            set;
        }

        protected Transform()
        {
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_get_position(out Vector3value);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_set_position(ref Vector3value);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_get_localPosition(out Vector3value);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_set_localPosition(ref Vector3value);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_get_localEulerAngles(out Vector3value);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_set_localEulerAngles(ref Vector3value);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_get_rotation(out Quaternionvalue);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_set_rotation(ref Quaternionvalue);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_get_localRotation(out Quaternionvalue);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_set_localRotation(ref Quaternionvalue);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_get_localScale(out Vector3value);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_set_localScale(ref Vector3value);

        public void SetParent(Transform parent)
        {
            this.SetParent(parent,true);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void SetParent(Transform parent, bool worldPositionStays);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_get_worldToLocalMatrix(out Matrix4x4value);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_get_localToWorldMatrix(out Matrix4x4value);

        [ExcludeFromDocs]
        public void Translate(Vector3 translation)
        {
            Space relativeTo = Space.Self;
            this.Translate(translation, relativeTo);
        }
//更新位置,如果传入得是世界坐标,则直接在原基础上增加,否则表示传入是一个该物体的方向上位移
        public void Translate(Vector3 translation, [DefaultValue("Space.Self")] Space relativeTo)
        {
            if (relativeTo == Space.World)
            {
                this.position += translation;
            }
            else
            {
                this.position += this.TransformDirection(translation);
            }
        }
//默认传入值为当前物体的坐标系上的位移
        [ExcludeFromDocs]
        public void Translate(float x,float y, float z)
        {
            Space relativeTo = Space.Self;
            this.Translate(x, y, z, relativeTo);
        }
//更新位置,如果传入得是世界坐标,则直接在原基础上增加,否则表示传入是一个该物体的方向上位移
        public void Translate(float x,float y, float z, [DefaultValue("Space.Self")] Space relativeTo)
        {
            this.Translate(newVector3(x, y, z), relativeTo);
        }
//更新位置,如果传入relativeTo为空表示是世界坐标,则直接在原基础上增加,否则表示传入是一个该物体的方向上位移
        public void Translate(Vector3 translation, Transform relativeTo)
        {
            if (relativeTo)
            {
                this.position += relativeTo.TransformDirection(translation);
            }
            else
            {
                this.position += translation;
            }
        }
//同上
        public void Translate(float x,float y, float z, Transform relativeTo)
        {
            this.Translate(newVector3(x, y, z), relativeTo);
        }
//默认该物体的坐标系中旋转
        [ExcludeFromDocs]
        public void Rotate(Vector3 eulerAngles)
        {
            Space relativeTo = Space.Self;
            this.Rotate(eulerAngles, relativeTo);
        }
//旋转,从这个方法我们可以看出来,unity的欧拉角旋转并非真正的欧拉角旋转,而是同样是四元数的方式
        public void Rotate(Vector3 eulerAngles, [DefaultValue("Space.Self")] Space relativeTo)
        {//相对于父节点的旋转
            Quaternion rhs = Quaternion.Euler(eulerAngles.x, eulerAngles.y, eulerAngles.z);
            if (relativeTo == Space.Self)
            {
                this.localRotation *= rhs;
            }
            else
            { //世界空间的旋转
                this.rotation *= Quaternion.Inverse(this.rotation) * rhs *this.rotation;
            }
        }
//默认相对父节点旋转
        [ExcludeFromDocs]
        public void Rotate(float xAngle,float yAngle, float zAngle)
        {
            Space relativeTo = Space.Self;
            this.Rotate(xAngle, yAngle, zAngle, relativeTo);
        }

        public void Rotate(float xAngle,float yAngle, float zAngle, [DefaultValue("Space.Self")] Space relativeTo)
        {
            this.Rotate(newVector3(xAngle, yAngle, zAngle), relativeTo);
        }

        internal void RotateAroundInternal(Vector3 axis,float angle)
        {
            Transform.INTERNAL_CALL_RotateAroundInternal(this,ref axis, angle);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern void INTERNAL_CALL_RotateAroundInternal(Transform self,ref Vector3 axis, float angle);
//绕着axis这跟轴旋转angle角度,非弧度制
        [ExcludeFromDocs]
        public void Rotate(Vector3 axis,float angle)
        {
            Space relativeTo = Space.Self;
            this.Rotate(axis, angle, relativeTo);
        }
//绕着axis这跟轴旋转angle角度,非弧度制
        public void Rotate(Vector3 axis,float angle, [DefaultValue("Space.Self")] Space relativeTo)
        {
            if (relativeTo == Space.Self)
            {
                this.RotateAroundInternal(base.transform.TransformDirection(axis), angle * 0.0174532924f);
            }
            else
            {
                this.RotateAroundInternal(axis, angle *0.0174532924f);
            }
        }
//在point处建立坐标系,方向向量为axis,绕着这跟轴旋转angle角度,非弧度制
        public void RotateAround(Vector3 point, Vector3 axis,float angle)
        {
            Vector3 vector = this.position;
            Quaternion rotation = Quaternion.AngleAxis(angle, axis);
            Vector3 vector2 = vector - point;
            vector2 = rotation * vector2;
            vector = point + vector2;
            this.position = vector;
            this.RotateAroundInternal(axis, angle *0.0174532924f);
        }

        [ExcludeFromDocs]
        public void LookAt(Transform target)
        {
            Vector3 up = Vector3.up;
            this.LookAt(target, up);
        }

        public void LookAt(Transform target, [DefaultValue("Vector3.up")] Vector3 worldUp)
        {
            if (target)
            {
                this.LookAt(target.position, worldUp);
            }
        }

        public void LookAt(Vector3 worldPosition, [DefaultValue("Vector3.up")] Vector3 worldUp)
        {
            Transform.INTERNAL_CALL_LookAt(this,ref worldPosition, ref worldUp);
        }

        [ExcludeFromDocs]
        public void LookAt(Vector3 worldPosition)
        {
            Vector3 up = Vector3.up;
            Transform.INTERNAL_CALL_LookAt(this,ref worldPosition, ref up);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern void INTERNAL_CALL_LookAt(Transform self,ref Vector3 worldPosition, ref Vector3 worldUp);

        public Vector3 TransformDirection(Vector3 direction)
        {
            return Transform.INTERNAL_CALL_TransformDirection(this,ref direction);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern Vector3 INTERNAL_CALL_TransformDirection(Transform self, ref Vector3 direction);

        public Vector3 TransformDirection(float x,float y, float z)
        {
            return this.TransformDirection(newVector3(x, y, z));
        }

        public Vector3 InverseTransformDirection(Vector3 direction)
        {
            return Transform.INTERNAL_CALL_InverseTransformDirection(this,ref direction);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern Vector3 INTERNAL_CALL_InverseTransformDirection(Transform self, ref Vector3 direction);

        public Vector3 InverseTransformDirection(float x,float y, float z)
        {
            return this.InverseTransformDirection(newVector3(x, y, z));
        }

        public Vector3 TransformVector(Vector3 vector)
        {
            return Transform.INTERNAL_CALL_TransformVector(this,ref vector);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern Vector3 INTERNAL_CALL_TransformVector(Transform self, ref Vector3 vector);

        public Vector3 TransformVector(float x,float y, float z)
        {
            return this.TransformVector(newVector3(x, y, z));
        }

        public Vector3 InverseTransformVector(Vector3 vector)
        {
            return Transform.INTERNAL_CALL_InverseTransformVector(this,ref vector);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern Vector3 INTERNAL_CALL_InverseTransformVector(Transform self, ref Vector3 vector);

        public Vector3 InverseTransformVector(float x,float y, float z)
        {
            return this.InverseTransformVector(newVector3(x, y, z));
        }

        public Vector3 TransformPoint(Vector3 position)
        {
            return Transform.INTERNAL_CALL_TransformPoint(this,ref position);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern Vector3 INTERNAL_CALL_TransformPoint(Transform self, ref Vector3 position);

        public Vector3 TransformPoint(float x,float y, float z)
        {
            return this.TransformPoint(newVector3(x, y, z));
        }

        public Vector3 InverseTransformPoint(Vector3 position)
        {
            return Transform.INTERNAL_CALL_InverseTransformPoint(this,ref position);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern Vector3 INTERNAL_CALL_InverseTransformPoint(Transform self, ref Vector3 position);

        public Vector3 InverseTransformPoint(float x,float y, float z)
        {
            return this.InverseTransformPoint(newVector3(x, y, z));
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void DetachChildren();

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void SetAsFirstSibling();

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void SetAsLastSibling();

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void SetSiblingIndex(int index);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern int GetSiblingIndex();

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern Transform Find(string name);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern void INTERNAL_get_lossyScale(out Vector3value);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern bool IsChildOf(Transform parent);

        public Transform FindChild(string name)
        {
            return this.Find(name);
        }

        public IEnumerator GetEnumerator()
        {
            return new Transform.Enumerator(this);
        }

        [Obsolete("use Transform.Rotate instead.")]
        public void RotateAround(Vector3 axis,float angle)
        {
            Transform.INTERNAL_CALL_RotateAround(this,ref axis, angle);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern void INTERNAL_CALL_RotateAround(Transform self,ref Vector3 axis, float angle);

        [Obsolete("use Transform.Rotate instead.")]
        public void RotateAroundLocal(Vector3 axis,float angle)
        {
            Transform.INTERNAL_CALL_RotateAroundLocal(this,ref axis, angle);
        }

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        private static extern void INTERNAL_CALL_RotateAroundLocal(Transform self,ref Vector3 axis, float angle);

        [WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern Transform GetChild(int index);

        [Obsolete("use Transform.childCount instead."), WrapperlessIcall]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern int GetChildCount();
    }
}

你可能感兴趣的:(unity源码解析Transform)