当前位置:网站首页>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;
}
}
}
}
边栏推荐
- 2022 financial products that can be invested
- numpy笔记
- Helix Swarm中文包发布,Perforce进一步提升中国用户体验
- When synchronized encounters this thing, there is a big hole, pay attention!
- Ali was laid off employees, looking for a job n day, headhunters came bad news
- How to build a technical team that will bring down the company?
- Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
- 智能客服赛道:网易七鱼、微洱科技打法迥异
- MySQL学习笔记——数据类型(数值类型)
- Hexadecimal form
猜你喜欢
Why do domestic mobile phone users choose iPhone when changing a mobile phone?
Ali was laid off employees, looking for a job n day, headhunters came bad news
.Net 应用考虑x64生成
对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
Redis 解决事务冲突之乐观锁和悲观锁
深度学习 网络正则化
When synchronized encounters this thing, there is a big hole, pay attention!
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)
近一亿美元失窃,Horizon跨链桥被攻击事件分析
随机推荐
IO flow: node flow and processing flow are summarized in detail.
web聊天室实现
AI做题水平已超过CS博士?
UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
深度学习 神经网络的优化方法
Luo Gu - some interesting questions
Preliminary exploration of flask: WSGI
Helix swarm Chinese package is released, and perforce further improves the user experience in China
Kubernets Pod 存在 Finalizers 一直处于 Terminating 状态
Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
Introduction of text mining tools [easy to understand]
Redis publier et s'abonner
Expose Ali's salary and position level
怎么判断外盘期货平台正规,资金安全?
Weibo and Huya advance into interest communities: different paths for peers
左右对齐!
暑期复习,一定要避免踩这些坑!
Graduation season - personal summary
直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
Kubernets pod exists finalizers are always in terminating state