欢迎大家学习指正
目录
一、Transform类简介
Transform类提供了查找(父、根、子)变换组件、改变位置、角度、大小功能
场景中的每一个对象都有一个Transform。用于储存并操控物体的位置、旋转和缩放。每一个Transform可以有一个父级,允许你分层次应用位置、旋转和缩放。可以在Hierarchy面板查看层次关系。他们也支持计数器(enumerator),因此你可以使用循环遍历子对象。
二、个别核心功能介绍
1.查找变换组件
代码如下(示例):
if (GUILayout.Button("foreach -- transform"))
{
foreach (Transform child in this.transform)
{
//child 是 每个子物体的变换组件
//child.position += Vector3.up * 10.0F;
print(child.name);
}
}ps:该处是查找当前物体的子物体的变换组件并且打印出来
当前层级(cube)

测试:

2.改变位置、角度
1.缩放
缩放会根据父物体和自身进行等比例缩放
if (GUILayout.Button("foreach--position"))
{
物体相对于世界坐标系原点的位置
this.transform.position
物体相对于父物体轴心点的位置
this.transform.localPosition
物体相对于父物体缩放比例 1 2 1
this.transform.localScale
理解:物体与模型缩放比例(自身缩放比例*父物体缩放比例)
this.transform.lossyScale
如:父物体localScale为3 当前物体localScale为2
lossyScale 则为6
}
2.移动
if(GUILayout.Button("pos/scale"))
{
//向z轴移动1m
//this.transform.Translate(0, 0, 1);
//向世界坐标z轴移动1m
this.transform.Translate(0, 0, 1,Space.World);
}
测试:
初始

移动后:
ps:这里我移动了很多下

3.旋转
if (GUILayout.Button("Rotate"))
{
// 沿自身坐标系 y轴旋转10°
//this.transform.Rotate(0, 10, 0);
//沿自身坐标系 y轴旋转10°
this.transform.Rotate(0, 10, 0, Space.World);
}
ps:这个测试是后者沿自身转

测试后:

4.设置父物体
if (GUILayout.Button("SetParent"))
{
//设置父物体
//当前物体的位置 视为 世界坐标
//this.transform.SetParent(tf);
//当前物体的位置 视为localPosition
this.transform.SetParent(tf,false);
}
类中设置public Transform tf;
测试:


测试后:

总结
这里对文章进行总结:
以上就是今天要讲的内容,本文仅仅简单介绍了Transform类,还有很多类api手册里都有暂时测试这么多
本文介绍了Unity引擎中的Transform组件,详细讲解了如何通过Transform组件来实现物体的位置、旋转和缩放等基本操作,并演示了如何设置物体间的父子关系。
1313

被折叠的 条评论
为什么被折叠?



