当前位置:网站首页>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;
}
}
}
}
边栏推荐
- Ffprobe common commands
- Five minutes per day machine learning: use gradient descent to complete the fitting of multi feature linear regression model
- 小数,指数
- Deep learning 7 transformer series instance segmentation mask2former
- 产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
- web聊天室实现
- 数据库函数的用法「建议收藏」
- leecode学习笔记-约瑟夫问题
- MySQL学习笔记——数据类型(数值类型)
- Weibo and Huya advance into interest communities: different paths for peers
猜你喜欢
Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding
深度学习 神经网络的优化方法
Luo Gu - some interesting questions 2
Numpy notes
Redis的4种缓存模式分享
信号处理之一阶RC低通滤波器宏指令实现(繁易触摸屏)
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
智能客服赛道:网易七鱼、微洱科技打法迥异
MP3是如何诞生的?
I plan to teach myself some programming and want to work as a part-time programmer. I want to ask which programmer has a simple part-time platform list and doesn't investigate the degree of the receiv
随机推荐
.Net 应用考虑x64生成
谈SaaS下如何迅速部署应用软件
MySQL组合索引(多列索引)使用与优化案例详解
Optimization method of deep learning neural network
這幾年爆火的智能物聯網(AIoT),到底前景如何?
MySQL learning notes - data type (numeric type)
selenium 浏览器(2)
PXE网络
Unity update process_ Principle of unity synergy
%f格式符
Luo Gu - some interesting questions
华为云数据库DDS产品深度赋能
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
暑期复习,一定要避免踩这些坑!
Width and alignment
PLC Analog input analog conversion FC s_ ITR (CoDeSys platform)
函数计算异步任务能力介绍 - 任务触发去重
直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
How to handle exceptions in multithreading?
Numpy notes