简体   繁体   English

如何将自定义类作为组件统一添加到游戏对象?

[英]How to add custom class as a component to gameobject in unity?

So I have a prefab which is Instantiated in run time, to that prefab I create and add another game object with components Image as a child.所以我有一个在运行时实例化的预制件,我在该预制件上创建并添加另一个游戏对象,其中包含组件 Image 作为子组件。 Now here how can I add a custom class data as component to the newly created child of prefab?现在在这里如何将自定义类数据作为组件添加到新创建的预制子节点中?

To be able to add your custom script to the game object on the scene, this custom script must be inherited from MonoBehaviour .为了能够将您的自定义脚本添加到场景中的游戏对象,此自定义脚本必须继承自MonoBehaviour After that, you can add your class do game object in the inspector (by clicking Add Component button, see image below) or from code by calling之后,您可以在检查器中添加您的类做游戏对象(通过单击Add Component按钮,见下图)或通过调用从代码中添加

gameObjToAddComponent.AddComponent<MyAwesomeComponentType>();

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM