Unity实战篇:移植游戏到安卓平台的注意事项及其实例(五)(玩家等级,对象池概念的引入以及优化)

版权声明:转载请注明出处!不注明也无所谓,嘿嘿。 https://blog.csdn.net/qq_15020543/article/details/82933629

1.对象池基础概念的了解(必看)

https://blog.csdn.net/qq_15020543/article/details/82933479

2.引入玩家等级概念,根据玩家等级来升级子弹,针对项目来进行嵌入对象池

对于玩家,我们需要多添加几个子弹发射位置。玩家等级和枚举类型皆由GameController控制

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using UnityEngine.UI;

public class Done_GameController : MonoBehaviour
{
    public enum Level
    {
        One,
        Two,
        Three,
        Four,
        Final
    }
    private static Done_GameController instance;
    private AudioManager audioManager;

    public Healthy playerHealth;

    public GameObject[] hazards;
    public GameObject[] UIPanels;
	public Vector3 spawnValues;
	public int hazardCount;
	public float spawnWait;
	public float startWait;
	public float waveWait;
    public bool gameOver = false;
    public bool isDead = false;
    public int level = 1;
    public int score = 0;
    public int loadCount = 1;
    public Level playerLevel = Level.One;


    public static Done_GameController Instance
    {
        get
        {
            return instance;
        }
        set
        {
            instance = value;
        }
    }
	

    private void Awake()
    {
        Instance = this;
        UIPanels[0].SetActive(true);
        audioManager = new AudioManager();
        playerHealth = new Healthy();
        playerHealth.PlayerHealth = 3;
        audioManager.OnInit();
    }
    public void StartIEnumerator()
    {
        StartCoroutine("SpawnWaves");
    }
    public void StopIEnumerator()
    {
        StopCoroutine("SpawnWaves");
    }
    IEnumerator SpawnWaves ()
	{
		yield return new WaitForSeconds (startWait);
		while (true)
		{
			for (int i = 0; i < hazardCount; i++)
			{
				GameObject hazard = hazards [Random.Range (0, hazards.Length)];
                ObjectPool.GetInstance().GetObj(hazard.name, new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z), hazard.transform.rotation);
				yield return new WaitForSeconds (spawnWait);
			}
			yield return new WaitForSeconds (waveWait);
		}
	}
    public void SetPanelInActive(int index)
    {
        UIPanels[index].SetActive(false);
    }
    public void SetPanelActive(int index)
    {
        UIPanels[index].SetActive(true);
    }
    public void PlayNormalSound(string soundName)
    {
        audioManager.PlayNormalSound(soundName);
    }
    public void PlayHitSound(string soundName)
    {
        audioManager.PlayHitSound(soundName);
    }
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class Done_Boundary 
{
	public float xMin, xMax, zMin, zMax;
}


public class Done_PlayerController : MonoBehaviour
{
	public Done_Boundary boundary;

	public GameObject shot;
	public List<Transform> spawns ;
	 
	private float nextFire=0.2f;
    private bool isMouseDown;
    private Vector3 lastMousePosition = Vector3.zero;
    private int playerLevel = 1;

    private void OnEnable()
    {
        isMouseDown = false;
        lastMousePosition = Vector3.zero;
    }
    private void OnDisable()
    {
        isMouseDown = false;
        lastMousePosition = Vector3.zero;
    }

    void Update ()
	{
        if(playerLevel!=Done_GameController.Instance.level)
        {
            ChangePlayerLevel();
        }
		if (nextFire>=0.2f)
        {
            foreach (Transform t in spawns)
            {
                if(t.gameObject.activeSelf)
                ObjectPool.GetInstance().GetObj(ObjectPool.playerAttack, t.position, t.rotation);
            }
            nextFire = 0;
		}
        else
        {
            nextFire += Time.deltaTime; 
        }
        playerLevel = Done_GameController.Instance.level;
        TwoDMove();
	}

