您当前的位置: 首页 >  unity

CoderZ1010

暂无认证

  • 2浏览

    0关注

    168博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity 使用this关键字进行函数拓展 - Transform

CoderZ1010 发布时间:2020-10-12 09:54:03 ,浏览量:2

Example:

private void Start()
{
    transform
        .Parent(null)
        .NullParent()
        .CopyTransformValues(transform)
        .SiblingIndex(0)
        .AsFirstSibling()
        .AsLastSibling()

        .Position(Vector3.zero)
        .Position(0f, 0f, 0f)
        .PositionX(0f)
        .PositionY(0f)
        .PositionZ(0f)
        .PositionIdentity()

        .Rotation(Quaternion.identity)
        .Rotation(0f, 0f, 0f, 0f)
        .RotationX(0f)
        .RotationY(0f)
        .RotationZ(0f)
        .RotationW(0f)
        .RotationIdentity()

        .EulerAngles(Vector3.zero)
        .EulerAngles(0f, 0f, 0f)
        .EulerAnglesX(0f)
        .EulerAnglesY(0f)
        .EulerAnglesZ(0f)
        .EulerAnglesIdentity()

        .LocalPosition(Vector3.zero)
        .LocalPosition(0f, 0f, 0f)
        .LocalPositionX(0f)
        .LocalPositionY(0f)
        .LocalPositionZ(0f)
        .LocalPositionIdentity()

        .LocalRotation(Quaternion.identity)
        .LocalRotation(0f, 0f, 0f, 0f)
        .LocalRotationX(0f)
        .LocalRotationY(0f)
        .LocalRotationZ(0f)
        .LocalRotationW(0f)
        .LocalRotationIdentity()

        .LocalEulerAngles(Vector3.zero)
        .LocalEulerAngles(0f, 0f, 0f)
        .LocalEulerAnglesX(0f)
        .LocalEulerAnglesY(0f)
        .LocalEulerAnglesZ(0f)
        .LocalEulerAnglesIdentity()

        .LocalScale(Vector3.zero)
        .LocalScale(0f, 0f, 0f)
        .LocalScaleX(0f)
        .LocalScaleY(0f)
        .LocalScaleZ(0f);
}

