http://blog.csdn.net/monzart7an/article/details/23199647

一、在编辑器上面添加一个组件。这个不用多说。

二、在脚本中利用AddComponent函数添加一个组件,例如:

using UnityEngine;
using System.Collections;

public class CharacterSpawner : MonoBehaviour {

public void Spawn(GameObject CharacterSlected){
        if(CharacterSlected){
            GameObject player = (GameObject)GameObject.Instantiate(CharacterSlected,this.transform.position,Quaternion.identity);
            if(!player.GetComponent<PlayerManager>()){
                player.AddComponent<PlayerManager>();
            }
        }
    }
}

AddComponent的官方说明:

GameObject.AddComponent

ComponentAddComponent(string className);
Description

Adds a component class named className to the game object.

Use this function to change behaviour of objects on the fly. You can also add script to game objects by passing in the name of the script class.

Some components require other components to exist in the same game object as well. This function automatically adds any required components as well eg. if you add a HingeJoint this will automatically add a Rigidbody as well.

using UnityEngine;
using System.Collections; public class Example : MonoBehaviour {
public SphereCollider sc;
void Example() {
gameObject.AddComponent("FoobarScript");
sc = gameObject.AddComponent("SphereCollider") as SphereCollider;
}
}
ComponentAddComponent(Type componentType);
Description

Adds a component class of type componentType to the game object. C# Users can use a generic version.

no example available in C#
Note that there is no RemoveComponent(), to remove a component, use Object.Destroy.

三、利用RequireComponent添加一个组件。

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PlayerCharacterController))]
[RequireComponent(typeof(PlayerCharacterUI))]
[RequireComponent(typeof(PlayerQuestManager))]
[RequireComponent(typeof(PlayerSave))]

public class PlayerManager : MonoBehaviour {

}

RequireComponent意思是表面这个类一定需要哪些组件,如果目前这些组件没有被加上,就自动加上。

官方解释:

RequireComponentNamespace: UnityEngine

 
Description

The RequireComponent attribute lets automatically add required component as a dependency.

When you add a script which uses RequireComponent, the required component will automatically be added to the game object. This is useful to avoid setup errors. For example a script might require that a rigid body is always added to the same game object. Using RequireComponent this will be done automatically, thus you can never get the setup wrong.
// Mark the PlayerScript as requiring a rigidbody in the game object.
@script RequireComponent(Rigidbody)
function FixedUpdate() {
rigidbody.AddForce(Vector3.up);
}
C# Example:
[RequireComponent (typeof (Rigidbody))]
public class PlayerScript : MonoBehaviour {
void FixedUpdate() {
rigidbody.AddForce(Vector3.up);
}
}

 

【转】Unity中添加组件的几种方法的更多相关文章

  1. mysql 中添加索引的三种方法

    原文:http://www.andyqian.com/2016/04/06/database/mysqleindex/ 在mysql中有多种索引,有普通索引,全文索引,唯一索引,多列索引,小伙伴们可以 ...

  2. IIS8中添加WCF支持几种方法小结[图文]

    方法一 最近在做Silverlight,Windows Phone应用移植到Windows 8平台,在IIS8中测试一些传统WCF服务应用,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少 ...

  3. IIS配置svc(IIS8中添加WCF支持几种方法小结)

    方法一 最近在做Silverlight,Windows Phone应用移植到Windows 8平台,在IIS8中测试一些传统WCF服务应用,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少 ...

  4. 关于MySQL中添加数据的两种方法

    下面介绍两种执行SQL命令的方法,并作出相应地总结,第一种介绍一种常规用法,下面进行做简要地分析,首先我们需要执行打开数据库操作首先创建一个MySqlConnection对象,在其构造函数中传入一个连 ...

  5. WPF中RichTextBox中添加文字的两种方法

    RichTextBox控件不同于TextBox控件,后者可以直接通过其Text属性绑定或者在后台动态添加文字. (一)使用数据绑定 <RichTextBox FontSize="12& ...

  6. 六、spring之通过FactoryBean为ioc容器中添加组件

    前面我们已经介绍了几种为容器中添加组件的方法,今天一起学习通过FactoryBean添加组件的方法. 首先我们准备一个类,也就是我们需要注册进spring的ioc容器中的类 类Color: // 不必 ...

  7. ASP.Net Core中处理异常的几种方法

    本文将介绍在ASP.Net Core中处理异常的几种方法 1使用开发人员异常页面(The developer exception page) 2配置HTTP错误代码页 Configuring stat ...

  8. vue中使用echarts的两种方法

    在vue中使用echarts有两种方法一.第一种方法1.通过npm获取echarts npm install echarts --save 2.在vue项目中引入echarts 在 main.js 中 ...

  9. 关于iOS去除数组中重复数据的几种方法

    关于iOS去除数组中重复数据的几种方法   在工作工程中我们不必要会遇到,在数组中有重复数据的时候,如何去除重复的数据呢? 第一种:利用NSDictionary的AllKeys(AllValues)方 ...