    private void TwoDMove()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isMouseDown = true;
        }
        if (Input.GetMouseButtonUp(0))
        {
            isMouseDown = false;
            lastMousePosition = Vector3.zero;//这里要归零,不然会有漂移效果
        }
        if (isMouseDown)
        {
            if (lastMousePosition != Vector3.zero)
            {
                Vector3 offset = Camera.main.ScreenToWorldPoint(Input.mousePosition) - lastMousePosition;
                transform.position += offset;
                GetComponent<Rigidbody>().position = new Vector3
                (
                 Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
                 0.0f,
                 Mathf.Clamp(GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
                );
            }
            lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }
    }
    private void SetAllSpawns()
    {
        foreach (Transform t in spawns)
        {
            t.gameObject.SetActive(false);
        }
    }
    private void ChangePlayerLevel()
    {
        switch (Done_GameController.Instance.level)
        {
            case 1:
                Done_GameController.Instance.playerLevel = Done_GameController.Level.One;
                break;
            case 3:
                Done_GameController.Instance.playerLevel = Done_GameController.Level.Two;
                break;
            case 5:
                Done_GameController.Instance.playerLevel = Done_GameController.Level.Three;
                break;
            case 8:
                Done_GameController.Instance.playerLevel = Done_GameController.Level.Four;
                break;
            case 9:
                Done_GameController.Instance.playerLevel = Done_GameController.Level.Final;
                break;

        }
        switch (Done_GameController.Instance.playerLevel)
        {
            case Done_GameController.Level.One:
                SetAllSpawns();
                spawns[0].gameObject.SetActive(true);
                break;
            case Done_GameController.Level.Two:
                SetAllSpawns();
                spawns[1].gameObject.SetActive(true);
                spawns[2].gameObject.SetActive(true);
                break;
            case Done_GameController.Level.Three:
                SetAllSpawns();
                spawns[0].gameObject.SetActive(true);
                spawns[1].gameObject.SetActive(true);
                spawns[2].gameObject.SetActive(true);
                break;
            case Done_GameController.Level.Four:
                SetAllSpawns();
                spawns[1].gameObject.SetActive(true);
                spawns[2].gameObject.SetActive(true);
                spawns[3].gameObject.SetActive(true);
                spawns[4].gameObject.SetActive(true);
                break;
            case Done_GameController.Level.Final:
                SetAllSpawns();
                spawns[0].gameObject.SetActive(true);
                spawns[1].gameObject.SetActive(true);
                spawns[2].gameObject.SetActive(true);
                spawns[3].gameObject.SetActive(true);
                spawns[4].gameObject.SetActive(true);
                break;
            default: break;
        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class GamePanel : MonoBehaviour {

    private GameObject [] enemy;
    private GameObject[] stone;
    private GameObject[] enemyAttack;
    private Text score;
    private Text percent_exp;
    private Slider exp;
    public GameObject player;
    private void Awake()
    {
        score = transform.Find("Score").GetComponent<Text>();
        exp = transform.Find("Exp").GetComponent<Slider>();
        percent_exp = transform.Find("Exp/Text").GetComponent<Text>();
        score.transform.localPosition = new Vector3(-1500, 819, 0);
        exp.transform.localPosition = new Vector3(1500, 820, 0);
        InActiveUI();
    }

    private void Update()
    {
        if(Done_GameController.Instance.loadCount==2)
        {
            if (Done_GameController.Instance.UIPanels[0].activeSelf == false &&
           Done_GameController.Instance.UIPanels[2].activeSelf == false)
            {

                Done_GameController.Instance.score = 0;
                Done_GameController.Instance.level = 1;
                StartGame();
                Done_GameController.Instance.loadCount++;
            }
        }
        if(Done_GameController.Instance.isDead&& !Done_GameController.Instance.gameOver)
        {
            ClearAllEnemy();
            ObjectPool.GetInstance().GetObj(ObjectPool.player, player.transform.position, player.transform.rotation);
            Done_GameController.Instance.isDead = false;
        }
        if (Done_GameController.Instance.gameOver)
        {
            GameOver();
        }
        score.text = "Score: " + Done_GameController.Instance.score.ToString();
        exp.value =float.Parse((Done_GameController.Instance.score / (1000 * (Mathf.Pow(2, Done_GameController.Instance.level)))).ToString("f2"));
        percent_exp.text = "Level " + Done_GameController.Instance.level.ToString() + "   " + (exp.value*100).ToString()+"%";
        if(exp.value>=1)
        {
            Done_GameController.Instance.level++;
        }
    }
    private void OnEnable()
    {
        Done_GameController.Instance.gameOver = false;
        Done_GameController.Instance.playerHealth.PlayerHealth = 3 ;
        Done_GameController.Instance.level = 1;
        Done_GameController.Instance.loadCount = 1;
        Done_GameController.Instance.loadCount++;
        Done_GameController.Instance.StartIEnumerator();
        ClearAllEnemy();
        ObjectPool.GetInstance().GetObj(ObjectPool.player,player.transform.position,player.transform.rotation);
    }
    private void OnDisable()
    {
        if(Done_GameController.Instance)
        Done_GameController.Instance.StopIEnumerator();
    }
    public void StartGame()
    {
        ActiveUI();
        score.transform.DOLocalMoveX(-233, 0.5f);
        exp.transform.DOLocalMoveX(258, 0.5f);

    }
    public void GameOver()
    {
        score.transform.DOLocalMoveX(-1500, 0.5f);
        exp.transform.DOLocalMoveX(1500, 0.5f);
        Invoke("InActiveUI", 0.5f);
        Invoke("SetPanelInActive", 0.5f);
        Done_GameController.Instance.SetPanelActive(2);


    }
    private void ActiveUI()
    {
        score.gameObject.SetActive(true);
        percent_exp.gameObject.SetActive(true);
        exp.gameObject.SetActive(true);
    }
    private void InActiveUI()
    {
        score.gameObject.SetActive(false);
        percent_exp.gameObject.SetActive(false);
        exp.gameObject.SetActive(false);
    }
    private void SetPanelInActive()
    {
        Done_GameController.Instance.SetPanelInActive(1);
    }
    private void SetPanelActive()
    {
        Done_GameController.Instance.SetPanelActive(1);
    }
    private void ClearAllEnemy()
    {
        enemy = GameObject.FindGameObjectsWithTag("Enemy");
        for (int i = 0; i < enemy.Length; i++)
        {
            if (enemy[i].activeSelf)
                ObjectPool.GetInstance().RecycleObj(enemy[i]);
        }
        stone = GameObject.FindGameObjectsWithTag("Stone");
        for (int i = 0; i < stone.Length; i++)
        {
            if (stone[i].activeSelf)
                ObjectPool.GetInstance().RecycleObj(stone[i]);
        }
        enemyAttack = GameObject.FindGameObjectsWithTag("EnemyAttack");
        for (int i = 0; i < enemyAttack.Length; i++)
        {
            if(enemyAttack[i].activeSelf)
            ObjectPool.GetInstance().RecycleObj(enemyAttack[i]);
        }
    }
}

最终效果

GC表示情绪稳定。

帧数显著提升!

猜你喜欢

转载自blog.csdn.net/qq_15020543/article/details/82933629