Unity中添加组件的几种方法

Unity 一文掌握使用AddListener方法为组件事件添加监听器的方法 Unity 使用AddListener方法为组件事件添加监听器的方法 阅读详情

转自:http://blog.youkuaiyun.com/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

Component 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 HingeJoint this will automatically add a Rigidbody as well.
<em>using UnityEngine;
using System.Collections;

public class Example : <a target=_blank target="_blank" href="http://blog.youkuaiyun.com/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.youkuaiyun.com/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.youkuaiyun.com/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.youkuaiyun.com/monzart7an/article/details/SphereCollider.html" style="color: rgb(20, 93, 123); text-decoration: none;">SphereCollider</a>;
    }
}
</em>
Component AddComponent(Type componentType);
Description

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>
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.
<em>// Mark the PlayerScript as requiring a rigidbody in the game object.
@script <a target=_blank target="_blank" href="http://blog.youkuaiyun.com/monzart7an/article/details/RequireComponent.html" style="color: rgb(20, 93, 123); text-decoration: none;">RequireComponent</a>(<a target=_blank target="_blank" href="http://blog.youkuaiyun.com/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.youkuaiyun.com/monzart7an/article/details/Vector3-up.html" style="color: rgb(20, 93, 123); text-decoration: none;">Vector3.up</a>);
}
</em>
C# Example:
<em>[<a target=_blank target="_blank" href="http://blog.youkuaiyun.com/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.youkuaiyun.com/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.youkuaiyun.com/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.youkuaiyun.com/monzart7an/article/details/Vector3-up.html" style="color: rgb(20, 93, 123); text-decoration: none;">Vector3.up</a>);
	}
}</em>
unity获取物件组件常用方法总结 目录 一.利用脚本获取物体的方法 二.利用脚本获取组件的方法 三.利用公开变量引用物体跟组件 一.利用脚本获取物体的方法 这里介绍两种方法 1.通过名称获取脚本 可以通过物体的名称直接获得物体,使用GameObject.Find()方法即可。 列如;我们需要在当前脚本,需要获得另外一个名为“object”的物体,具体内容如下 此时Ob便获得Object这个物体,以及这个物体身上的所有组件。 这个方法有两个弊端。 1.GameObject.Find(),无法找到未激活的物体. 阅读详情

相关推荐

【例0001】add component 在装配中添加组件

add component 在装配中添加组件》这是一个NX二次开发官方小例子,下面是代码和解析。相较于混乱、未经验证的代码,官方案例能够确保开发者获得准确的开发方法,这些官方示例代码经过严格测试,能够正确地反映出NX软件的功能和API使用方式,有助于开发者系统地掌握NX二次开发技能,提高开发质量和效率。本专栏订阅后是永久阅读的。欢迎一起学习NX二次开发案例,逐步积累宝贵的经验,早日成为行业专家。

王牌飞行员_里海的博客 534

Unity简单操作:Unity中添加组件的几种方法

