当前位置:网站首页>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);
}
}
}
边栏推荐
- Numpy notes
- Preliminary exploration of flask: WSGI
- Halo effect - who says that those with light on their heads are heroes
- Exploration and practice of eventbridge in the field of SaaS enterprise integration
- .Net 应用考虑x64生成
- Width accuracy
- 直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
- I plan to teach myself some programming and want to work as a part-time programmer. I want to ask which programmer has a simple part-time platform list and doesn't investigate the degree of the receiv
- 产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
- 基于MAX31865的温度控制系统
猜你喜欢
Five minutes per day machine learning: use gradient descent to complete the fitting of multi feature linear regression model
近一亿美元失窃,Horizon跨链桥被攻击事件分析
Helix Swarm中文包发布,Perforce进一步提升中国用户体验
这几年爆火的智能物联网(AIoT),到底前景如何?
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
Memory management summary
MP3是如何诞生的?
Techsmith Camtasia Studio 2022.0.2屏幕录制软件
深度学习 神经网络案例(手写数字识别)
03-存储系统
随机推荐
03 storage system
基于MAX31865的温度控制系统
音视频技术开发周刊 | 252
数据库函数的用法「建议收藏」
MySQL组合索引(多列索引)使用与优化案例详解
微博、虎牙挺进兴趣社区:同行不同路
這幾年爆火的智能物聯網(AIoT),到底前景如何?
selenium 浏览器(2)
Ffprobe common commands
Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked
智能客服赛道:网易七鱼、微洱科技打法迥异
对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
Redis sentinel mode realizes one master, two slave and three Sentinels
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
信号处理之一阶RC低通滤波器宏指令实现(繁易触摸屏)
Details of FPGA underlying resources
Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)
深度学习 网络正则化
js平铺数据查找叶子节点
进制形式