Unity实战篇:移植游戏到安卓平台的注意事项及其实例(四)(FixBug,生命概念的引入和Audio优化)
由于我们重建项目的时候,并没有采用原项目那样的场景重载,所以生成敌人的那个协程就出了问题,每次再来一次游戏的时候,刚开始那段时间会出现大量的敌人,基本逃不过死亡的结局。所以我们要修改一下。
我们在GameController里面创建对外的两个函数(StartIEnumerator和StopIEnumerator)来控制生成敌人的开始与结束。
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using UnityEngine.UI;
public class Done_GameController : MonoBehaviour
{
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 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)];
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
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);
}
}
在GamePanel里面调用
*****************************************************************************************************************************************
我们引入玩家生命次数,和敌人的生命值概念,让我们的等级变得有意义。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Healthy {
private int enemyHealth;
private int playerHealth;
public int EnemyHealth
{
get
{
return enemyHealth;
}
set
{
enemyHealth = value;
}
}
public int PlayerHealth
{
get
{
return playerHealth;
}
set
{
playerHealth = value;
}
}
}
GamePanel部分代码做如下修改
***********************************************************************************************************************************************
前面我们提到了Audio的占用率很高的问题。导致它的原因有两点。
1.频繁的Instantiate含有Audio Source组件的游戏物体。
2.频繁的播放
针对第一个问题,我们可以减少Audio Source组件的数量,所以我们新建一个AudioManager脚本,用来管理音乐的播放。它的初始化和调用我们放在GameController里面
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManager
{
private const string Sound_Prefix = "Audio/";
public const string explosion_asteroid = "explosion_asteroid";
public const string explosion_enemy = "explosion_enemy";
public const string explosion_player = "explosion_player";
public const string music_background = "music_background";
public const string weapon_enemy = "weapon_enemy";
public const string Hit = "weapon_player";//由于我们关闭了玩家射击声效,所以我们用玩家射击声效做击中声效
private AudioSource bgAudioSource;
private AudioSource normalAudioSource;
public void OnInit()
{
GameObject audioSourceGo = new GameObject("AudioSource(GameObject)");
bgAudioSource = audioSourceGo.AddComponent<AudioSource>();
normalAudioSource = audioSourceGo.AddComponent<AudioSource>();
PlaySound(bgAudioSource, 0.5f, LoadSound(music_background),true);
}
public void PlayNormalSound(string soundName)
{
PlaySound(normalAudioSource, 1.0f, LoadSound(soundName),false);
}
public void PlayHitSound(string soundName)
{
PlaySound(normalAudioSource, 1.0f, LoadSound(soundName), false);
}
public void PlaySound(AudioSource audioSource, float volume, AudioClip clip , bool loop = false)
{
audioSource.clip = clip;
audioSource.volume = 0.5f;
audioSource.loop = loop;
audioSource.Play();
}
public AudioClip LoadSound(string soundName)
{
return Resources.Load<AudioClip>(Sound_Prefix + soundName);
}
}
然后我们可以把所有游戏物体上的Audio Source组件去掉,节约一定的性能。