如何在Unity中使用单例?
在我的游戏中,我创建了一个对象摄像头,其中包含主摄像头,还有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您要切换场景,您可能不得不使用附加的脚本使用预贴,因为我认为
gameObject
s幸存场景过渡。您可以在不使用
static
的情况下安全地实现单身人士。想法是,您从空白 Game Manager 3D对象(Unity甚至具有 tag 为此目的为“ Game Manager” )您连接任何Monobehaviour
s的场景。我称其为“ soft singleton” ,因为缺乏更好的名字。然后,任何寻找Singleton C#对象的脚本,而不是像我们通常在AC#Singleton模式中那样寻找C#对象的实例,而是通过
findwithTag(“ GameManager”)
来查询unitygameObject
然后调用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
GameObject
s 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 anyMonoBehaviour
s . I call this a soft singleton for want of a better name.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 theGameObject
then callGetComponent<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 aGameObject
.GameObject
s should always be in control of aMonoBehaviour
's and child object's lifetime. Usingstatic
essentially leads to orphaned objects created adhoc.如果您想在不同的场景中分享一些单声言,可以使用所谓的“ 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.