Unity中添加组件的几种方法
来源:互联网 发布:加拿大订酒店软件 编辑:程序博客网 时间:2024/04/24 18:51
一、在编辑器上面添加一个组件。这个不用多说。
二、在脚本中利用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.
<em>using UnityEngine;using System.Collections;public class Example : <a target=_blank target="_blank" href="http://blog.csdn.net/monzart7an/article/details/MonoBehaviour.html" style="color: rgb(20, 93, 123); text-decoration: none;">MonoBehaviour</a> { public <a target=_blank target="_blank" href="http://blog.csdn.net/monzart7an/article/details/SphereCollider.html" style="color: rgb(20, 93, 123); text-decoration: none;">SphereCollider</a> sc; void Example() { gameObject.AddComponent("FoobarScript"); sc = gameObject.AddComponent("<a target=_blank target="_blank" href="http://blog.csdn.net/monzart7an/article/details/SphereCollider.html" style="color: rgb(20, 93, 123); text-decoration: none;">SphereCollider</a>") as <a target=_blank target="_blank" href="http://blog.csdn.net/monzart7an/article/details/SphereCollider.html" style="color: rgb(20, 93, 123); text-decoration: none;">SphereCollider</a>; }}</em>
Adds a component class of type componentType
to the game object. C# Users can use a generic version.
<em>no example available in C#</em>
三、利用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.
<em>// Mark the PlayerScript as requiring a rigidbody in the game object.@script <a target=_blank target="_blank" href="http://blog.csdn.net/monzart7an/article/details/RequireComponent.html" style="color: rgb(20, 93, 123); text-decoration: none;">RequireComponent</a>(<a target=_blank target="_blank" href="http://blog.csdn.net/monzart7an/article/details/Rigidbody.html" style="color: rgb(20, 93, 123); text-decoration: none;">Rigidbody</a>)</em><p class="basic" style="margin-top: 0px; margin-bottom: 1.5em; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Arial, sans-serif; color: rgb(68, 68, 68);"></p><em>function FixedUpdate() {rigidbody.AddForce(<a target=_blank target="_blank" href="http://blog.csdn.net/monzart7an/article/details/Vector3-up.html" style="color: rgb(20, 93, 123); text-decoration: none;">Vector3.up</a>);}</em>
<em>[<a target=_blank target="_blank" href="http://blog.csdn.net/monzart7an/article/details/RequireComponent.html" style="color: rgb(20, 93, 123); text-decoration: none;">RequireComponent</a> (typeof (<a target=_blank target="_blank" href="http://blog.csdn.net/monzart7an/article/details/Rigidbody.html" style="color: rgb(20, 93, 123); text-decoration: none;">Rigidbody</a>))]public class PlayerScript : <a target=_blank target="_blank" href="http://blog.csdn.net/monzart7an/article/details/MonoBehaviour.html" style="color: rgb(20, 93, 123); text-decoration: none;">MonoBehaviour</a> {void FixedUpdate() {rigidbody.AddForce(<a target=_blank target="_blank" href="http://blog.csdn.net/monzart7an/article/details/Vector3-up.html" style="color: rgb(20, 93, 123); text-decoration: none;">Vector3.up</a>);}}</em>
- Unity中添加组件的几种方法
- Unity中添加组件的几种方法
- Unity中添加组件的几种方法
- Unity中添加组件的几种方法
- Unity中添加组件的几种方法
- android 给组件添加事件的几种方法
- [Unity]Unity组件崩溃的通用的几种解决办法
- Unity中移动物体的几种常用方法
- 组件的几种方法VC中使用COM
- [Unity-25] Unity中让游戏对象消失或隐藏的几种方法
- ORACLE10G中添加数据库客户端的几种方法
- windows中添加菜单的几种方法
- Qt中为按钮添加图片的几种方法
- python中List添加元素的几种方法
- Android Studio中添加按钮事件的几种方法
- python中List添加,删除元素的几种方法
- python中List添加、删除元素的几种方法
- python中List添加、删除元素的几种方法
- CF#267 (Div. 2) A
- cocos2dx 系列教程
- N个实数中偶数的平均值
- 依然大爱的《棋魂》
- QT中各种MessageBox的使用
- Unity中添加组件的几种方法
- 2.3 IP网络性能测量
- IOS开发调用图片库或相机时界面设定为中文
- linux/unix僵尸进程(转自http://www.cnblogs.com/newpanderking/p/3784328.html)
- 公钥证书cer,pfx
- android 关于ExpandableListView单击一个group其他的group关闭,此group标题应该顶头
- TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR<三>
- hdu 5023(线段树区间更新+区间查询)
- 内存池技术介绍