Inheritance: MonoBehaviour
示例#1
0
    // Initialize the singleton instance
    private void Awake()
    {
        // If there is not already an instance of GameManager, set it to this
        if (Instance == null)
        {
            Instance = this;
        }
        // If an instance already exists, destroy whatever this object is to enforce the singleton
        else if (Instance != this)
        {
            Destroy(gameObject);
        }

        // Set GameManager to DontDestroyOnLoad so that it won't be destroyed when reloading our scene
        DontDestroyOnLoad(gameObject);

        // set the asset palette up only one time
        if (assetPalette == null)
        {
            assetPalette     = GetComponent <AssetPalette>();
            enemyPrefabCount = Enum.GetNames(typeof(AssetPalette.EnemyList)).Length;
        }

        // initialize the player numbers
        ResetPlayerLives();
        ResetPointsCollected();
    }
    private void SpawnEnemies()
    {
        if (GameObject.FindGameObjectsWithTag("Enemy").Length == 0)
        {
            // set the asset palette up only one time
            if (assetPalette == null)
            {
                assetPalette     = GetComponent <AssetPalette>();
                enemyPrefabCount = Enum.GetNames(typeof(AssetPalette.EnemyList)).Length;
            }
            // 5 enemies at most on screen at one time
            int          randomEnemyCount = UnityEngine.Random.Range(1, 6);
            GameObject[] randomEnemies    = new GameObject[randomEnemyCount];
            for (int i = 0; i < randomEnemyCount; i++)
            {
                // pick a random enemy and position it
                int enemyIndex = UnityEngine.Random.Range(0, enemyPrefabCount);
                randomEnemies[i]      = Instantiate(assetPalette.enemyPrefabs[enemyIndex]);
                randomEnemies[i].name = assetPalette.enemyPrefabs[enemyIndex].name;
                randomEnemies[i].transform.position = new Vector3(worldViewCoords.Right + UnityEngine.Random.Range(0, 1f), UnityEngine.Random.Range(-1f, 1f), 0);
                // for enemies with colors, randomize them
                switch (randomEnemies[i].name)
                {
                case "BigEye":
                    // and dropping big eye into the scene inside camera view
                    randomEnemies[i].transform.position = new Vector3(worldViewCoords.Right - UnityEngine.Random.Range(0.75f, 1.5f), 2f, 0);
                    randomEnemies[i].GetComponent <BigEyeController>().SetColor((BigEyeController.BigEyeColors)UnityEngine.Random.Range(0, Enum.GetNames(typeof(BigEyeController.BigEyeColors)).Length));
                    break;

                case "KillerBomb":
                    randomEnemies[i].GetComponent <KillerBombController>().SetColor((KillerBombController.KillerBombColors)UnityEngine.Random.Range(0, Enum.GetNames(typeof(KillerBombController.KillerBombColors)).Length));
                    break;
                }
            }
        }
    }