如何从预制件中获取场景中游戏对象的参考 [英] How can I grab a reference of a game-object in a scene from a prefab

查看:34
本文介绍了如何从预制件中获取场景中游戏对象的参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在开发一个设备系统,它将从列表中实例化游戏对象到场景中,在此之前我没有使用实例化系统,只是让游戏对象始终在场景中保持活动状态.

currently I am working on a Equipment system that will Instantiate Game objects into the scene from a list, prior to this I was not using a Instantiation system with just keeping the Game Objects Active in the scene at all time.

现在通过实例化游戏对象(预制件),当我实例化它们时,我遇到了丢失引用的错误,我之前看过各种其他文章,例如这里的讨论:

Now with Instantiating the Game Object(Prefab) I am running into the error of Loosing the References when I instantiate them, I have looked at various other articles before such as this Discussion here:

但是,我正在寻找除使用标签以外的其他方法,因为该系统可能会使用大量标签降低游戏性能加班,有人知道将场景中的引用保存到预制件中的不同方法吗?

However I am looking at alternative ways other than using Tags as this system may use a large amount of tags decreasing game performance overtime, would anyone know different methods as to saving references from a scene into a prefab?

因此,由于这是一个新系统,我实际上只是在寻找有关如何将场景游戏对象的引用保存到预制件中的建议,因为我的旧系统只是在场景中保持游戏对象处于活动状态,而不是实际实例化它们,我再次希望听到不同类型的方法,而不仅仅是使用标签.

So as this is a new system I am really just looking for suggestions on how to save references of scene game-objects into a prefab, as my old system was just holding the game objects active in the scene and not actually instantiating them, once again I would love to hear different types of methods, something more than just using Tags.

我试图保存的参考是来自附加到我的预制件的脚本的三个变换元素,它们是以下

The References im trying to save are three Transform Elements from a script attached to my Prefab they are the following

    public Transform camPos;
    public Transform aimLocation;
    public Transform crosshairLocation;

目前我只是将游戏对象从场景中拖放到脚本中的公共字段中

Currently I am just dragging and dropping the gameobjects from the scene into the public fields in the script

由于这将是一个主要系统,我不想在这个系统中使用标签.但这绝对是一个有效的选项,我只是在寻找其他方法将场景游戏对象的引用保存到预制件中,将预制件实例化到场景中时,

Since this will be a major system I would like to not want to use tags for this system. But it is definitely a Valid option I am just looking for other methods for saving references of scene game-objects into a prefab When Instantiating the prefab into the scene,

感谢您花时间阅读,任何建议将不胜感激!

Thank you for taking the time to read and any suggestions would be greatly Appreciated!

有任何问题都可以问!

推荐答案

您可以在预制件中保存场景引用,原因很明显(如果您将该预制件实例化到另一个场景中会发生什么?).您的预制件需要在实例化后发现它需要的对象.我不太喜欢 Unity 中的标签系统(或任何其他使用字符串作为标识符的系统,错误空间太大).我建议您采用以下三种方法中的任何一种:

You can not save scene references in prefabs, for obvious reasons (what happens if you instantiate that prefab into another scene?). Your prefab needs to discover the objects it needs after it's instantiated. I'm not a huge fan of the tag system in Unity (or any other system that uses strings as identifiers, too much room for error). I'd recommend you take any of these three approaches:

  1. 使您需要的变换(凸轮位置、瞄准位置等)成为单例,如果每个变换只有一个.您可以为每个脚本类专门创建一个脚本类,使该类成为单例,并将其放在游戏对象上.然后,在您的预制件实例化后,您可以执行以下操作: camPos = CamPos.Instance.transform; 您可以阅读单例模式 此处.

  1. Make the transforms you need (cam pos, aim location, etc.) singletons if there's ever only going to be one of each. You'd make a script class specifically for each one, make that class a singleton, and put it on the game objects. Then, after your prefab is instantiated, you can do something like this: camPos = CamPos.Instance.transform; You can read up on the singleton pattern here.

如果每个对象都不止一个,请将所有相关对象分组到一个层次结构中(即,使它们全部成为单个游戏对象的子代).例如,如果将有许多角色并且每个角色都有一个单独的瞄准位置,您可以将瞄准位置作为角色游戏对象的子级.然后,在您的预制件中(注意使预制件也成为同一根游戏对象的子代),您可以执行以下操作:camPos = transform.root.GetComponentInChildren().transform; 更好的是,您可以拥有一个引用所有此类对象的组件,并将一个组件附加到根游戏对象.然后,你可以这样做:camPos = transform.root.GetComponent().CamPos;

If there's going to be more than one of each, group all related objects into a single hierarchy (that is, make them all the child of a single game object). For example, if there are going to be many characters and each is going to have a separate aim location, you can make aim location a child of the character game object. Then, in your prefab (take care to make the prefab a child of the same root game object as well), you can do something along these lines: camPos = transform.root.GetComponentInChildren<CamPos>().transform; Better yet, you can have a component that has references to all such objects, and attach one to the root game object. Then, you can do: camPos = transform.root.GetComponent<ReferenceRepository>().CamPos;

也就是说,无论如何,我不认为将目标位置和十字准线位置之类的东西分开对象是一个很好的主意.您应该有一个玩家/人类/pawn 类,该类可以向需要它的其他对象提供此信息.

That said, I don't think making stuff like aim location and crosshair location separate objects is a very good idea anyway. You should have a player/human/pawn class, and that class can provide this information to other objects that need it.

这篇关于如何从预制件中获取场景中游戏对象的参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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