当前位置:网站首页>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;
}
}
}
}边栏推荐
- Huawei cloud database DDS products are deeply enabled
- The performance of major mainstream programming languages is PK, and the results are unexpected
- LeetCode 58. 最后一个单词的长度
- 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?
- 小数,指数
- 【读书会第十三期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
- Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
- %f格式符
- Decimal, exponential
- Solve the error of JSON module in PHP compilation and installation under CentOS 6.3
猜你喜欢

How to build a technical team that will bring down the company?

Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作

MySQL学习笔记——数据类型(数值类型)

Redis 發布和訂閱

Guitar Pro 8win10 latest guitar learning / score / creation
Detailed explanation of MySQL composite index (multi column index) use and optimization cases

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

MP3是如何诞生的?

Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked

AI做题水平已超过CS博士?
随机推荐
Guitar Pro 8win10 latest guitar learning / score / creation
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
Redis shares four cache modes
Redis publier et s'abonner
MySQL学习笔记——数据类型(2)
LeetCode 58. 最后一个单词的长度
. Net applications consider x64 generation
Redis publish and subscribe
Redis哨兵模式实现一主二从三哨兵
数据湖治理:优势、挑战和入门
They are all talking about Devops. Do you really understand it?
[learning notes] matroid
Openresty current limiting
Enter the width!
Deep learning neural network case (handwritten digit recognition)
大神详解开源 BUFF 增益攻略丨直播
What is the future of the booming intelligent Internet of things (aiot) in recent years?
智能客服赛道:网易七鱼、微洱科技打法迥异
Understand the context in go language in an article
Unity脚本API—Time类