当前位置:网站首页>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;
}
}
}
}边栏推荐
- Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
- Redis publish and subscribe
- Openresty current limiting
- 信号处理之一阶RC低通滤波器宏指令实现(繁易触摸屏)
- Kubernets pod exists finalizers are always in terminating state
- 每周招聘|高级DBA年薪49+,机会越多,成功越近!
- go-zero微服务实战系列(九、极致优化秒杀性能)
- 数据库函数的用法「建议收藏」
- Exploration and practice of eventbridge in the field of SaaS enterprise integration
- 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?
猜你喜欢

Halcon knowledge: NCC_ Model template matching

夜天之书 #53 Apache 开源社群的“石头汤”
![Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding](/img/4a/6763e3fbdeaf9de673fbe8eaf96858.png)
Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding

MP3是如何诞生的?

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

Redis publier et s'abonner

AI做题水平已超过CS博士?

Optimization method of deep learning neural network

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

Preliminary exploration of flask: WSGI
随机推荐
【学习笔记】拟阵
夜天之书 #53 Apache 开源社群的“石头汤”
03 storage system
LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
Optimization method of deep learning neural network
智能客服赛道:网易七鱼、微洱科技打法迥异
MySQL learning notes - data type (numeric type)
UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
Flutter reports an error no mediaquery widget ancestor found
CentOS 6.3 下 PHP编译安装JSON模块报错解决
Comment configurer un accord
Huawei cloud database DDS products are deeply enabled
Introduction to modern control theory + understanding
Is BigDecimal safe to calculate the amount? Look at these five pits~~
How did the beyond concert 31 years ago get super clean and repaired?
[differential privacy and data adaptability] differential privacy code implementation series (XIV)
小数,指数
numpy笔记
.Net之延迟队列
Redis 解决事务冲突之乐观锁和悲观锁