将类的实例连接到GameObject网格的正确方法 [英] Correct way to connect an instance of a class to a GameObject mesh

查看:115
本文介绍了将类的实例连接到GameObject网格的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有动画师的模型;和一个控制器脚本来使其移动,然后我创建了一个简单的类"TheEntity",其中包含一个名称和一个int能量.随着时间的流逝,能量会减少,因此网格会四处走动或随机执行动画.

I have a model with an animator; and a controller script to make it move, then I created a simple class called "TheEntity", which hold a name, and an int for the energy. When the time goes by, the energy goes down, so the mesh walk around or perform animations at random.

public class TheEntity()
{
    public string name;
    public int energy;

    public TheEntity()
    {
        // make a random name
        name = "joe"+ rnd.next(1,1000).toString();
        energy = rnd.next(20, 100);
    }

}

当能量值变为0时,网格进入睡眠状态",并在一定时间内重新生成.

When the energy value goes to 0, the mesh "goes to sleep", and regenerate again in a certain amount of time.

如果我有一个TheEntity实例列表,就像我有一个NPC列表一样,那么为列表中的每个实体类分配网格的正确方法是什么?

If I have a list of TheEntity instances, as it would be if I have a list of NPC, what would be the correct way to assign a mesh to each entity class in the list?

我应该在网格物体上有一个对TheEntity类的引用的脚本.并在加载网格预制件时在运行时分配它? 还是应该将整个"TheEntity"类脚本放到网格上,然后将其保存为预制件,因此每次加载预制件时,我都会直接拥有一个与相关的TheEntity实例关联的网格?

Should I have a script on the mesh, that has a reference to a TheEntity class. and assign it at runtime when I load the mesh prefab? Or should I put the whole "TheEntity" class script on the mesh, and save it as prefab, so every time that I load the prefab, I will have a mesh with a related TheEntity instance directly?

推荐答案

  1. Unity场景中的事物"是GameObject,仅此而已.

您的课程必须是MonoBehaviour才能出现在游戏对象上.

Your class must be a MonoBehaviour to be on a game object.

在Unity中,所有内容都是MonoBehaviour(a Component).从字面上看,Unity中什么也没有.

In Unity, everything is a MonoBehaviour (a Component). There is, quite literally, nothing else whatsoever in Unity.

(当然,您可能有一些原始"非Unity类用于诸如数学计算之类的事情,但这无关紧要.)

(Of course, you may have some "raw" non-Unity classes for things like say math calculations, but that's irrelevant.)

就这么简单.

public Class Entity:Monobehaviour
{
}

将其附加到一个空的游戏对象上.添加模型(网格)..或任何您想要的.添加声音效果,添加任何内容.

attach that to an empty game object. Add the models (meshes) .. or whatever you want. Add sound effects, add anything.

关于更换网格",没问题.

Regarding "changing the mesh", no problem.

如果愿意,可以在Entity中的例程中进行此操作.

Do that in a routine in Entity, if you like.

public Class Entity:Monobehaviour
{
public void ChooseRandomMesh()
{
}
public void ChooseRandomColorPattern()
{
}
public void RunForTheHills()
{
}
public void AttackHero()
{
}
}

如果愿意,可以编写Component,它只能随机更改网格.

If you prefer, write a Component which does nothing other than randomly change the mesh.

public Class Entity:Monobehaviour
{
public void RandomizeEntity()
{
}
public void ChooseRandomColorPattern()
{
}
}

..并将该脚本附加到游戏对象上.

.. and attach that script to the game object, also.

在Unity中,一切都是MonoBehaviour(一个Component),就这么简单.

In Unity, everything is a MonoBehaviour (a Component), it's that simple.

关于将其制成预制件,请确保这样做.阅读关于预制件的数千个教程.

Regarding making it a prefab, if you want to, sure do that. Read any of thousands of tutorials on prefabs.

您在下面的评论中有一条关键评论:

There's a critical comment you made in your comments below:

实体类也不是MonoBehaviour ..."

"Also the entity class is not MonoBehaviour..."

在理解Unity方面,这是一个非常关键的要点:

Here's an incredibly critical point in understanding Unity:

1)您完全正确,您的模型"或"AI"或数据库连接"与统一游戏对象无关". (他们当然没有位置"或质量!"之类的东西,对吧?!)

1) You're quite right that your "model" or "AI" or "database connection" sort of has "nothing to do" with unity game objects. (They of course don't have a "position" or "mass!" or anything, right?!)

2)在Unity中,除非一个类是MonoBehaviour:您不能用它做任何事情/您甚至不能访问运行循环,这完全是个入门.

2) In Unity unless a class is a MonoBehaviour: you can't do anything with it/ You can't even access the run loop, it's a total nonstarter.

3)在Unity中,像这样的所有东西,实际上是一种MonoBehaviour,您只需将其放在一个空的游戏对象上. (例如,继承制度中的名称通常以下划线开头,因此您知道它不是真的"是常规游戏对象.)

3) In Unity all the stuff like that, IS IN FACT a MonoBehaviour AND YOU SIMPLY sit it on an empty game object. (Usually the name in the Heirarchy starts with underscore, say, so you know it's "not really" a conventional game object.)

简单的底线就在您的预加载场景中(您必须有100%的项目),您拥有所有的"AI",模型"和数据库"内容,只是坐在一个或多个假装"标记上(标记==,否则为空游戏对象).

The simple bottom line is in your preload scene (you must have one in 100% of projects) you have all your "AI" and "model" and "database" stuff, just sitting on one or more "pretend" markers (marker == otherwise empty game object).

最重要的是,当您在下面说您的模型不是MonoBehaviour"时,这是错误的.它必须是MonoBehaviour(如果考虑到这一点,这绝对是不可避免的,如果没有其他要求,则需要访问runloop)...只需使其成为MonoBehaviour并将其放在标记上,几乎可以肯定是在您的预加载中

Bottom line, when you say below "your model is not a MonoBehaviour" that is wrong. It will have to be a MonoBehaviour (if you think about it, it's absolutely inevitable you'll need to access the runloop, if nothing else) ... just make it a MonoBehaviour and put it on a marker, almost certainly in your preload.

我希望这是有道理的. 论文.

I hope it makes sense. Essay on the topic.

这篇关于将类的实例连接到GameObject网格的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