Managers - EnemyManager
在已完成的项目中,有三个EnemyManager,spawnPoints大小都设置为1。也就是说此段代码的随机是没用的。
using UnityEngine;
namespace CompleteProject
{
public class EnemyManager : MonoBehaviour
{
public PlayerHealth playerHealth; // Reference to the player's heatlh.
public GameObject enemy; // The enemy prefab to be spawned.
public float spawnTime = 3f; // How long between each spawn.
public Transform[] spawnPoints; // An array of the spawn points this enemy can spawn from.
void Start ()
{
// Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
InvokeRepeating ("Spawn", spawnTime, spawnTime);
}
void Spawn ()
{
// If the player has no health left...
if(playerHealth.currentHealth <= 0f)
{
// ... exit the function.
return;
}
// Find a random index between zero and one less than the number of spawn points.
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
// Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
}
}
Managers - ScoreManager
此处score声明为静态变量,静态变量属于类本身,而不属于任何实例,在使用时不需要GetComponent等操作,可以直接调用。在EnemyHealth.cs中通过ScoreManager.score调用。
(当我们拖拽PlayerHealth.cs、PlayerMovement.cs等到对象时,是类生成实例)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
namespace CompleteProject
{
public class ScoreManager : MonoBehaviour
{
public static int score; // The player's score.
Text text; // Reference to the Text component.
void Awake ()
{
// Set up the reference.
text = GetComponent <Text> ();
// Reset the score.
score = 0;
}
void Update ()
{
// Set the displayed text to be the word "Score" followed by the score value.
text.text = "Score: " + score;
}
}
}
Managers - PauseManager
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Audio;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class PauseManager : MonoBehaviour {
public AudioMixerSnapshot paused;
public AudioMixerSnapshot unpaused;
Canvas canvas;
void Start()
{
canvas = GetComponent<Canvas>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
canvas.enabled = !canvas.enabled;
Pause();
}
}
public void Pause()
{
Time.timeScale = Time.timeScale == 0 ? 1 : 0;
Lowpass ();
}
void Lowpass()
{
if (Time.timeScale == 0)
{
paused.TransitionTo(.01f);
}
else
{
unpaused.TransitionTo(.01f);
}
}
public void Quit()
{
#if UNITY_EDITOR
EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
Helpers - VolumeHandler
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class VolumeHandler : MonoBehaviour {
// Use this for initialization
void Start ()
{
if(GameObject.Find("EffectsSlider"))
GameObject.Find("EffectsSlider").GetComponent<Slider>().onValueChanged.AddListener(SetVolume);
}
void SetVolume(float volume)
{
GetComponent<AudioSource>().volume = volume;
}
void OnDestroy()
{
if(GameObject.Find("EffectsSlider"))
GameObject.Find("EffectsSlider").GetComponent<Slider>().onValueChanged.RemoveListener(SetVolume);
}
}