本帖最后由 da11 于 2021-3-14 23:23 编辑
//以下讲解Transform变换物体组件的基本使用方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TransformDemo : MonoBehaviour
{
///<summary>
///Transform类提供了查找(父、根、子)变换组件、改变位置、角度、大小功能
///</summary>
/*
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
*/
public Transform tf;
private void OnGUI()
{
if (GUILayout.Button("恢复位置"))
{
this.transform.position = new Vector3(0, 0, 0);
this.transform.rotation = new Quaternion(0, 0, 0,0);
foreach (Transform child in this.transform)
{
child.position = new Vector3(0,0,0);
}
}
if (GUILayout.Button("Transform组件测试按钮1"))
{
//遍历Transform组件的物体名字及改变其世界坐标
Debug.Log("物体名字是:" + this.transform.name + " 对应的世界坐标:" + this.transform.position);
int xWZ = 1;
//Transform child = this.GetComponent<Transform>();
foreach (Transform child in this.transform)
{
//child为每个子物体的变换组件(只能遍历一代子物体)
//child.position += Vector3.up * 10.0F;
//print(child.name);
//为每个遍历的子物体变换世界空间位置--position属性
child.position = new Vector3(0 + xWZ, 0 + xWZ);
Debug.Log("物体名字是:"+child.name+" 对应的世界坐标:"+child.position);
xWZ++;
}
}
if (GUILayout.Button("Transform组件测试按钮2"))
{
//首先更改父物体的世界坐标
this.transform.position = new Vector3(2, 3, 1);
//遍历Transform组件的物体名字及改变其相对父物体轴心点的坐标
Debug.Log("物体名字是:" + this.transform.name + " 对应的世界坐标:" + this.transform.position);
float xWZ = 1;
//Transform child = this.GetComponent<Transform>();
foreach (Transform child in this.transform)
{
//child为每个子物体的变换组件(只能遍历一代子物体)
//child.position += Vector3.up * 10.0F;
//print(child.name);
//为每个遍历的子物体变换相对父物体轴心点的位置--localPosition属性
child.localPosition = new Vector3(0 + xWZ, 0 + xWZ);
Debug.Log("物体名字是:" + child.name + " 对应的世界坐标:" + child.position);
xWZ+=0.5F;
}
}
if (GUILayout.Button("Transform组件测试按钮3"))
{
//相对与世界坐标旋转角度的属性
this.transform.rotation = new Quaternion(0, 90, 0, 0);
//相对与父类物体旋转角度的属性
this.transform.localRotation = new Quaternion(0, 90, 0, 0);
//相对与父类物体缩放比例的属性
this.transform.localScale = new Vector3(1, 2, 1);
//可以理解为:物体与模型缩放比例(自身缩放比例*父物体缩放比例)
//this.transform.lossyScale
//如:父物体localScale为3,当前物体localScale为2,则lossyScale为6
}
//GUILayout类型的RepeatButton方法代表这个按钮不松手,则会一直执行(重点)
if (GUILayout.RepeatButton("Transform组件测试-平移方法"))
{
//向自身坐标系Z轴移动一米(根据物体自身的坐标移动)
//this.transform.Translate(0, 0, 1);
//向世界坐标系Z轴移动一米(根据场景界面的世界坐标移动)
//this.transform.Translate(0, 0, 1,Space.World);
//向自身坐标系Z轴旋转一度(根据物体自身的坐标旋转)
//this.transform.Rotate(0, 0, 1);
//向世界坐标系Z轴旋转一度(根据场景界面的世界坐标旋转)
//this.transform.Rotate(0, 10, 0, Space.World);
//小知识:Vector3类型的zero属性是世界原点(重点!)
//Vector3类型的up属性是世界的y轴
/*Vector3类型的常用属性代表的xyz轴为:(重点)
* up,0,1,0
* down,0,-1,0
* left,-1,0,0
* right,1,0,0
* forward,0,0,1
* back,0,0,-1
* zero,0,0,0
* one,1,1,1
*/
//transform.RotateAround方法两个形参的重载已经否决,只有三个形参的重载还保留
//第一个参数的含义是,从哪个点开始,第二个参数是沿那个轴,第三个参数是围绕角度(一帧转的角度)
this.transform.RotateAround(Vector3.zero, Vector3.up, 1);
Debug.Log("现在的坐标,x轴: "+this.transform.position.x +" y轴: "+ this.transform.position.y +" z轴: "+ this.transform.position.z);
Debug.Log("现在的角度,x轴角度: " + this.transform.rotation.x + " y轴角度: " + this.transform.rotation.y + " z轴角度: " + this.transform.rotation.z);
}
if (GUILayout.Button("获取根物体的transform组件按钮"))
{
Transform rootTransObj = this.transform.root;
Debug.Log(rootTransObj.name);
}
if (GUILayout.Button("获取父物体的transform组件按钮"))
{
Transform parentTransObj = this.transform.parent;
Debug.Log(parentTransObj.name);
}
if (GUILayout.Button("设置父物体"))
{
//this.transform.SetParent默认第二个参数是bool值true
//设置父物体:当前物体的位置视为世界坐标
//this.transform.SetParent(tf);
//设置父物体:当前物体的位置视为localPosition(自身坐标)
this.transform.SetParent(tf, false);
}
if (GUILayout.Button("查找子物体"))
{
//根据名称获取子物体
//this.transform.Find("【写要查找的子物体名称】");
//注意,查找子物体可以使用Find,在子物体下一级的子物体,Find要加斜杠“/”标明路径才可以查找,可以有多个斜杠,如下
//this.transform.Find("【写要查找的子物体名称/在下一级的子物体名称/第3级的子物体名称/第4级的子物体名称】");
//温馨提示:不建议这么写
//Transform childTrans = this.transform.Find("Cube");
//查找子物体的数量有多少
int childCountNum = this.transform.childCount;
for (int i = 0; i < childCountNum; i++)
{
//this.transform.GetChild是根据索引获取子物体transform组件的方法
//for循环将this.transform.GetChild索引,获取到的给childTrans
Transform childTrans = this.transform.GetChild(i);
}
}
if (GUILayout.Button("练习按钮-查找子物体"))
{
Transform checkChildOBJ = GetNameFindGameObject.GetNameFindGameObjectSF(this.transform, "Cube (24)");
//GetNameFindGameObject.DGFindChild(this.transform, "Cube (5)");
if (checkChildOBJ != null)
{
Debug.Log("已找到 " + checkChildOBJ.name + " 的游戏物体");
checkChildOBJ.GetComponent<MeshRenderer>().material.color = Color.yellow;
}
else
{
Debug.Log("没有找到游戏物体");
}
}
if (GUILayout.Button("解除孩子关系和父子"))
{
//解除Cube(5)的孩子关系
//GetNameFindGameObject.GetNameFindGameObjectSF(this.gameObject, "Cube (24)").transform.DetachChildren();
//将Cube (5)父物体设置为null
//GetNameFindGameObject.GetNameFindGameObjectSF(this.gameObject, "Cube (24)").transform.SetParent(null);
}
}
}
|