博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转】Unity中添加组件的几种方法
阅读量:6394 次
发布时间:2019-06-23

本文共 2568 字,大约阅读时间需要 8 分钟。

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的官方说明:

.AddComponent

AddComponent(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  this will automatically add a  as well.
using UnityEngine;using System.Collections;public class Example :  {    public  sc;    void Example() {        gameObject.AddComponent("FoobarScript");        sc = gameObject.AddComponent("") as ;    }}
AddComponent(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 ()

 

function FixedUpdate() {	rigidbody.AddForce();}
C# Example:
[ (typeof ())]public class PlayerScript :  {	void FixedUpdate()  {		rigidbody.AddForce();	}}

 

转载于:https://www.cnblogs.com/mimime/p/6240243.html

你可能感兴趣的文章
ASP.NET 2.0 验证控件新的功能
查看>>
CentOS下Zabbix安装部署及汉化
查看>>
判断两矩形是否相交
查看>>
第7章 "敏捷+"项目管理
查看>>
Sublime-text gitHub 问题收集
查看>>
UML 类图
查看>>
Unity Remote 无法连接
查看>>
linux下core file size设置笔记
查看>>
mysql 、redis的区别
查看>>
使用JPA中@Query 注解实现update 操作
查看>>
7.4. APC Cache (php-apc - APC (Alternative PHP Cache) module for PHP 5)
查看>>
Web 仪表盘
查看>>
我的Fedora 7硬盘安装过程
查看>>
Python——SSHClient.py
查看>>
MVC解决更新冲突问题
查看>>
江西理工大学南昌校区cool code竞赛
查看>>
[LeetCode] Trim a Binary Search Tree 修剪一棵二叉搜索树
查看>>
Ubuntu SDL lib 安装
查看>>
Java 并发编程内部分享PPT分享
查看>>
关于discuz中禾金投票系统循环出现引导页的问题
查看>>