当前位置:网站首页>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;
}
}
}
}边栏推荐
- 这几年爆火的智能物联网(AIoT),到底前景如何?
- LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
- When synchronized encounters this thing, there is a big hole, pay attention!
- LeetCode 58. 最后一个单词的长度
- 31年前的Beyond演唱会,是如何超清修复的?
- UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
- Preliminary exploration of flask: WSGI
- 力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
- Details of FPGA underlying resources
- 浮点数如何与0进行比较?
猜你喜欢
一篇文章搞懂Go语言中的Context

Five minutes per day machine learning: use gradient descent to complete the fitting of multi feature linear regression model

信号处理之一阶RC低通滤波器宏指令实现(繁易触摸屏)

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

Weekly recruitment | senior DBA annual salary 49+, the more opportunities, the closer success!

Five minutes of machine learning every day: how to use matrix to represent the sample data of multiple characteristic variables?
Redis的4种缓存模式分享

大神详解开源 BUFF 增益攻略丨直播

Techsmith Camtasia Studio 2022.0.2屏幕录制软件

【大连理工大学】考研初试复试资料分享
随机推荐
MySQL学习笔记——数据类型(数值类型)
%f格式符
selenium 元素交互
LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
Exploration and practice of eventbridge in the field of SaaS enterprise integration
Techsmith Camtasia Studio 2022.0.2屏幕录制软件
openresty 重定向
智能客服赛道:网易七鱼、微洱科技打法迥异
03-存储系统
Width accuracy
hexadecimal
Redis 发布和订阅
文本挖掘工具的介绍[通俗易懂]
31年前的Beyond演唱会,是如何超清修复的?
Unity update process_ Principle of unity synergy
numpy笔记
每周招聘|高级DBA年薪49+,机会越多,成功越近!
小数,指数
Deep learning network regularization
怎么判断外盘期货平台正规,资金安全?