当前位置:网站首页>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;
}
}
}
}
边栏推荐
- MYSQL索引优化
- 【读书会第十三期】视频文件的编码格式
- 智能客服赛道:网易七鱼、微洱科技打法迥异
- 一篇文章搞懂Go语言中的Context
- Is BigDecimal safe to calculate the amount? Look at these five pits~~
- 深度学习 网络正则化
- Temperature control system based on max31865
- Summer Review, we must avoid stepping on these holes!
- Helix swarm Chinese package is released, and perforce further improves the user experience in China
- Deep learning network regularization
猜你喜欢
numpy笔记
Introduction to modern control theory + understanding
智能客服赛道:网易七鱼、微洱科技打法迥异
MySQL学习笔记——数据类型(数值类型)
TechSmith Camtasia studio 2022.0.2 screen recording software
31年前的Beyond演唱会,是如何超清修复的?
MySQL组合索引(多列索引)使用与优化案例详解
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?
. Net applications consider x64 generation
深度学习 神经网络的优化方法
随机推荐
LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
【读书会第十三期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
numpy笔记
AI has surpassed Dr. CS in question making?
Weibo and Huya advance into interest communities: different paths for peers
深入JS中几种数据类型的解构赋值细节
暑期复习,一定要避免踩这些坑!
Guitar Pro 8win10 latest guitar learning / score / creation
odoo数据库主控密码采用什么加密算法?
Align left and right!
Unity脚本API—GameObject游戏对象、Object 对象
An article learns variables in go language
Unity脚本生命周期 Day02
The performance of major mainstream programming languages is PK, and the results are unexpected
进制乱炖
音视频技术开发周刊 | 252
科研漫画 | 联系到被试后还需要做什么?
从0到1建设智能灰度数据体系:以vivo游戏中心为例
Preliminary exploration of flask: WSGI
Luo Gu - some interesting questions