Extension:

    /// 
    /// The extension of transform.
    /// 
    public static class TransformExtension
    {
        /// 
        /// Set parent as target.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T Parent(this T self, Component parent, bool worldPositionStays = true) where T : Component
        {
            self.transform.SetParent(parent ? parent.transform : null, worldPositionStays);
            return self;
        }
        /// 
        /// Set parent as null.
        /// 
        /// 
        /// 
        /// 
        public static T NullParent(this T self) where T : Component
        {
            self.transform.SetParent(null);
            return self;
        }
        /// 
        /// Copy the transfrom valus from target.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T CopyTransformValues(this T self, Component target) where T : Component
        {
            self.transform.position = target.transform.position;
            self.transform.eulerAngles = target.transform.eulerAngles;
            self.transform.localScale = target.transform.localScale;
            return self;
        }

        #region SiblingIndex
        /// 
        /// Set sibling index.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T SiblingIndex(this T self, int index) where T : Component
        {
            self.transform.SetSiblingIndex(index);
            return self;
        }
        /// 
        /// Set the sibling index as first.
        /// 
        /// 
        /// 
        /// 
        public static T AsFirstSibling(this T self) where T : Component
        {
            self.transform.SetAsFirstSibling();
            return self;
        }
        /// 
        /// Set the sibling index as last.
        /// 
        /// 
        /// 
        /// 
        public static T AsLastSibling(this T self) where T : Component
        {
            self.transform.SetAsLastSibling();
            return self;
        }
        #endregion

        #region Position
        /// 
        /// Get the position.
        /// 
        /// 
        /// 
        /// 
        public static Vector3 GetPosition(this T self) where T : Component
        {
            return self.transform.position;
        }
        /// 
        /// Set the position.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T Position(this T self, Vector3 pos) where T : Component
        {
            self.transform.position = pos;
            return self;
        }
        /// 
        /// Set the position.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T Position(this T self, float x, float y, float z) where T : Component
        {
            Vector3 pos = self.transform.position;
            pos.x = x;
            pos.y = y;
            pos.z = z;
            self.transform.position = pos;
            return self;
        }
        /// 
        /// Set the position x value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T PositionX(this T self, float x) where T : Component
        {
            Vector3 pos = self.transform.position;
            pos.x = x;
            self.transform.position = pos;
            return self;
        }
        /// 
        /// Set the position y value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T PositionY(this T self, float y) where T : Component
        {
            Vector3 pos = self.transform.position;
            pos.y = y;
            self.transform.position = pos;
            return self;
        }
        /// 
        /// Set the position z value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T PositionZ(this T self, float z) where T : Component
        {
            Vector3 pos = self.transform.position;
            pos.z = z;
            self.transform.position = pos;
            return self;
        }
        /// 
        /// Identity the position.
        /// 
        /// 
        /// 
        /// 
        public static T PositionIdentity(this T self) where T : Component
        {
            self.transform.position = Vector3.zero;
            return self;
        }
        #endregion

        #region Rotation
        /// 
        /// Get the rotation.
        /// 
        /// 
        /// 
        /// 
        public static Quaternion GetQuaternion(this T self) where T : Component
        {
            return self.transform.rotation;
        }
        /// 
        /// Set the rotation.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T Rotation(this T self, Quaternion rot) where T : Component
        {
            self.transform.rotation = rot;
            return self;
        }
        /// 
        /// Set the rotation.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T Rotation(this T self, float x, float y, float z, float w) where T : Component
        {
            Quaternion rot = self.transform.rotation;
            rot.x = x;
            rot.y = y;
            rot.z = z;
            rot.w = w;
            self.transform.rotation = rot;
            return self;
        }
        /// 
        /// Set the rotation x value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T RotationX(this T self, float x) where T : Component
        {
            Quaternion rot = self.transform.rotation;
            rot.x = x;
            self.transform.rotation = rot;
            return self;
        }
        /// 
        /// Set the rotation y value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T RotationY(this T self, float y) where T : Component
        {
            Quaternion rot = self.transform.rotation;
            rot.y = y;
            self.transform.rotation = rot;
            return self;
        }
        /// 
        /// Set the rotation z value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T RotationZ(this T self, float z) where T : Component
        {
            Quaternion rot = self.transform.rotation;
            rot.z = z;
            self.transform.rotation = rot;
            return self;
        }
        /// 
        /// Set the rotation w value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T RotationW(this T self, float w) where T : Component
        {
            Quaternion rot = self.transform.rotation;
            rot.w = w;
            self.transform.rotation = rot;
            return self;
        }
        /// 
        /// Identity the rotation.
        /// 
        /// 
        /// 
        /// 
        public static T RotationIdentity(this T self) where T : Component
        {
            self.transform.rotation = Quaternion.identity;
            return self;
        }
        #endregion

        #region EulerAngles
        /// 
        /// Get the euler angles.
        /// 
        /// 
        /// 
        /// 
        public static Vector3 GetEulerAngles(this T self) where T : Component
        {
            return self.transform.eulerAngles;
        }
        /// 
        /// Set the euler angles.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T EulerAngles(this T self, Vector3 eulerAngles) where T : Component
        {
            self.transform.eulerAngles = eulerAngles;
            return self;
        }
        /// 
        /// Set the euler angles.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T EulerAngles(this T self, float x, float y, float z) where T : Component
        {
            Vector3 eulerAngles = self.transform.eulerAngles;
            eulerAngles.x = x;
            eulerAngles.y = y;
            eulerAngles.z = z;
            self.transform.eulerAngles = eulerAngles;
            return self;
        }
        /// 
        /// Set the euler angles x value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T EulerAnglesX(this T self, float x) where T : Component
        {
            Vector3 eulerAngles = self.transform.eulerAngles;
            eulerAngles.x = x;
            self.transform.eulerAngles = eulerAngles;
            return self;
        }
        /// 
        /// Set the euler angles y value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T EulerAnglesY(this T self, float y) where T : Component
        {
            Vector3 eulerAngles = self.transform.eulerAngles;
            eulerAngles.y = y;
            self.transform.eulerAngles = eulerAngles;
            return self;
        }
        /// 
        /// Set the euler angles z value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T EulerAnglesZ(this T self, float z) where T : Component
        {
            Vector3 eulerAngles = self.transform.eulerAngles;
            eulerAngles.z = z;
            self.transform.eulerAngles = eulerAngles;
            return self;
        }
        /// 
        /// Identity the euler angles.
        /// 
        /// 
        /// 
        /// 
        public static T EulerAnglesIdentity(this T self) where T : Component
        {
            self.transform.eulerAngles = Vector3.zero;
            return self;
        }
        #endregion

        #region LossyScale
        /// 
        /// Get the lossy scale.
        /// 
        /// 
        /// 
        /// 
        public static Vector3 GetLossyScale(this T self) where T : Component
        {
            return self.transform.lossyScale;
        }
        #endregion

        #region LocalPosition
        /// 
        /// Get the local position.
        /// 
        /// 
        /// 
        /// 
        public static Vector3 GetLocalPosition(this T self) where T : Component
        {
            return self.transform.localPosition;
        }
        /// 
        /// Set the local position.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalPosition(this T self, Vector3 localPos) where T : Component
        {
            self.transform.localPosition = localPos;
            return self;
        }
        /// 
        /// Set the local position.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalPosition(this T self, float x, float y, float z) where T : Component
        {
            Vector3 localPos = self.transform.localPosition;
            localPos.x = x;
            localPos.y = y;
            localPos.z = z;
            self.transform.localPosition = localPos;
            return self;
        }
        /// 
        /// Set the local position x value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalPositionX(this T self, float x) where T : Component
        {
            Vector3 localPos = self.transform.localPosition;
            localPos.x = x;
            self.transform.localPosition = localPos;
            return self;
        }
        /// 
        /// Set the local position y value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalPositionY(this T self, float y) where T : Component
        {
            Vector3 localPos = self.transform.localPosition;
            localPos.y = y;
            self.transform.localPosition = localPos;
            return self;
        }
        /// 
        /// Set the local position z value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalPositionZ(this T self, float z) where T : Component
        {
            Vector3 localPos = self.transform.localPosition;
            localPos.z = z;
            self.transform.localPosition = localPos;
            return self;
        }
        /// 
        /// Identity the local position.
        /// 
        /// 
        /// 
        /// 
        public static T LocalPositionIdentity(this T self) where T : Component
        {
            self.transform.localPosition = Vector3.zero;
            return self;
        }
        #endregion

        #region LocalRotation
        /// 
        /// Get the local rotation.
        /// 
        /// 
        /// 
        /// 
        public static Quaternion GetLocalRotation(this T self) where T : Component
        {
            return self.transform.localRotation;
        }
        /// 
        /// Set the local rotation.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalRotation(this T self, Quaternion rot) where T : Component
        {
            self.transform.localRotation = rot;
            return self;
        }
        /// 
        /// Set the local rotation.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalRotation(this T self, float x, float y, float z, float w) where T : Component
        {
            Quaternion rot = self.transform.localRotation;
            rot.x = x;
            rot.y = y;
            rot.z = z;
            rot.w = w;
            self.transform.localRotation = rot;
            return self;
        }
        /// 
        /// Set the local rotation x value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalRotationX(this T self, float x) where T : Component
        {
            Quaternion rot = self.transform.localRotation;
            rot.x = x;
            self.transform.localRotation = rot;
            return self;
        }
        /// 
        /// Set the local rotation y value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalRotationY(this T self, float y) where T : Component
        {
            Quaternion rot = self.transform.localRotation;
            rot.y = y;
            self.transform.localRotation = rot;
            return self;
        }
        /// 
        /// Set the local rotation z value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalRotationZ(this T self, float z) where T : Component
        {
            Quaternion rot = self.transform.localRotation;
            rot.z = z;
            self.transform.localRotation = rot;
            return self;
        }
        /// 
        /// Set the local rotation w value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalRotationW(this T self, float w) where T : Component
        {
            Quaternion rot = self.transform.localRotation;
            rot.w = w;
            self.transform.localRotation = rot;
            return self;
        }
        /// 
        /// Identity the local rotation.
        /// 
        /// 
        /// 
        /// 
        public static T LocalRotationIdentity(this T self) where T : Component
        {
            self.transform.localRotation = Quaternion.identity;
            return self;
        }
        #endregion

        #region LocalEulerAngles
        /// 
        /// Get the local euler angles.
        /// 
        /// 
        /// 
        /// 
        public static Vector3 GetLocalEulerAngles(this T self) where T : Component
        {
            return self.transform.localEulerAngles;
        }
        /// 
        /// Set the local euler angles.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalEulerAngles(this T self, Vector3 localEulerAngles) where T : Component
        {
            self.transform.localEulerAngles = localEulerAngles;
            return self;
        }
        /// 
        /// Set the local euler angles.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalEulerAngles(this T self, float x, float y, float z) where T : Component
        {
            Vector3 localEulerAngles = self.transform.localEulerAngles;
            localEulerAngles.x = x;
            localEulerAngles.y = y;
            localEulerAngles.z = z;
            self.transform.localEulerAngles = localEulerAngles;
            return self;
        }
        /// 
        /// Set the local euler angles x value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalEulerAnglesX(this T self, float x) where T : Component
        {
            Vector3 localEulerAngles = self.transform.localEulerAngles;
            localEulerAngles.x = x;
            self.transform.localEulerAngles = localEulerAngles;
            return self;
        }
        /// 
        /// Set the local euler angles y value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalEulerAnglesY(this T self, float y) where T : Component
        {
            Vector3 localEulerAngles = self.transform.localEulerAngles;
            localEulerAngles.y = y;
            self.transform.localEulerAngles = localEulerAngles;
            return self;
        }
        /// 
        /// Set the local euler angles z value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalEulerAnglesZ(this T self, float z) where T : Component
        {
            Vector3 localEulerAngles = self.transform.localEulerAngles;
            localEulerAngles.z = z;
            self.transform.localEulerAngles = localEulerAngles;
            return self;
        }
        /// 
        /// Identity the local euler angles.
        /// 
        /// 
        /// 
        /// 
        public static T LocalEulerAnglesIdentity(this T self) where T : Component
        {
            self.transform.localEulerAngles = Vector3.zero;
            return self;
        }
        #endregion

        #region LocalScale
        /// 
        /// Get the local scale.
        /// 
        /// 
        /// 
        /// 
        public static Vector3 GetLocalScale(this T self) where T : Component
        {
            return self.transform.localScale;
        }
        /// 
        /// Set the local scale.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalScale(this T self, Vector3 localScale) where T : Component
        {
            self.transform.localScale = localScale;
            return self;
        }
        /// 
        /// Set the local scale.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalScale(this T self, float x, float y, float z) where T : Component
        {
            Vector3 localScale = self.transform.localScale;
            localScale.x = x;
            localScale.y = y;
            localScale.z = z;
            self.transform.localScale = localScale;
            return self;
        }
        /// 
        /// Set the local scale x value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalScaleX(this T self, float x) where T : Component
        {
            Vector3 localScale = self.transform.localScale;
            localScale.x = x;
            self.transform.localScale = localScale;
            return self;
        }
        /// 
        /// Set the local scale y value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalScaleY(this T self, float y) where T : Component
        {
            Vector3 localScale = self.transform.localScale;
            localScale.y = y;
            self.transform.localScale = localScale;
            return self;
        }
        /// 
        /// Set the local scale z value.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T LocalScaleZ(this T self, float z) where T : Component
        {
            Vector3 localScale = self.transform.localScale;
            localScale.z = z;
            self.transform.localScale = localScale;
            return self;
        }
        /// 
        /// Identity the local scale.
        /// 
        /// 
        /// 
        /// 
        public static T LocalScaleIdentity(this T self) where T : Component
        {
            self.transform.localScale = Vector3.one;
            return self;
        }
        #endregion

        #region Identity
        /// 
        /// Identity the transform.
        /// 
        /// 
        /// 
        /// 
        public static T Identity(this T self) where T : Component
        {
            self.transform.position = Vector3.zero;
            self.transform.eulerAngles = Vector3.zero;
            self.transform.localScale = Vector3.one;
            return self;
        }
        /// 
        /// Identity the local transform.
        /// 
        /// 
        /// 
        /// 
        public static T LocalIdentity(this T self) where T : Component
        {
            self.transform.localPosition = Vector3.zero;
            self.transform.localEulerAngles = Vector3.zero;
            self.transform.localScale = Vector3.one;
            return self;
        }
        #endregion
    }
关注
打赏
1653184800
查看更多评论
立即登录/注册

微信扫码登录

0.1225s