Beispiel #1
0
    protected override void Awake()
    {
        base.Awake();

        // Initialize the pools
        if (Instance == this)
        {
            // This manager should persist after level restarts
            transform.SetParent(null, false);
            DontDestroyOnLoad(gameObject);

            piecePool.CreateFunction = () =>
            {
                HexagonPiece result = Instantiate(piecePrefab);
                SceneManager.MoveGameObjectToScene(result.gameObject, gameObject.scene);                   // So that the piece will be DontDestroyOnLoad'ed
                result.transform.localScale = new Vector3(GridManager.PIECE_WIDTH, GridManager.PIECE_WIDTH, GridManager.PIECE_WIDTH);

                return(result);
            };
            piecePool.OnPop  = (piece) => piece.gameObject.SetActive(true);
            piecePool.OnPush = (piece) => piece.gameObject.SetActive(false);
            piecePool.Populate(32);

            bombPool.CreateFunction = () =>
            {
                HexagonBomb result = Instantiate(bombPrefab);
                SceneManager.MoveGameObjectToScene(result.gameObject, gameObject.scene);                   // So that the bomb will be DontDestroyOnLoad'ed
                result.transform.localScale = new Vector3(GridManager.PIECE_WIDTH, GridManager.PIECE_WIDTH, GridManager.PIECE_WIDTH);

                return(result);
            };
            bombPool.OnPop  = (bomb) => bomb.gameObject.SetActive(true);
            bombPool.OnPush = (bomb) =>
            {
                bomb.gameObject.SetActive(false);
                bomb.transform.SetParent(null, false);                   // Bombs are attached to their target hexagon pieces, release the bomb
            };
            bombPool.Populate(2);

            matchPool.CreateFunction = () => new HexagonMatch();
            matchPool.OnPush         = (match) => match.Clear();
            matchPool.Populate(8);

            moveAnimationsPool.CreateFunction = () => new AnimationManager.MovePieceAnimation();
            moveAnimationsPool.Populate(64);

            blowAnimationsPool.CreateFunction = () => new AnimationManager.BlowPieceAnimation();
            blowAnimationsPool.Populate(8);
        }
    }
Beispiel #2
0
    // Simple pool initializer that set the go inactive when pushed and active when popped.
    public static SimplePool <T> PopulateSimplePool <T>(SimplePool <T> simplePool, GameObject blueprint, string name, int size, Transform poolParent) where T : Component
    {
        simplePool        = GetPool <T>(name);
        simplePool.OnPush = (item) => item.gameObject.SetActive(false);
        simplePool.OnPop  = (item) => item.gameObject.SetActive(true);

        simplePool.CreateFunction = (template) =>
        {
            T newObject = GameObject.Instantiate(blueprint).GetComponent <T>();
            if (newObject)
            {
                newObject.transform.SetParent(poolParent);
            }

            return(newObject);
        };
        simplePool.Populate(size);
        return(simplePool);
    }