当前位置:网站首页>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);
}
}
}
边栏推荐
- 音视频技术开发周刊 | 252
- go-zero微服务实战系列(九、极致优化秒杀性能)
- 都在说DevOps,你真正了解它吗?
- Dialogue with ye Yanxiu, senior consultant of Longzhi and atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- web聊天室实现
- [local differential privacy and random response code implementation] differential privacy code implementation series (13)
- 5g TV cannot become a competitive advantage, and video resources become the last weapon of China's Radio and television
- 宽度与对齐
- 力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
猜你喜欢
中国主要城市人均存款出炉,你达标了吗?
华为云数据库DDS产品深度赋能
Deep learning neural network case (handwritten digit recognition)
Quick introduction to automatic control principle + understanding
They are all talking about Devops. Do you really understand it?
MySQL组合索引(多列索引)使用与优化案例详解
Preliminary exploration of flask: WSGI
How to match chords
智能客服赛道:网易七鱼、微洱科技打法迥异
内存管理总结
随机推荐
LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked
.Net之延迟队列
%S format character
Deep learning 7 transformer series instance segmentation mask2former
C1 certification learning notes 3 -- Web Foundation
十六进制
Decimal, exponential
从0到1建设智能灰度数据体系:以vivo游戏中心为例
UFO: Microsoft scholars have proposed a unified transformer for visual language representation learning to achieve SOTA performance on multiple multimodal tasks
局部修改-渐进型开发
音视频技术开发周刊 | 252
PLC Analog input analog conversion FC s_ ITR (CoDeSys platform)
Techsmith Camtasia Studio 2022.0.2屏幕录制软件
Leecode learning notes - Joseph problem
都在说DevOps,你真正了解它吗?
Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)
Redis publish and subscribe
Kubernets Pod 存在 Finalizers 一直处于 Terminating 状态
Weekly recruitment | senior DBA annual salary 49+, the more opportunities, the closer success!