【转】Unity中添加组件的几种方法
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
Adds a component class named className to the game object.
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;
}
}
Adds a component class of type componentType to the game object. C# Users can use a generic version.
no example available in C#
三、利用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
The RequireComponent attribute lets automatically add required component as a dependency.
// Mark the PlayerScript as requiring a rigidbody in the game object.
@script RequireComponent(Rigidbody)
function FixedUpdate() {
rigidbody.AddForce(Vector3.up);
}
[RequireComponent (typeof (Rigidbody))]
public class PlayerScript : MonoBehaviour {
void FixedUpdate() {
rigidbody.AddForce(Vector3.up);
}
}
【转】Unity中添加组件的几种方法的更多相关文章
- mysql 中添加索引的三种方法
原文:http://www.andyqian.com/2016/04/06/database/mysqleindex/ 在mysql中有多种索引,有普通索引,全文索引,唯一索引,多列索引,小伙伴们可以 ...
- IIS8中添加WCF支持几种方法小结[图文]
方法一 最近在做Silverlight,Windows Phone应用移植到Windows 8平台,在IIS8中测试一些传统WCF服务应用,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少 ...
- IIS配置svc(IIS8中添加WCF支持几种方法小结)
方法一 最近在做Silverlight,Windows Phone应用移植到Windows 8平台,在IIS8中测试一些传统WCF服务应用,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少 ...
- 关于MySQL中添加数据的两种方法
下面介绍两种执行SQL命令的方法,并作出相应地总结,第一种介绍一种常规用法,下面进行做简要地分析,首先我们需要执行打开数据库操作首先创建一个MySqlConnection对象,在其构造函数中传入一个连 ...
- WPF中RichTextBox中添加文字的两种方法
RichTextBox控件不同于TextBox控件,后者可以直接通过其Text属性绑定或者在后台动态添加文字. (一)使用数据绑定 <RichTextBox FontSize="12& ...
- 六、spring之通过FactoryBean为ioc容器中添加组件
前面我们已经介绍了几种为容器中添加组件的方法,今天一起学习通过FactoryBean添加组件的方法. 首先我们准备一个类,也就是我们需要注册进spring的ioc容器中的类 类Color: // 不必 ...
- ASP.Net Core中处理异常的几种方法
本文将介绍在ASP.Net Core中处理异常的几种方法 1使用开发人员异常页面(The developer exception page) 2配置HTTP错误代码页 Configuring stat ...
- vue中使用echarts的两种方法
在vue中使用echarts有两种方法一.第一种方法1.通过npm获取echarts npm install echarts --save 2.在vue项目中引入echarts 在 main.js 中 ...
- 关于iOS去除数组中重复数据的几种方法
关于iOS去除数组中重复数据的几种方法 在工作工程中我们不必要会遇到,在数组中有重复数据的时候,如何去除重复的数据呢? 第一种:利用NSDictionary的AllKeys(AllValues)方 ...
随机推荐
- HDU5853 Jong Hyok and String(二分 + 后缀数组)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5853 Description Jong Hyok loves strings. One da ...
- HD1847-(博弈论??)
Good Luck in CET-4 Everybody! Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知 ...
- ural 1073. Square Country
1073. Square Country Time limit: 1.0 secondMemory limit: 64 MB There live square people in a square ...
- webpack练手项目之easySlide(三):commonChunks(转)
Hello,大家好. 在之前两篇文章中: webpack练手项目之easySlide(一):初探webpack webpack练手项目之easySlide(二):代码分割 与大家分享了webpack的 ...
- Codeforces Round #243 (Div. 2) A. Sereja and Mugs
#include <iostream> #include <vector> #include <algorithm> #include <numeric> ...
- Flex弹性布局在移动设备上的应用
引文 首先,我们有表格布局.当不考虑语义并且利用一些适当的嵌套和其他技巧,我们可以用table建立具有一定功能的布局. 然后是现在大多数人都在使用的浮动布局.我们可以使用任何我们想用的元素,但浮动并不 ...
- 【ZOJ】3329 One Person Game
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题意:有三个色子,分别有k1.k2.k3个面,权值分别是1-k1, 1~ ...
- IOS面试题总结
iOS面试题: 一:网络理论知识的理解 1:Internet物理地址和IP地址转换采用什么协议 ARP(Address Resolution Protocol)地址解析协议 2:Internet采用哪 ...
- GO语言练习:channel select 超时机制
1.代码 2.运行 3.解析 1.代码 package main import ( "time" "fmt" ) func waitFor(ch chan in ...
- [LintCode] Submatrix Sum 子矩阵之和
Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...