タワーデフェンスゲーム　Unity タレット設置機能  6/14   45 タレットUIのCancelボタンを実装する

# 概要
目次

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


今回はタレットUIのキャンセルを実装します
以下gifアニメはキャンセルボタンを押してタレットUIを非表示にしている様子です。

![6-14.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/de6427e4-5636-519f-4997-0dcbe81e201f.gif)





# 開発環境

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

# UnityEditor上の設定
キャンセルボタンを押した時にUIManager.CloseTurretShopPanelを実行するようにします。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/073f8663-0c23-35ff-cf2f-9d581eb53303.png)


# 実装のポイント
UIManagerクラスにタレットUIを閉じるメソッドCloseTurretShopPanelを追加します。

TurretCardクラスのPlaceTurretメソッド内にUIManager.CloseTurretShopPanelを加えます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/391605ed-8ac1-c122-ceef-6debce5b6eb3.png)


# コード部分

## 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;
    
    private Node _currentNodeSelected;

    // 追加箇所
+    public void CloseTurretShopPanel()
+    {
+        turretShopPanel.SetActive(false);
+    }
    
    private void ShowNodeUI()
    {
        nodeUIPanel.SetActive(true);
    }
    private void NodeSelected(Node nodeSelected)
    {
        _currentNodeSelected = nodeSelected;
        if (_currentNodeSelected.IsEmpty())
        {
            turretShopPanel.SetActive(true);
        }
        else
        {
            ShowNodeUI();
        }
    }
    private void OnEnable()
    {
        Node.OnNodeSelected += NodeSelected;
    }

    private void OnDisable()
    {
        Node.OnNodeSelected -= NodeSelected;
    }
}


```

## TurretCard

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

public class TurretCard : MonoBehaviour
{
    public static Action<TurretSetting> OnPlaceTurret;
    [SerializeField] private Image turretImage;
    [SerializeField] private TextMeshProUGUI turretCost;

    public TurretSetting TurretLoaded { get; set; }
    
    public void SetupTurretButton(TurretSetting turretSetting)
    {
        TurretLoaded = turretSetting;
        turretImage.sprite = turretSetting.TurretShopSprite;
        turretCost.text = turretSetting.TurretShopCost.ToString();
        
    }

    public void PlaceTurret()
    {
        if (CurrencySystem.Instance.TotalCoins >= TurretLoaded.TurretShopCost)
        {
            CurrencySystem.Instance.RemoveCoins(TurretLoaded.TurretShopCost);
+            UIManager.Instance.CloseTurretShopPanel();
            OnPlaceTurret?.Invoke(TurretLoaded);
         
        }
    }
    
}


```



# 参考




Section8 45

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


github コミット分

https://github.com/RYA234/2DTowerDeffenceMyLearning/commit/71f12a26009ec6b8fe0a4571a7de79804f0943a5