简体   繁体   English

在重新加载场景时,如何使我的Singleton MonoBehaviour类的实例以新的方式启动?

[英]How can I make my instance of a Singleton MonoBehaviour class start as new when reloading the scene?

I have a 'SingletonManager' class with an instance of the 'LevelManager' singleton (and interface to it). 我有一个'SingletonManager'类,带有'LevelManager'单例的实例(以及它的接口)。 The 'LevelManager' GameObject is not supposed to be persistant throughout the application lifetime and should be replaced with a new instance every time a new level is loaded. 'LevelManager'GameObject不应该在整个应用程序生命周期中持久存在,并且每次加载新级别时都应该用新实例替换。

While running my current code, when the scene is loaded for the second time, 'LevelManager' seems to be missing, despite the GameObject (with script attached) being in the scene. 在运行我当前的代码时,当第二次加载场景时,尽管游戏对象(附带脚本)在场景中,但“LevelManager”似乎丢失了。 I realise that this is probably because I am still trying to access the old reference. 我意识到这可能是因为我仍在尝试访问旧的引用。

What is the best way to ensure that my 'SingletonManager' holds the new instance of 'LevelManager' every time a new level is loaded? 每次加载新级别时,确保我的'SingletonManager'保存'LevelManager'的新实例的最佳方法是什么?

My code (with irrelevance removed): 我的代码(删除了无关紧要):

SingletonManager: SingletonManager:

public class SingletonManager
{
    private static LevelManager instance;
    public static ILevelManager Instance
    {
        get
        {
            if (this.levelManager == null)
            {
                this.levelManager = GameObject.FindObjectOfType(typeof(LevelManager)) as LevelManager;
            }
            return this.levelManager;
        }
    }
}

Singleton, is not persistant: 单身,不是持久的:

public class Singleton<Instance> : MonoBehaviour where Instance : Singleton<Instance> {
    public static Instance instance;
    public bool isPersistant;

    public virtual void Awake() {
        if(isPersistant) {
            if(!instance) {
                instance = this as Instance;
            }
            else {
                DestroyObject(gameObject);
            }
            DontDestroyOnLoad(gameObject);
        }
        else {
            instance = this as Instance;
        }
    }
}

What i understand from your question is you need a new object as game stage is change , 我从你的问题中理解的是, 你需要一个新的对象,因为游戏阶段是变化的
If i understand correctly then this may helps you. 如果我理解正确,那么这可能对你有所帮助。
Try Multiton pattern for your solution. 为您的解决方案尝试Multiton模式
here is an example 这是一个例子

class levelManager {
    private static readonly Dictionary<object, levelManager> _instances = new Dictionary<object, levelManager>();

    private levelManager() {
    }

    public static levelManager GetInstance(object key) { // unique key for your stage
        lock (_instances) {   
            levelManager instance;
            if (!_instances.TryGetValue(key, out instance)) {
                instance = new levelManager();
                _instances.Add(key, instance);
            }
            return instance;
        }
    }
}

Note:- I just give you an example not perfect code for your class 注意: - 我只给你一个例子,不是你班级的完美代码
Reference link : http://en.wikipedia.org/wiki/Multiton_pattern 参考链接: http//en.wikipedia.org/wiki/Multiton_pattern

Simple solution: 简单方案:

Create a manager for the level managers. 为级别经理创建经理。 It should be persistant, and have an array of LevelManager prefabs (I assume you're instantiating them from a prefab?). 它应该是持久的,并且有一系列LevelManager预制件(我假设你是从预制件中实例化它们的?)。 When each level is loaded, have it spawn the appropriate LevelManager prefab (or build it or whatever is needed to create it). 加载每个级别后,让它生成相应的LevelManager预制件(或构建它或创建它所需的任何内容)。

Each level manager should simply set the instance in Awake, and let the level destroy it at the end. 每个级别管理器应该只在Awake中设置实例,并让级别在最后销毁它。 (ie. your non-persistant singleton.) (即你的非持久单身人士。)

public class LevelManagerSpawner
{
    public GameObject[] LevelManagerPrefabs;

    void OnLevelWasLoaded( int levelNumber )
    {
        Instantiate( LevelManagerPrefabs[ levelNumber ] );
    }
}

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

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