如何在Unity中使用单例?

发布于 2025-02-06 16:02:26 字数 203 浏览 2 评论 0原文

在我的游戏中,我创建了一个对象摄像头,其中包含主摄像头,还有2个脚本,相机控制器,可以控制摄像机和Teammanager的运动,旋转和缩放器,可接收单元列表,以便我可以切换相机位置在团队成员之间。

我一直在尝试在场景之间传递这种摄影作用,所以我认为使用单身人士是正确的事情,我的疑问是,我在哪里实施所有代码?

抱歉,如果这是一个广泛的问题,我感谢我能得到的任何帮助

In my game I have created an object CameraRig, it contains the main camera, also it has 2 scripts, CameraController which controls the movement, rotation, and zoom of the camera and TeamManager which receives a List of units so I can switch the camera position between the members of the team.

I have been trying to pass this CameraRig between my scenes so I think using a singleton is the right thing to do, my doubt is, where do I implement all the code for that?

sorry if this can be a broad question, I'm thankful with any help I can get

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

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

发布评论

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

评论(2

半衾梦 2025-02-13 16:02:27

如果您要切换场景,您可能不得不使用附加的脚本使用预贴,因为我认为gameObject s幸存场景过渡。

您可以在不使用static的情况下安全地实现单身人士。想法是,您从空白 Game Manager 3D对象(Unity甚至具有 tag 为此目的为“ Game Manager” )您连接任何Monobehaviour s的场景。我称其为“ soft singleton” ,因为缺乏更好的名字。

Scene
|--GameObject "Game Manager", tag = "GameManager"
|  |--CameraRig 
|  |--AnotherSingleton
|
. (other scene objects)

然后,任何寻找Singleton C#对象的脚本,而不是像我们通常在AC#Singleton模式中那样寻找C#对象的实例,而是通过findwithTag(“ GameManager”)来查询unity gameObject然后调用getComponent< mysingleton>()

这是实现统一兼容单例而不是使用static的一种更安全的方法,如果不采用护理,这可能会在Unity上有问题。

c#“ singleton”行为是GameObject的Lifetime 不是如何定义C类的结果,对于所有其他MonoBehaviour也是如此,这也是如此。 。

gameObject s应该始终控制MonoBehaviour的和子对象的寿命。使用静态实质上导致创建的孤立对象。

You would probably have to use pre-fabs with the scripts attached if you are going to switch scenes as I don't think GameObjects survive scene transition.

You can safely implement singletons in Unity without using static. The idea is that you start off with a blank game manager 3D object (Unity even has the tag "Game Manager" for this purpose) in the scene to which you attach any MonoBehaviours . I call this a soft singleton for want of a better name.

Scene
|--GameObject "Game Manager", tag = "GameManager"
|  |--CameraRig 
|  |--AnotherSingleton
|
. (other scene objects)

Then any script looking for the singleton c# object, instead of looking for the instance of the c# object as we normally would in a c# singleton patterns, you instead query Unity via FindWithTag("GameManager") to get the GameObject then call GetComponent<MySingleton>().

This is a safer way of implementing Unity-compatible singletons rather than using static, something that can be problematic in Unity if care is not taken.

The C# "singleton" behaviour are the result of the GameObject's lifetime not how the C# class is defined and this is also true for all other MonoBehaviour attached to a GameObject.

GameObjects should always be in control of a MonoBehaviour's and child object's lifetime. Using static essentially leads to orphaned objects created adhoc.

烟火散人牵绊 2025-02-13 16:02:27

如果您想在不同的场景中分享一些单声言,可以使用所谓的“ Monosingleton”。通过调用 doneDestroyonload()正在加载,静态变量使您可以从项目中的任何代码访问此实例。

这是此类模式实现的示例

If you want to share some monobehaviour across different scenes, you can use the so-called "monosingleton". By calling DontDestroyOnLoad() you tell unity not to destroy an object when another scene is loading and the static variable allows you to have access to this instance from any piece of code in your project.

Here is an example of such pattern implementation.

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