目錄
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 updatevoid 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 framevoid 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 updatevoid 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 framevoid 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 updatevoid 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 framevoid 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 updatevoid 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 framevoid 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 updatevoid 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 framevoid 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 updatevoid 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 framevoid 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 updatevoid 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 framevoid Update(){}
}
銷毀游戲物體
Transform.Destroy(GrisGO);