当前位置:网站首页>Unity脚本API—Component组件
Unity脚本API—Component组件
2022-07-04 14:13:00 【@夜魅】
常用方法:
GetComponent:获取当前物体其他组件类型的引用。
GetComponents:获取当前物体所有组件引用。
GetComponentsInChildren:查找指定类型组件(从自身开始,并搜索所有后代)
GetComponentInChildren:查找指定类型组件(从自身开始,并搜索所有后代,查找到第一个满足条件则结束)
GetComponentsInParent:查找指定类型组件(从自身开始,并搜索所有先辈)
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Component 类 提供了查找组件的功能(从自身、从后代、从先辈)组件的功能
/// </summary>
public class ComponentDemo : MonoBehaviour
{
private void OnGUI()
{
if (GUILayout.Button("transform"))
{
this.transform.position = new Vector3(0,0,10);
}
if (GUILayout.Button("GetComponent"))
{
this.GetComponent<MeshRenderer>().material.color = Color.red;
}
if (GUILayout.Button("GetComponents"))
{
//获取当前组件
var allComponent = this.GetComponents<Component> ();
foreach (var component in allComponent)
{
Debug.Log(component.GetType());
}
}
if (GUILayout.Button("GetComponentsInChildren"))
{
//获取后代物体的指定类型组件(从自身开始)
var allComponent = this.GetComponentsInChildren<MeshRenderer>();
foreach (var component in allComponent)
{
component.material.color = Color.red;
}
}
if (GUILayout.Button("GetComponentsInParent"))
{
//获取先辈物体的指定类型组件(从自身开始)
var allComponent = this.GetComponentsInParent<MeshRenderer>();
foreach (var component in allComponent)
{
component.material.color = Color.red;
}
}
}
}边栏推荐
- 宽度与对齐
- MP3是如何诞生的?
- Helix Swarm中文包发布,Perforce进一步提升中国用户体验
- IO flow: node flow and processing flow are summarized in detail.
- 宽度精度
- 直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
- Redis的4种缓存模式分享
- Hexadecimal form
- Intelligent customer service track: Netease Qiyu and Weier technology play different ways
- [local differential privacy and random response code implementation] differential privacy code implementation series (13)
猜你喜欢

Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist

Redis 發布和訂閱

Kubernets pod exists finalizers are always in terminating state

Redis 解决事务冲突之乐观锁和悲观锁

.Net 应用考虑x64生成

中国主要城市人均存款出炉,你达标了吗?

Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture

They are all talking about Devops. Do you really understand it?

UFO: Microsoft scholars have proposed a unified transformer for visual language representation learning to achieve SOTA performance on multiple multimodal tasks

MySQL学习笔记——数据类型(数值类型)
随机推荐
Kubernets Pod 存在 Finalizers 一直处于 Terminating 状态
mysql 联合主键_Mysql 创建联合主键[通俗易懂]
Redis哨兵模式实现一主二从三哨兵
31年前的Beyond演唱会,是如何超清修复的?
直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
go-zero微服务实战系列(九、极致优化秒杀性能)
Redis sentinel mode realizes one master, two slave and three Sentinels
Decimal, exponential
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
MP3是如何诞生的?
openresty 重定向
对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
一篇文章搞懂Go语言中的Context
Huawei cloud database DDS products are deeply enabled
Redis 发布和订阅
Enter the width!
Graduation season - personal summary
How to handle exceptions in multithreading?
Is BigDecimal safe to calculate the amount? Look at these five pits~~
Introduction to modern control theory + understanding