タワーデフェンスゲーム　Unity タレット設置機能 14/14　49 WaveUIの実装

# 概要
目次

https://qiita.com/RYA234/private/c3d52a0f4ab3258a494a


今回はゲーム画面右上のWaveUIの実装を行います。
下図は現Waveの敵を全滅させて次のWaveに進んでいる様子です。


# 開発環境

IDE:Rider
Unity:2020.3.42(LTS)
OS:Windows10

# UnityEditor上の設定
## Canvas以下にWavePanelを新規作成します。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/517a865f-5bd5-3ece-492c-45ccb73f1d23.png)


## UIManagerにCurrentWave-TMPをアタッチします。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/f1cedcec-a1f6-20f4-ed61-587190cb5466.png)


# 実装のポイント
下図はWaveを更新するときの流れです。
オブサーバーパターンを構成しており、ObservaleがSpawnerクラス　ObserverがLevelManagerです。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/0e0e1c32-c136-8173-41bb-5ccdda735f9f.png)


下図はWaveのUIを更新する流れです。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/85bccd11-15f3-dd27-2fd9-5538b84bf5e3.png)


# コード部分

## LevelManager

```diff_c_sharp:LevelManager.cs
using System;
using UnityEngine;


public class LevelManager : Singleton<LevelManager>
{
    [SerializeField] private int lives = 10;

    public int TotalLives { get; set; }
+    public int CurrentWave { get; set; }
    
    private void Start()
    {
        TotalLives = lives;
+        CurrentWave = 1;
        
    }

    private void ReduceLives(Enemy enemy)
    {
        TotalLives--;
        if (TotalLives <= 0)
        {
            TotalLives = 0;
        }
    }

+    private void WaveCompleted()
+    {
+        CurrentWave++;
+    }
    private void OnEnable()
    {
        Enemy.OnEndReached += ReduceLives;
+        Spawner.OnWaveCompleted += WaveCompleted;
        
    }

    private void OnDisable()
    {
        Enemy.OnEndReached -= ReduceLives;  
+        Spawner.OnWaveCompleted -= WaveCompleted;
    }
}


```

## UIManager

```diff_c_sharp:UIManager.cs
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class UIManager : Singleton<UIManager>
{
    [Header("Panels")]
    [SerializeField] private GameObject turretShopPanel;

    [SerializeField] private GameObject nodeUIPanel;
    [SerializeField] private TextMeshProUGUI upgradeText;
    [SerializeField] private TextMeshProUGUI sellText;
    [SerializeField] private TextMeshProUGUI turretLevelText;
    [SerializeField] private TextMeshProUGUI totalCoinsText;
    [SerializeField] private TextMeshProUGUI lifesText;
+    [SerializeField] private TextMeshProUGUI currentWaveText;
    
    public TurretUpgrade TurretUpgrade { get; set; }
    private Node _currentNodeSelected;

    private void Update()
    {
        totalCoinsText.text = CurrencySystem.Instance.TotalCoins.ToString();
        lifesText.text = LevelManager.Instance.TotalLives.ToString();
+        currentWaveText.text = $"Wave {LevelManager.Instance.CurrentWave.ToString()}";

    }

 // 中略
}


```


## Spawner

```diff_c_sharp:Spawner.cs
using System;
using System.Collections;
using UnityEngine;


    public class Spawner : MonoBehaviour
    {
+        public static Action OnWaveCompleted;
        
        [SerializeField]  private  int enemyCount = 10;
        // Btw = Between
        [SerializeField] private float delayBtwSpawns;
        [SerializeField] private float delayBtwWaves = 1f;

        private int _enemiesRemaining;
        
        private int _enemiesSpawned;
        private float _spawnTimer;
        private ObjectPooler _objectPooler;
        private Waypoint _waypoint;

   // 中略
     
        private void RecordEnemy(Enemy enemy)
        {
            _enemiesRemaining--;
            if (_enemiesRemaining <= 0)
            {
+                OnWaveCompleted?.Invoke();
                StartCoroutine(NextWave());
            }
        }

   // 中略
    }


```






# 参考

Section8 49

https://www.udemy.com/course/learn-how-to-create-a-2d-tower-defense-game-in-unity-2020/learn/lecture/23174280#content


github コミット分

https://github.com/RYA234/2DTowerDeffenceMyLearning/commit/ed835c1ad5aef8d2c9d27ff22b896a776203623d#diff-551fbd16a1030b416b24e57d93084acfc07594060f1fc316f909fd3c64c68a01