简体   繁体   English

Unity/C# - 在 Inspector 中使用 [Serializefield] 与 Find 方法

[英]Unity/C# - Using [Serializefield] in Inspector vs a Find Method

I am building a simple one scene mobile arcade game in Unity as part of my attempt to learn to code.我正在 Unity 中构建一个简单的单场景移动街机游戏,作为我学习编码的一部分。 The game might have multiple levels, say 100. To build the first level, I have used multiple scripts which are all referencing each other via [serializefield] in the inspector.游戏可能有多个级别,比如 100 个。为了构建第一个级别,我使用了多个脚本,这些脚本都通过检查器中的[serializefield]相互引用。 Moreover, there are multiple UI textboxes displaying info, which are also linked to relevant scripts in the inspector.此外,还有多个显示信息的 UI 文本框,这些文本框也链接到检查器中的相关脚本。 I did this as I thought it is the most efficient way after doing a bit of research (basically avoiding the engine looking for an already existing object in the scene).我这样做是因为我认为这是在做了一些研究之后最有效的方法(基本上避免了引擎在场景中寻找已经存在的 object)。 I prefabbed everything, including the empty game objects holding the scripts and moved to the second level.我预制了所有东西,包括持有脚本的空游戏对象并移至第二层。

However...然而...

When I started building the second level, which would essentially contain the same game objects with different parameters, I noticed that when I dropped the prefabs into the scene I had to manually re-link all references in the inspector.当我开始构建第二个关卡时,它基本上包含具有不同参数的相同游戏对象,我注意到当我将预制件放入场景中时,我必须手动重新链接检查器中的所有引用。 With so many links to make over hundreds of levels, this would be very tedious and I would keep missing some links.有这么多的链接要超过数百个级别,这将是非常乏味的,我会一直错过一些链接。 It is all well and good to drop prefabbed objects in the scene but the references have to be re-established for each level directly in the scene.在场景中放置预制对象非常好,但是必须直接在场景中为每个级别重新建立引用。 Having prefabs link to prefabs does not work (I tried it).将预制件链接到预制件不起作用(我试过了)。 So, there seems to be a lot of manual linking work this way.因此,这种方式似乎有很多手动链接工作。

So the upshot is...所以结果是...

I am starting to think I am better off using GameObject.Find("string") and FindObjectOfType<> for all my references to avoid having to continually make lots of links in the inspector for each level.我开始认为我最好将GameObject.Find("string")FindObjectOfType<>用于我的所有引用,以避免在每个级别的检查器中不断创建大量链接。

However...然而...

Checking on forums, people say Find methods are inefficient and should be avoided, how inefficient I don't know.检查论坛,人们说 Find 方法效率低下,应该避免,我不知道效率如何。 That concerns me as this will be a mobile game.这让我很担心,因为这将是一款手机游戏。 Most of them are recommending [serializefield] and links in the Inspector as the best solution.他们中的大多数都推荐[serializefield]和 Inspector 中的链接作为最佳解决方案。

So, I am wondering if you can shed some light from your experience.所以,我想知道你是否可以从你的经历中得到一些启发。 What would work best for my situation?什么最适合我的情况? Other options I haven't considered?我没有考虑过的其他选择?

Many thanks.非常感谢。

You don't need to search for objects, ever .您永远不需要搜索对象 Just makes sure those objects' existence is tracked with some kind of static field.只需确保使用某种 static 字段跟踪这些对象的存在即可。

For many instances of something (like players or enemies):对于某些事物的许多实例(例如玩家或敌人):

  • static list static列表

For manager classes etc:对于经理类等:

Simplest & best way to track life cycle of a pool of objects is this:跟踪对象池生命周期的最简单和最好的方法是:

public class Enemy : MonoBehaviour
{
    public static List<Enemy> instances = new List<Enemy>(100);
    void OnEnable () => instances.Add( this );
    void OnDisable () => instances.Remove( this );
}

With little care this list will self-maintain to be always correct - after making sure you don't access this list before all OnEnable are called (in Start and Update is totally fine).稍加注意,此列表将自我维护始终正确 - 在确保您在调用所有OnEnable之前不访问此列表之后(在StartUpdate中完全没问题)。

As you can hopefully see - search is just being lazy.正如您所希望看到的 - 搜索只是懒惰。

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

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