【unity3d study ---- 麦子学院】---------- unity3d常用组件及分析 ---------- 组件的使用

unity中的组件可以可视化添加,删除

unity 中组件也可以通过代码添加

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class ComponentUse : MonoBehaviour {
 5 
 6     // Use this for initialization
 7     void Start () {
 8      
 9         // add component 添加
10         gameObject.AddComponent<ParticleSystem> (); // 效率是最高的  最好的
11         // gameObject.AddComponent (typeof(ParticleSystem)); // c# 的反射机制  不推荐
12         // gameObject.AddComponent ("ParticleSystem");  // 字符串 类名 
13 
14         // query component 查询
15         ParticleSystem ps = gameObject.GetComponent<ParticleSystem> (); // 获取 自己的这个组件
16         ParticleSystem[] pss = gameObject.GetComponents<ParticleSystem>(); // 获取 自己的所有这个组件
17         ParticleSystem ps_child = gameObject.GetComponentInChildren<ParticleSystem> (); // 获取 第一个找到的孩子身上的这个组件 并不是规律的
18         ParticleSystem[] pss_child = gameObject.GetComponentsInChildren<ParticleSystem> (); // 获取所有孩子身上的这个组件
19         ParticleSystem[] pss_parent = gameObject.GetComponentsInParent<ParticleSystem> (); // 获取所有父节点上的这个组件
20 
21         // delete component 删除
22         if (ps)
23             Destroy (ps); // 其实 unity 里面有判断是否为空,但是我感觉还是最好在外面判断一下
24 
25 
26     }
27     
28     // Update is called once per frame
29     void Update () {
30     
31     }
32 }
原文地址:https://www.cnblogs.com/dudu580231/p/5952972.html