Пример #1
0
    void InitContainer()
    {
        var sortedManagers = GetSortedManagers();

        for (int i = 0; i < sortedManagers.Count; ++i)
        {
            if (sortedManagers[i] == null)
            {
                continue;
            }
            if (!isGlobalContainer && ManagerAttributeCache.IsManagerAlwaysGlobal(sortedManagers[i].GetType()))
            {
                Debug.LogError("Cannot add [ManagerAlwaysGlobal] manager prefab \""
                               + sortedManagers[i].name
                               + "\"(" + sortedManagers[i].GetType().Name
                               + ") to non-global container!"
                               + name
                               + ".");
                continue;
            }
            var manager = Instantiate(sortedManagers[i]);
            AddNewManager(manager);
        }

        StartNewManagers();
    }
    public static T GetManager <T>(Scene scene) where T : Manager
    {
        T manager = null;
        ManagerContainer sceneContainer = null;

        if (ManagerAttributeCache.IsManagerAlwaysGlobal(typeof(T)))
        {
            return(GetGlobalManager <T>());
        }

        if (!s_managerContainers.TryGetValue(scene, out sceneContainer))
        {
            // if a a global container exists, check it before creating a scene one
            // while creating a blank scene manager would be valid, it is unexpected beahaviour.
            if (s_globalContainer != null)
            {
                manager = s_globalContainer.LookupManager(typeof(T)) as T;
                if (manager != null)
                {
                    return(manager);
                }
            }

            var gameObject = new GameObject(scene.name + " ManagerContainer (autogenerated)");
            sceneContainer = gameObject.AddComponent <ManagerContainer>();
        }

        manager = sceneContainer.LookupManager(typeof(T)) as T;
        if (manager != null)
        {
            return(manager);
        }

        if (s_globalContainer != null)
        {
            manager = s_globalContainer.LookupManager(typeof(T)) as T;
            if (manager != null)
            {
                return(manager);
            }
        }

        if (sceneContainer != null)
        {
            manager = sceneContainer.AutoconstructManager(typeof(T)) as T;
            if (manager != null)
            {
                return(manager);
            }
        }

        return(null);
    }