单合一管理类 [英] Unity singleton manager classes
问题描述
在团结,什么好办法来创建随处访问与静态变量的全局类,将吐相同的恒定值,每一个,拉这些值类单身游戏管理员?而这将是实现它在Unity的方式吗?我必须把它连接到一个游戏物体?它可以只是有一个文件夹中没有的场景在视觉上?
In Unity, whats a good way to create a singleton game manager that can be accessed everywhere as a global class with static variables that will spit the same constant values to every class that pulls those values? And what would be the way to implement it in Unity? Do I have to attach it to a GameObject? Can it just be there in a folder without being in the scene visually?
推荐答案
像往常一样:它依赖。我用这两种单件,组件连接到游戏对象
,而不是从 MonoBehaviour
派生单独的类。 IMO的整体问题是如何被实例绑定到场景,游戏对象的lifcycle,...而不是有时会忘记它是更方便的一个组成部分特别引用其他 MonoBehaviour
物体更容易,更安全。
Like always: it depends. I use singletons of both kinds, components attached to GameObject
and standalone classes not derived from MonoBehaviour
. IMO the overall question is how are instances bound to the lifcycle of scenes, game objects, ... And not to forget sometimes it is more convenient to have a component especially referencing other MonoBehaviour
objects is easier and safer.
- 有,只是需要提供例如像,需要调用时加载的持久层设置的配置类的一些值类。我设计theese类作为简单的单身。
- 在另一方面,一些对象需要在一个场景开始认识即
开始
被称为还是得执行更新$行动C $ C>或其它方法。然后,我实现它们的成分,并将其安装到能承受住加载新的场景游戏对象。
- There are classes that just need to provide some values like for example a config class that needs to load settings from persistence layer when called. I design theese classes as simple singletons.
- On the other hand some objects need to know when a scene is started i.e.
Start
is called or have to perform actions inUpdate
or other methods. Then I implement them as component and attach them to a game object that survives loading new scenes.
我设计了基于组件的单身两部分(2型):持久游戏对象
名为主
,其持有所有部件和名为 MainComponentManager
管理它的扁平单(1型)。一些演示code:
I designed component based singletons (type 2) with two parts: a persistent GameObject
called Main
, which holds all components and a flat singleton (type 1) called MainComponentManager
for managing it. Some demo code:
public class MainComponentManger {
private static MainComponentManger instance;
public static void CreateInstance () {
if (instance == null) {
instance = new MainComponentManger ();
GameObject go = GameObject.Find ("Main");
if (go == null) {
go = new GameObject ("Main");
instance.main = go;
// important: make game object persistent:
Object.DontDestroyOnLoad (go);
}
// trigger instantiation of other singletons
Component c = MenuManager.SharedInstance;
// ...
}
}
GameObject main;
public static MainComponentManger SharedInstance {
get {
if (instance == null) {
CreateInstance ();
}
return instance;
}
}
public static T AddMainComponent <T> () where T : UnityEngine.Component {
T t = SharedInstance.main.GetComponent<T> ();
if (t != null) {
return t;
}
return SharedInstance.main.AddComponent <T> ();
}
这要注册为主
组件现在单身其他单看这样的:
Now other singletons that want to register as Main
component just look like:
public class AudioManager : MonoBehaviour {
private static AudioManager instance = null;
public static AudioManager SharedInstance {
get {
if (instance == null) {
instance = MainComponentManger.AddMainComponent<AudioManager> ();
}
return instance;
}
}
这篇关于单合一管理类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!