随机推荐

  1. ashx 文件 与js文件数据交互

    //js代码 //城市下拉列表             $("#selPro").change(function() {                 var option = ...

  2. 由addOneMember引发的思考

    addOneMember是一个方法,这个方法在两处地方重复了. 所以在修改页面的时候,发现修改了一处,如果是新手,肯定不会注意到另外一处有问题,他如果没有看清楚这个类到底整体怎样,那么他会犯的错误是就 ...

  3. 如何在2016年成为一个更好的Node.js开发者

    本文主要讨论一些进行Node.js开发的最佳实践和建议,这些建议不仅仅适合开发者,还适合那些管理与维护Node.js基础架构的工作人员.遵循本文提供的这些建议,能够让你更好的进行日常的开发工作. St ...

  4. 移动前端头部标签(HTML5 head meta)

    移动前端开发中添加一些webkit专属的HTML5头部标签,帮助浏览器更好解析html代码,更好地将移动web前端页面表现出来.本文整理一些常用的meta标签. <!DOCTYPE html&g ...

  5. 关于vim插件

    本人比较喜欢amix它集成了很多插件. 1.mru.vim:用于打开最近使用过的文件 使用命令: :MRU     打开最近的文件列表 上下箭头可以移动关标 :o 在新窗口中打开文件 2.NERD T ...

  6. 【BZOJ】2216: [Poi2011]Lightning Conductor

    题意 给一个长度为\(n\)的序列\(a_i\),对于每个\(1 \le i \le n\),找到最小的非负整数\(p\)满足 对于任意的\(j\), \(a_j \le a_i + p - \sqr ...

  7. 淘宝UED上关于chrome的transition闪烁问题的解决方案

    前段时间,有同事和会员反馈使用Chrome访问淘宝首页会出现画面闪动的现象,但是我在Mac和Win下面的Chrome都无法重现这个问题,后来重装了一遍Win7下的Chrome Beta版本,终于重现了 ...

  8. Java_关于App class loader的总结

    Java本身是一种设计的非常简单,非常精巧的语言,所以Java背后的原理也很简单,归结起来就是两点: 1.JVM的内存管理 理解了这一点,所有和对象相关的问题统统都能解决 2.JVM Class Lo ...

  9. Infragistics公司的UltraWebGrid控件在显示的时候报“theForm” 未定义错误的解决。

    在项目中使用了Infragistics公司的UltraWebGrid控件,浏览器中报错,“theForm” 未定义,并且造成客户端js,滚动条,失效.最后查官网论坛找到问题.需要把web.config ...

  10. 如何使用命令行编译以及运行java文件

    要想编译和运行java文件,很简单,只需要两个命令: (1) javac:作用:编译java文件:使用方法: javac Hello.java ,如果不出错的话,在与Hello.java 同一目录下会 ...