当前位置:网站首页>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;
}
}
}
}
边栏推荐
猜你喜欢
MySQL组合索引(多列索引)使用与优化案例详解
深度学习 神经网络的优化方法
Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
Kubernets pod exists finalizers are always in terminating state
How to handle exceptions in multithreading?
Weekly recruitment | senior DBA annual salary 49+, the more opportunities, the closer success!
Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding
Five minutes of machine learning every day: how to use matrix to represent the sample data of multiple characteristic variables?
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作
随机推荐
PLC Analog input analog conversion FC s_ ITR (CoDeSys platform)
Exploration and practice of eventbridge in the field of SaaS enterprise integration
Optimization method of deep learning neural network
SAIC Maxus officially released its new brand "mifa", and its flagship product mifa 9 was officially unveiled!
Expose Ali's salary and position level
selenium 浏览器(2)
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?
Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
Deep learning network regularization
大神详解开源 BUFF 增益攻略丨直播
openresty 重定向
中国主要城市人均存款出炉,你达标了吗?
IO flow: node flow and processing flow are summarized in detail.
On the implementation plan of MySQL explain
【学习笔记】拟阵
03 storage system
函数计算异步任务能力介绍 - 任务触发去重
十六进制
Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding
怎么判断外盘期货平台正规,资金安全?