一、在编辑器上面添加一个组件。这个不用多说。 二、在脚本中利用AddComponent函数添加一个组件,例如: usingUnityEngine; usingSystem.Collections; publicclassCharacterSpawner:MonoBehaviour{ publicvoidSpawn(GameObjectCharacterSlected){ if(CharacterSlected){ GameOb...

weixin_39114763的博客 1万+

UG NX二次开发(C#)-装配-添加组件AddComponent

在UG NX的使用过程中,装配是很重要的一环,本文是为了实现组件的批量装配,采用UG NX二次开发,采用的主要函数为AddComponent。 AddComponent()的帮助说明如下: 其是属于NXOpen.Assemblies空间的ComponentAssembly类, 采用: using NXOpen.Assemblies; 其实现代码如下: /// <summary> /// 添加组件 /// </summary>

GimiGimmy 3061

关于Unity在运行时使用AddComponent添加脚本后,新脚本的生命周期执行时机探究

今日上班摸鱼水群时,某群友提出了本文标题涉及的问题,即在运行时通过调用AddComponent方法添加一个新脚本A,A的Awake,Start,Update等生命周期方法会在Add的这一帧执行还是等到下一帧执行。 随后我便马上打开Unity,写了两个测试脚本,具体代码如下: using System.Collections; using System.Collections.Generic;...

道友请留步 1万+

Unity零基础到入门 ☀️| 学会这些Unity常用组件,Unity中必备组件技能学习!

组件开发 组件(Component) 简介: 组件是在游戏对象(Game Object)中的实现某些功能的集合。 无论是模型、 GUI、灯光还是摄像机,所有游戏对象本质上都是一个空对象挂载了不同类别的 组件,从而让该游戏对象拥有不同的功能。 简单说一下Unity开发的框架:项目—>场景—>游戏对象—>组件—>属性 一个游戏由多个场景(Scene)组成 一个场景由多个游戏物体(GameObject)组成 一个游戏物体由多个组件(Component)组成 对于一个空游戏对象来说,如果为其

努力前行,总会成为自己心中的那道光 1万+

Python自动化生成HyperMesh Tcl命令流:从几何建模到有限元分析

本文详细介绍了如何利用Python自动化生成HyperMesh的Tcl命令流,实现从几何建模到有限元分析的全流程二次开发。通过封装常用操作函数,工程师可以高效完成参数化建模、自动网格划分及求解器文件输出,显著提升CAE前处理效率与标准化水平。

weixin_29041767的博客 423

【转】Unity中添加组件的几种方法

http://blog.youkuaiyun.com/monzart7an/article/details/23199647 一、在编辑器上面添加一个组件。这个不用多说。 二、在脚本中利用AddComponent函数添加一个组件,例如: usingUnityEngine;usingSystem.Collections;publicclassCharacterSpawner:MonoBe...

weixin_33877885的博客 465

Cadence Allegro安装全攻略:从准备到优化的完整指南

本文提供了一份详尽的Cadence Allegro安装教程,涵盖从系统准备、硬件要求到获取安装文件的完整前期工作。指南逐步解析了自定义安装、路径选择及核心组件配置,并重点阐述了许可证服务器的环境变量设置与服务启动等关键激活步骤,最后给出了首次启动验证、个性化优化及常见问题排错方案,助您高效完成安装与配置。

weixin_29243063的博客 185

unity移动物体的方法及区别

二、利用Rigidbody rigidbody.AddForce(); rigidbody.MovePosition(); 三、获取轴 Input.GetAxis("Horizontal") * Time.deltaTime * speed; i

aaaaaaaxiba0_0的博客 392

华大HC32F460 ADC+DMA高效数据采集实战解析

本文深入解析了华大HC32F460微控制器中ADC与DMA协同工作的高效数据采集方案。通过实战代码演示,详细阐述了如何配置硬件触发、序列扫描及AOS事件联动,实现后台自动采集,从而解放CPU资源,提升系统实时性与可靠性,适用于传感器数据采集、音频处理等多种场景。

weixin_29189373的博客 177

Origin 2025b中文版安装全指南:避开乱码、水印与崩溃陷阱

Origin作为科研绘图领域的事实标准,其2025b版本首次官方支持简体中文UI,但安装远非‘下一步’即可完成。核心在于理解软件本体、语言包、许可证服务与系统区域设置四者间的耦合关系——尤其Windows非Unicode程序语言必须设为‘中文(简体,中国)’,否则必然触发坐标轴乱码、PDF文字方块等典型故障;而AI辅助拟合、内置Python 3.11等新特性,又依赖正确的字体链(如SimSun)与注册表强制加载(UILanguage=zh-CN)才能稳定启用。该版本广泛应用于材料科学、电化学、XRD/SEM

weixin_42548874的博客 1403

Unity-物理系统-刚体加力

/如果阻力为0 那给了一个力过后 始终 是不会停止运动。//每物理帧移动0.2m/s*0.02 = 0.004m。//每物理帧移动0.1m/s*0.02 = 0.002m。//每物理帧移动10m/s*0.02 = 0.2m。//如果想要在 世界坐标系方法中 让对象 相对于自己的面朝向动。//每物理帧移动5m/s*0.02 = 0.1m。//加力过后 对象是否停止移动 是由阻力决定的。//世界坐标系 Z轴正方向加了一个里。获取刚体是否处于休眠状态 如果是。

m0_53393728的博客 1748

Sony DPT RP1/CP1破解攻略

前言 自己有一台Sony DPT CP1和一台kindle,两个都已经用了很多年。直到最近发现微信读书上经常可以找到一些免费而且不错的书,所以就萌生了在两台机器上使用微信读书的想法。kindle比较容易实现,直接打开内置浏览器,进入微信读书网页版就行了,而Sony的电纸书则只能通过破解+安装微信读书的方式实现。大概弄了两三个小时吧,也遇到了一些问题,所以这里记录一下整个过程,希望能方便后来的使用者。 首先感谢HappyZ, osc_t5nbj8ds。前者是提供公开破解的方法的大神,后者则是解决了我遇到的问题

weixin_43467572的博客 1万+

unity 动画 音频播放

采用Unity进行音频动画的播放时最常用的技术,在此进行一下简单讲解与应用。 (一)动画播放(本文采用animation进行验证,关于animation和animator区别可问度娘,在此不做赘述) 1、播放默认动画 直接将动画拖入动画对象animation组件的animation变量中,然后加入如下代码既可以播放(此处采用toggle控制动画的播放) public Anima...

weixin_30682127的博客 668

100天精通Oracle-实战系列(第14天)Veritas NetBackup(NBU) 配合 RMAN 脚本备份 Oracle 数据库

100天精通Oracle-实战系列(第14天)Veritas NetBackup(NBU) 配合 RMAN 脚本备份 Oracle 数据库

LuciferLiu_DBA 1993

静态时序分析—门控时钟检查(clock gating check)

clock gating check的条件与类型

m0_61544122的博客 9606

Unity 添加Click事件的几种方法总结

所有的点击事件的生效其实都是实现了接口:IPointerClickHandler 里的 OnPointClick 先看看源码: 接口: Button: 所以其实要实现点击事件,就是要实现接口。 总结了以下方法实现点击事件: Button(指组件)添加点击事件: 1.Button组件仔细看看他的各个参数发现一个On Click()/鼠标单击() 下面有3个选择的东西 1.运行生效时间(大概),...

weixin_43381316的博客 3万+

把 MANDT 锁进保险箱:ABAP Client Handling 与 Client Isolation(聚焦 AMDP 的 Client Safety )

本文探讨了ABAP系统中Client Handling与Client Isolation机制的演进,重点分析了AMDP环境下多租户隔离的风险与解决方案。传统ABAP SQL/CDS通过隐式Client过滤提供安全保障,而AMDP因其原生SQLScript特性容易绕过隔离机制。文章对比了旧时代依赖开发自觉的传参方式与新时代通过AMDP OPTIONS强约束的语法方案,详细介绍了CLIENT INDEPENDENT和CDS SESSION CLIENT DEPENDENT两种编译期安全机制,阐述了USING列表

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 340

在unity中获取组件和物体的几种方法(通过代码)

在游戏开发过程中我们要获取unity中的组件和物体 在这篇博客中我将介绍几种方法来获取他们 1.在代码中直接public 在代码中直接public 类型 名称 然后在unity中赋值 这是最直接但是最不方便的方法 在小的项目中可以 但是在比较大的项目中是不可取的 因为物体组件很多很容易遗漏 从而出现很麻烦的Bug 2.Find方法 在unity中官方提供了一个Find的方法 很方便 这个方法的...

weixin_44302602的博客 2万+

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

unity 获取组件,添加组件

#region 获取组件 2 //1.通过泛型的方式获取组件 3 BoxCollider bc = gameObject.GetComponent(); 4 gameObject.GetComponent().material.color = Color.red; 5 6 //2.脚本也是组建可以同样的方法获取 7 ...

weixin_43847546的博客 2228

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

Unity中EventTrigger的几种使用方法

Unity中EventTrigger的几种使用方法 EventTrigger事件触发器的使用可以说是无处不在,EventTrigger继承了很多的事件接口,这些接口对我们开发是十分有用的。弄懂EventTrigger在以后开发游戏过程中可以说是事半功倍。 以下为官方API中的类 namespace UnityEngine.EventSystems { [AddComponentMenu ("Event/Event Trigger")] public class EventTrigger :

qq_45152631的博客 1万+

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

上一篇: Unity3D Assetbundle的实战
下一篇: Unity3D之AStarPathFinding插件的使用
我是一纸巾
博客等级 码龄12年 14粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值