当前位置:网站首页>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;
}
}
}
}
边栏推荐
- 进制形式
- Align left and right!
- Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked
- 深度学习 神经网络案例(手写数字识别)
- Redis publier et s'abonner
- Unity预制件Prefab Day04
- Flutter reports an error no mediaquery widget ancestor found
- On the implementation plan of MySQL explain
- 十六进制
- Logstash~Logstash配置(logstash.yml)详解
猜你喜欢
Optimization method of deep learning neural network
Luo Gu - some interesting questions 2
Preliminary exploration of flask: WSGI
Helix Swarm中文包发布,Perforce进一步提升中国用户体验
微博、虎牙挺进兴趣社区:同行不同路
Redis 發布和訂閱
中国主要城市人均存款出炉,你达标了吗?
What is the future of the booming intelligent Internet of things (aiot) in recent years?
Guitar Pro 8win10 latest guitar learning / score / creation
每周招聘|高级DBA年薪49+,机会越多,成功越近!
随机推荐
The performance of major mainstream programming languages is PK, and the results are unexpected
案例分享|金融业数据运营运维一体化建设
【学习笔记】拟阵
MySQL组合索引(多列索引)使用与优化案例详解
干货 | fMRI标准报告指南新鲜出炉啦,快来涨知识吧
重排数组
Introduction to modern control theory + understanding
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
Openresty current limiting
[learning notes] matroid
PXE网络
[differential privacy and data adaptability] differential privacy code implementation series (XIV)
Data Lake Governance: advantages, challenges and entry
LeetCode 1184. 公交站间的距离 ---vector顺逆时针
Weekly recruitment | senior DBA annual salary 49+, the more opportunities, the closer success!
Enter the width!
An article learns variables in go language
Luo Gu - some interesting questions 2
Ffmpeg Visual Studio development (IV): audio decoding
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example