zoukankan      html  css  js  c++  java
  • 《Unity_API解析》 第十二章 Transform类

    Transform类继承自Component类,并实现了IEnumberable接口。Transform是GameObject必须拥有的一个组件,用来管理所在GameObject对象的坐标位置、旋转角度和大小缩放。由于Transform实现了Ienumberable接口,于是可以在程序中使用foreach()方法快速遍历子物体的Transform结构。即:

     void Start()
        {
            foreach (Transform item in transform)
            {
     
            }
        }
     
    Transform类实例属性
    eulerAngles属性:欧拉角
    public Vector3 eulerAngles{get; set;}
    此功能用于返回和设置GameObject对象的欧拉角。
     
    forward属性:z轴单位向量
    public Vector3 forward{get; set;}
    此属性用于返回或设置transform自身坐标系中z轴方向的单位向量对应的世界坐标系中的单位向量,transform.forward即为transform.TransformDirection(new Vector3(0.0f, 0.0f, 0.0f))的简化方式。
     
    hasChanged属性:transform组件是否被修改
    public bool hasChanged{get; set;}
    此属性用于判断GameObject对象上次将此属性设为false以来,其transform组件的此属性是否被修改过,默认值为true。
     
    localPosition属性:局部坐标系为值
    public Vector3 localPosition{get; set;}
    此属性用于设置或返回GameObject对象在局部坐标系的位置,若无父级对象则和属性Transform.positon相同。transform.localPosition的值受父级对象lossyScale的影响。
     
    localToWorldMatrix属性:转换矩阵
    public Matrix4x4 localToWorldMatrix{get;}
    此属性用于返回从transofrm局部坐标系向世界坐标系转换的Matrix4x4矩阵。例设A为Vector3,B为Transform实例,且B的x、y、z轴方向与世界坐标系方向一致。
    Vector3 vec3 = B.localToWorldMatrix.MultiplyPoint3x4(A);
    则分量值vec3.x = B.Position.x + A.x * B.lossyScale.x。
    一般情况可用TransformPoint(positon : Vector3)来实现Vector3实例从transform局部坐标系向世界坐标系的转换。
     
    parent属性:父物体Transform实例
    public Transform parent{get; set;}
    此属性用于返回父物体的Transform实例。若要返回transform的最顶层的父物体,可以使用transform.root属性。
     
    worldToLocalMatrix属性:转换矩阵
    public Matrix4x4 worldToLocalMatrix{get;}
    此属性用于返回物体从世界坐标系向transform自身坐标系转换的Matrix4x4矩阵。
    一般可用方法InverseTransformPoint(position : Vector3)来实现Vector3实例从世界坐标系向transform自身坐标系转换。
     
    Transform类实例方法
    DetachChildren方法:分离物体层级关系
    public void DetachChidren();
    此方法的功能是使GameObject对象的所有子物体和自身分离层级关系。
     
    GetChild方法:获取GameObject对象子类
    public Transform GetChild(int index);
    参数为子物体索引值。
    此方法用于返回transform的索引值为index的子类Transfrom实例。参数index的值要小于transform的childCount的值。
     
    InverseTransformDirection方法:坐标系转换
    public Vector3 InverseTransformDirection(Vector3 direction);
    public Vector3 InverseTransformDirection(float x, float y, float z);
    此方法用于将参数direction从世界坐标系转换到GameObject对象的局部坐标系。
     
    InverseTransfromPoint方法:点的相对坐标向量
    public Vector3 InverseTransformPoint(Vector3 position);
    public  Vector3 InverseTransformPoint(float x, float y ,float z);
    此方法用于返回参数position向量相对于GameObject对象局部坐标系的差向量,返回值受transform.lossyScale和transform.rotation影响。
     
    IsChildOf方法:是否为子物体
    public bool IsChildOf(Transform parent);
    参数parent为父物体的Transform实例。此方法用于判断transform对应的GameObject对象是否为参数parent的子物体。
     
    LookAt方法:物体朝向
    public void LookAt(Transform target);
    public void LookAt(Vector3 worldPosition);
    public void LookAt(Transform target, Vector3 worldUp);
     
    public void LookAt(Vector3 worldPosition, Vector3 worldUp);
    其中参数target为transform自身坐标系指向的目标,参数worldUp为transform自身坐标系中y轴最大限度指向的方向。
    此方法的功能为使GameObject对象的z轴指向target,若自定义worldUp的方向,则GameObject对象的forword方向一直指向target,然后transform绕着自身坐标系的z轴即forward方向旋转到一个使得自身的y轴方向最接近worldUp的地方。
     
    Rotate方法:绕坐标轴或某个向量旋转
    public void Rotate(Vector3 eulerAngles);
    public void Rotate(Vector3 eulerAngles, Space relativeTo);
    public void Rotate(float xAngle, float yAngle, float zAngle);
    public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo);
    public void Rotate(Vector3 axis, float angle);
    public void Rotate(Vector3 axis, float angle, Space relativeTo);
    此方法的前四个重载是使得transform实例在相对参数relativeTo的坐标系中旋转欧拉角eulerAngles,后两个重载是使得GameObject对象在relativeTo坐标系中绕轴向量axis旋转angle度。
    RotateAround方法:绕轴点旋转
    public void RotateAround(Vector3 axis, float angle);
    public void RotateAround(Vector3 point, Vector3 axis, float angle);
    其中参数point为参考点坐标,参数axis为旋转轴方向,参数angle为旋转角度。
     
    TransfromDirection方法:坐标系转换
    public Vector3 TransformDirection(Vector3 direction);
    public Vector3 TransformDirection(float x, float y, float z);
    此方法用于将向量direction从transform局部坐标系转换到世界坐标系。
     
    TransformPoint方法:点的世界坐标位置
    public Vector3 TransformPoint(Vector3 position);
    public Vector3 TransformPoint(float x, float y, float z);
    此方法用于返回GameObject对象局部坐标系中position在世界坐标系中的位置。
     
    Translate方法:相对坐标系移动或相对其他物体移动
    public void Translate(Vector3 translation);
    public void Translate(Vector3 translation, Space relativeTo);
    public void Translate(float x, float y, float z);
    public void Translate(float x, float y, float z, Space relativeTo);
    public void Translate(Vector3 translation, Transform relativeTo);
    public void Translate(float x, float y, float z,  Transform relativeTo);
     
    此方法前四个重载的功能是使得GameObject对象在relativeTo的坐标系空间中移动参数translation指定的向量,后两个重载的功能是使得GameObject对象在相对relativeTo的坐标系中移动向量translation。
     
    关于localScale和lossyScale的注解:
    当GameObject对象A为GameObject对象B的父物体时,父物体A的各个分量放缩值x、y、z的大小应该保持1:1:1的比例,否则当子物体B的Rotation值比例不为1:1:1时,B物体将会发生变形。
    设GameObject对象A为B的父物体,当A物体各个分量的放缩值保持1:1:1的比例时,子物体B的lossyScale返回值即为B物体相对世界坐标系的放缩值,关系为B.localScale = B.lossyScale/A.localScale。
     
  • 相关阅读:
    hmset
    java 调用mongo 判断大于等于 并且小约等于<=
    Maven项目,别人的没问题,自己机器一直有问题
    linux 时间datetimectl 问题
    真正手把手教 git
    0324-SQLMAP使用参数备注
    安全推荐网址:
    JavaScript Base64 作为文件上传的实例代码解析
    学习笔记|变量的解构赋值
    学习笔记|let 和 const 命令
  • 原文地址:https://www.cnblogs.com/colve/p/5968379.html
Copyright © 2011-2022 走看看