Survival Shooter tutorial(二)—— 遊戲管理器
阿新 • • 發佈:2019-02-01
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);
}
}