当前位置:网站首页>Unity script API - component component
Unity script API - component component
2022-07-04 15:22:00 【@Night Charm】
Common methods :
GetComponent: Get references of other component types of the current object .
GetComponents: Get all component references of the current object .
GetComponentsInChildren: Find components of the specified type ( Start with yourself , And search all descendants )
GetComponentInChildren: Find components of the specified type ( Start with yourself , And search all descendants , If you find the first one that meets the condition, it will end )
GetComponentsInParent: Find components of the specified type ( Start with yourself , And search all ancestors )
The code is as follows :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Component class It provides the function of finding components ( From oneself 、 From offspring 、 From the ancestors ) Functions of components
/// </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"))
{
// Get the current component
var allComponent = this.GetComponents<Component> ();
foreach (var component in allComponent)
{
Debug.Log(component.GetType());
}
}
if (GUILayout.Button("GetComponentsInChildren"))
{
// Gets the specified type component of the descendant object ( Start with yourself )
var allComponent = this.GetComponentsInChildren<MeshRenderer>();
foreach (var component in allComponent)
{
component.material.color = Color.red;
}
}
if (GUILayout.Button("GetComponentsInParent"))
{
// Gets the specified type component of the predecessor object ( Start with yourself )
var allComponent = this.GetComponentsInParent<MeshRenderer>();
foreach (var component in allComponent)
{
component.material.color = Color.red;
}
}
}
}
边栏推荐
- 文本挖掘工具的介绍[通俗易懂]
- 进制乱炖
- Redis的4种缓存模式分享
- 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?
- selenium 浏览器(2)
- MySQL federated primary key_ MySQL creates a federated primary key [easy to understand]
- .Net 应用考虑x64生成
- JS tile data lookup leaf node
- LeetCode 58. Length of the last word
- How did the beyond concert 31 years ago get super clean and repaired?
猜你喜欢
Helix swarm Chinese package is released, and perforce further improves the user experience in China
What is the future of the booming intelligent Internet of things (aiot) in recent years?
Guitar Pro 8win10 latest guitar learning / score / creation
MySQL学习笔记——数据类型(数值类型)
Unity动画Animation Day05
flutter 报错 No MediaQuery widget ancestor found.
Deep learning neural network case (handwritten digit recognition)
每周招聘|高级DBA年薪49+,机会越多,成功越近!
【大连理工大学】考研初试复试资料分享
lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
随机推荐
小数,指数
中国主要城市人均存款出炉,你达标了吗?
每周招聘|高级DBA年薪49+,机会越多,成功越近!
MySQL组合索引(多列索引)使用与优化案例详解
They are all talking about Devops. Do you really understand it?
华为云数据库DDS产品深度赋能
How to handle exceptions in multithreading?
Intelligent customer service track: Netease Qiyu and Weier technology play different ways
从0到1建设智能灰度数据体系:以vivo游戏中心为例
Redis publier et s'abonner
Shell 编程基础
Logstash~Logstash配置(logstash.yml)详解
Kubernets pod exists finalizers are always in terminating state
宽度与对齐
Unity脚本生命周期 Day02
【读书会第十三期】 音频文件的封装格式和编码格式
Width accuracy
Ffprobe common commands
暑期复习,一定要避免踩这些坑!
On the implementation plan of MySQL explain