当前位置:网站首页>Unity脚本API—Transform 变换
Unity脚本API—Transform 变换
2022-07-04 14:13:00 【@夜魅】
场景中的每一个对象都有一个Transform。用于储存并操控物体的位置、旋转和缩放。每一个Transform可以有一个父级,允许你分层次应用位置、旋转和缩放。可以在Hierarchy面板查看层次关系。他们也支持计数器(enumerator),因此你可以使用循环遍历子对象。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Transfrom 提供了查找(父、根、子)变换组件功能、改变位置、角度、大小功能
/// </summary>
public class TransfromDemo : MonoBehaviour
{
public Transform tf;
private void OnGUI()
{
if (GUILayout.Button("foreach -- transfrom"))
{
//场景中的每一个对象都有一个Transfrom,用于存储并操控物体的位置、旋转、缩放。
//每一个Transfrom可以有一个父级,允许你分层次应用位置、旋转和缩放。
//可以在Hierarchy面板查看层次关系。他们也支持计算器,因此你可以使用循环遍历子对象
foreach (Transform child in this.transform)
{
//child为每个子物体的变换组件
print(child.name);
}
}
if (GUILayout.Button("root"))
{
//获取根物体变换组件
Transform rootTF = this.transform.root;
Debug.Log(rootTF.name);
}
if (GUILayout.Button("parent"))
{
//获取父物体变换组件
Transform parentTF = this.transform.parent;
Debug.Log(parentTF.name);
}
if (GUILayout.Button("Setparent"))
{
//设置父物体
//当前物体的位置 视为世界坐标position
//this.transform.SetParent(tf,true);
//当前物体的位置 视为localPosition
this.transform.SetParent(tf,false);
}
if (GUILayout.Button("Find"))
{
//根据名称获取子物体
Transform tf = this.transform.Find("子物体名称");
Debug.Log(tf.name);
Transform tf1 = this.transform.Find("子物体名称/子物体名称");
Debug.Log(tf1.name);
}
if (GUILayout.Button("GetChild"))
{
int count = this.transform.childCount;
//根据索引获取子物体 不能获取孙子物体
for (int i = 0;i < count;i++)
{
Transform childTf = this.transform.GetChild(i);
}
}
//练习:在层级未知情况下查找子物体
if (GUILayout.Button("pos / scale"))
{
//物体相对于世界坐标系原点的位置
//this.transform.position;
//物体相对于父物体轴心点的位置
//this.transform.localPosition;
//相对于父物体缩放比例 1 2 1
//this.transform.localScale;
//理解为:物体与模型缩放比例(自身缩放比例 * 父物体缩放比例)
//this.transfrom.lossyScale
//如:父物体localScale为3 当前物体localScale为2
// lossyScale则为6
}
if (GUILayout.Button("Translate"))
{
//向自身坐标系 Z轴 移动1米
this.transform.Translate(0, 0, 1);
//向世界坐标系 Z轴 移动1米
//this.transform.Translate(0, 0, 1, Space.World);
}
if (GUILayout.Button("Rotate"))
{
//向自身坐标系 y轴 旋转10度
//this.transform.Rotate(0, 10, 0);
//向世界坐标系 y轴 旋转10度
this.transform.Rotate(0, 10, 0, Space.World);
}
if (GUILayout.RepeatButton("RotateAround"))
{
//向世界坐标系 绕y轴旋转
transform.RotateAround(Vector3.zero, Vector3.up, 1);
}
}
}
边栏推荐
猜你喜欢

How did the beyond concert 31 years ago get super clean and repaired?

智能客服赛道:网易七鱼、微洱科技打法迥异
![[Dalian University of technology] information sharing of postgraduate entrance examination and re examination](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination

Kubernets pod exists finalizers are always in terminating state

IO flow: node flow and processing flow are summarized in detail.

Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?

MySQL学习笔记——数据类型(2)

Ali was laid off employees, looking for a job n day, headhunters came bad news

Programmers exposed that they took private jobs: they took more than 30 orders in 10 months, with a net income of 400000

Quick introduction to automatic control principle + understanding
随机推荐
Introduction of text mining tools [easy to understand]
Deep learning network regularization
Helix Swarm中文包发布,Perforce进一步提升中国用户体验
Redis 发布和订阅
Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked
lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
How to rapidly deploy application software under SaaS
开源人张亮的 17 年成长路线,热爱才能坚持
Redis 解决事务冲突之乐观锁和悲观锁
LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Luo Gu - some interesting questions 2
Five minutes of machine learning every day: how to use matrix to represent the sample data of multiple characteristic variables?
Unity update process_ Principle of unity synergy
這幾年爆火的智能物聯網(AIoT),到底前景如何?
MySQL学习笔记——数据类型(数值类型)
浮点数如何与0进行比较?
hexadecimal
web聊天室实现