UnityAPI学习之Transform组件基本使用

目录

Transform组件

访问与获取

Transform的位置和旋转信息

Transform局部坐标和旋转信息的获取

Transform的缩放与正方向

缩放(Scale)

正方向

Transform相关的查找方法

销毁游戏物体


Transform组件

访问与获取

现在创建一个容器放置GrisGO物体的、Transform组件并输出其名字

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO5_Transform : MonoBehaviour
{
    public GameObject GrisGO;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(GrisGO.transform);
        Transform GrisTrans = GrisGO.transform;
        Debug.Log("GrisGO所挂载的物体的transform组件的名字为:" + GrisTrans);
        Debug.Log("GrisGO所挂载的游戏物体引用为" + GrisTrans.gameObject);
        Debug.Log("GrisGO下的子物体(Transform)个数为" + GrisTrans.childCount);
            
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Transform的位置和旋转信息

示例1:获取GrisGO的位置信息和旋转信息

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO5_Transform : MonoBehaviour
{
    public GameObject GrisGO;
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log(GrisGO.transform);
        Transform GrisTrans = GrisGO.transform;
        /*Debug.Log("GrisGO所挂载的物体的transform组件的名字为:" + GrisTrans);
        Debug.Log("GrisGO所挂载的游戏物体引用为" + GrisTrans.gameObject);
        Debug.Log("GrisGO下的子物体(Transform)个数为" + GrisTrans.childCount);*/
        Debug.Log("GrisGO世界空间的坐标位置为" + GrisTrans.position);
        Debug.Log("GrisGO以四元数形式表示的旋转是" + GrisTrans.rotation);

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

示例2:

在以上实例中,获取的旋转信息是以四元数的形式表示出来(如在rotation属性中,x=1.58,但实际输出的四元数为0.01379),现在要将rotation属性以其原本的数值(欧拉角/度)形式展现出来

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO5_Transform : MonoBehaviour
{
    public GameObject GrisGO;
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log(GrisGO.transform);
        Transform GrisTrans = GrisGO.transform;
        /*Debug.Log("GrisGO所挂载的物体的transform组件的名字为:" + GrisTrans);
        Debug.Log("GrisGO所挂载的游戏物体引用为" + GrisTrans.gameObject);
        Debug.Log("GrisGO下的子物体(Transform)个数为" + GrisTrans.childCount);*/
        Debug.Log("GrisGO世界空间的坐标位置为" + GrisTrans.position);
        Debug.Log("GrisGO以四元数形式表示的旋转是" + GrisTrans.rotation);
        Debug.Log("GrisGO以欧拉角/度的形式表示旋转结果为" + GrisTrans.eulerAngles);

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Transform局部坐标和旋转信息的获取

局部坐标:子级相对于父级的位置信息

示例:输出GrisGO的局部坐标和旋转欧拉角

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO5_Transform : MonoBehaviour
{
    public GameObject GrisGO;
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log(GrisGO.transform);
        Transform GrisTrans = GrisGO.transform;
        /*Debug.Log("GrisGO所挂载的物体的transform组件的名字为:" + GrisTrans);
        Debug.Log("GrisGO所挂载的游戏物体引用为" + GrisTrans.gameObject);
        Debug.Log("GrisGO下的子物体(Transform)个数为" + GrisTrans.childCount);*/
        Debug.Log("GrisGO世界空间的坐标位置为" + GrisTrans.position);
        Debug.Log("GrisGO局部坐标为" + GrisTrans.localPosition);
        Debug.Log("GrisGO以四元数形式表示的旋转是" + GrisTrans.rotation);
        Debug.Log("GrisGO以欧拉角/度的形式表示旋转结果为" + GrisTrans.eulerAngles);
        Debug.Log("GrisGO的局部欧拉角/度的形式表示旋转结果为" + GrisTrans.localEulerAngles);

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Transform的缩放与正方向

缩放(Scale)

对于Scale属性,只有局部缩放LocalScale

示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO5_Transform : MonoBehaviour
{
    public GameObject GrisGO;
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log(GrisGO.transform);
        Transform GrisTrans = GrisGO.transform;
        /*Debug.Log("GrisGO所挂载的物体的transform组件的名字为:" + GrisTrans);
        Debug.Log("GrisGO所挂载的游戏物体引用为" + GrisTrans.gameObject);
        Debug.Log("GrisGO下的子物体(Transform)个数为" + GrisTrans.childCount);*/
        /*Debug.Log("GrisGO世界空间的坐标位置为" + GrisTrans.position);
        Debug.Log("GrisGO局部坐标为" + GrisTrans.localPosition);
        Debug.Log("GrisGO以四元数形式表示的旋转是" + GrisTrans.rotation);
        Debug.Log("GrisGO以欧拉角/度的形式表示旋转结果为" + GrisTrans.eulerAngles);
        Debug.Log("GrisGO的局部欧拉角/度的形式表示旋转结果为" + GrisTrans.localEulerAngles);*/
        Debug.Log("GrisGO的局部缩放为" + GrisTrans.localScale);


    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

正方向

正方向即关于xyz轴所指的方向,其中分为有相对于世界空间的正方向和本地正方向(若无父级,则本地正方向与世界正方向相同),根据实际开发中的需求不同,调整相应的世界空间或者本地正方向的数值大小

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO5_Transform : MonoBehaviour
{
    public GameObject GrisGO;
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log(GrisGO.transform);
        Transform GrisTrans = GrisGO.transform;
        /*Debug.Log("GrisGO所挂载的物体的transform组件的名字为:" + GrisTrans);
        Debug.Log("GrisGO所挂载的游戏物体引用为" + GrisTrans.gameObject);
        Debug.Log("GrisGO下的子物体(Transform)个数为" + GrisTrans.childCount);*/
        /*Debug.Log("GrisGO世界空间的坐标位置为" + GrisTrans.position);
        Debug.Log("GrisGO局部坐标为" + GrisTrans.localPosition);
        Debug.Log("GrisGO以四元数形式表示的旋转是" + GrisTrans.rotation);
        Debug.Log("GrisGO以欧拉角/度的形式表示旋转结果为" + GrisTrans.eulerAngles);
        Debug.Log("GrisGO的局部欧拉角/度的形式表示旋转结果为" + GrisTrans.localEulerAngles);*/
        ///Debug.Log("GrisGO的局部缩放为" + GrisTrans.localScale);
        Debug.Log("GrisGO关于x轴的正方向为" + GrisTrans.right);
        Debug.Log("GrisGO关于y轴的正方向为" + GrisTrans.up);
        Debug.Log("GrisGO关于z轴的正方向为" + GrisTrans.forward);

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Transform相关的查找方法

示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO5_Transform : MonoBehaviour
{
    public GameObject GrisGO;
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log(GrisGO.transform);
        Transform GrisTrans = GrisGO.transform;
        /*Debug.Log("GrisGO所挂载的物体的transform组件的名字为:" + GrisTrans);
        Debug.Log("GrisGO所挂载的游戏物体引用为" + GrisTrans.gameObject);
        Debug.Log("GrisGO下的子物体(Transform)个数为" + GrisTrans.childCount);*/
        /*Debug.Log("GrisGO世界空间的坐标位置为" + GrisTrans.position);
        Debug.Log("GrisGO局部坐标为" + GrisTrans.localPosition);
        Debug.Log("GrisGO以四元数形式表示的旋转是" + GrisTrans.rotation);
        Debug.Log("GrisGO以欧拉角/度的形式表示旋转结果为" + GrisTrans.eulerAngles);
        Debug.Log("GrisGO的局部欧拉角/度的形式表示旋转结果为" + GrisTrans.localEulerAngles);*/
        ///Debug.Log("GrisGO的局部缩放为" + GrisTrans.localScale);
        Debug.Log("GrisGO关于x轴的正方向为" + GrisTrans.right);
        Debug.Log("GrisGO关于y轴的正方向为" + GrisTrans.up);
        Debug.Log("GrisGO关于z轴的正方向为" + GrisTrans.forward);


        //查找
        Debug.Log("当前脚本挂载的游戏对象下的叫Gris的子对象身上的Transform组件是" + transform.Find("Gris"));
        //索引
        Debug.Log("当前脚本挂载的游戏对象下的叫Gris的子对象身上的Transform组件是" + transform.GetChild(0));
        Debug.Log("Gris当前在此父对象同级里所在的索引位置" + GrisTrans.GetSiblingIndex());
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

销毁游戏物体

Transform.Destroy(GrisGO);

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值