当前位置:网站首页>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;
}
}
}
}
边栏推荐
- 谈SaaS下如何迅速部署应用软件
- Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
- Comment configurer un accord
- MySQL组合索引(多列索引)使用与优化案例详解
- LeetCode 1200 最小絕對差[排序] HERODING的LeetCode之路
- Five minutes per day machine learning: use gradient descent to complete the fitting of multi feature linear regression model
- Summer Review, we must avoid stepping on these holes!
- Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
- 关于FPGA底层资源的细节问题
- Why do domestic mobile phone users choose iPhone when changing a mobile phone?
猜你喜欢
深度学习 神经网络案例(手写数字识别)
近一亿美元失窃,Horizon跨链桥被攻击事件分析
AI做题水平已超过CS博士?
Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)
Helix Swarm中文包发布,Perforce进一步提升中国用户体验
一篇文章搞懂Go语言中的Context
5g TV cannot become a competitive advantage, and video resources become the last weapon of China's Radio and television
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
随机推荐
Kubernets pod exists finalizers are always in terminating state
Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked
如何配和弦
Usage of database functions "recommended collection"
PLC Analog input analog conversion FC s_ ITR (CoDeSys platform)
Leecode learning notes - Joseph problem
Techsmith Camtasia Studio 2022.0.2屏幕录制软件
Luo Gu - some interesting questions 2
宽度精度
Introduction of text mining tools [easy to understand]
Details of FPGA underlying resources
web聊天室实现
Flutter reports an error no mediaquery widget ancestor found
Exploration and practice of eventbridge in the field of SaaS enterprise integration
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
夜天之书 #53 Apache 开源社群的“石头汤”
openresty 限流
一篇文章搞懂Go语言中的Context
Implementation of web chat room
Partial modification - progressive development