如何将自定义类添加为unity的GameObject的组件?

发布于 2025-02-09 13:52:58 字数 90 浏览 3 评论 0原文

因此,我有一个预先实例化的预制措施,该预制是我创建的那个预制,并在小时候添加了一个带有组件图像的游戏对象。现在,如何将自定义类数据作为组件添加到预制的新创建的孩子中?

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. Now here how can I add a custom class data as component to the newly created child of prefab?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小猫一只 2025-02-16 13:52:58

为了能够将您的自定义脚本添加到场景上的游戏对象中,必须从Monobehaviour继承此自定义脚本。之后,您可以在Inspector中添加类“ Do Game对象”(通过单击添加组件按钮,请参见下面的图像),也可以通过调用

gameObjToAddComponent.AddComponent<MyAwesomeComponentType>();

To be able to add your custom script to the game object on the scene, this custom script must be inherited from 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

gameObjToAddComponent.AddComponent<MyAwesomeComponentType>();

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文