MyBotのセット⑥(残機数をUIで表示する)
残機数の表示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using TMPro;
public class SetBot : MonoBehaviour
{
private GameObject target;
public GameObject[] myBotsPrefab;
private int num = 0;
public AudioClip selectSound;
public AudioClip NGSound;
public Material NGMark;
public GameObject[] botIcons;
private int[] botCounts = new int[6];
// ★追加
public TextMeshProUGUI[] botCountLabels;
private void Start()
{
for (int i = 0; i < botIcons.Length; i++)
{
if (i == num)
{
botIcons[i].SetActive(true);
}
else
{
botIcons[i].SetActive(false);
}
}
botCounts[0] = 2; // MyBot_A
botCounts[1] = 2; // MyBot_B
botCounts[2] = 2; // MyBot_C
botCounts[3] = 2; // MyBot_D
botCounts[4] = 2; // MyBot_E
botCounts[5] = 2; // MyBot_F
// ★追加
// ゲームスタート時の各MyBotの残機数を表示する
for (int i = 0; i < botCountLabels.Length; i++)
{
botCountLabels[i].text = "" + botCounts[i];
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse1))
{
num = (num + 1) % myBotsPrefab.Length;
AudioSource.PlayClipAtPoint(selectSound, Camera.main.transform.position);
for (int i = 0; i < botIcons.Length; i++)
{
if (i == num)
{
botIcons[i].SetActive(true);
}
else
{
botIcons[i].SetActive(false);
}
}
}
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
target = hit.collider.gameObject;
if (Input.GetKeyDown(KeyCode.Mouse0))
{
if (target.tag == "Block")
{
if (botCounts[num] < 1)
{
AudioSource.PlayClipAtPoint(NGSound, Camera.main.transform.position);
return;
}
GameObject bot = Instantiate(myBotsPrefab[num], target.transform.position, Quaternion.identity);
target.tag = "NG";
target.GetComponent<MeshRenderer>().material = NGMark;
botCounts[num] -= 1;
// ★追加
// セットしたMyBotの残機数の表示を減らす。
botCountLabels[num].text = "" + botCounts[num];
}
else
{
AudioSource.PlayClipAtPoint(NGSound, Camera.main.transform.position);
}
}
}
}
}
【2021版】TowerD Ⅱ(全17回)
他のコースを見る残機数の表示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using TMPro;
public class SetBot : MonoBehaviour
{
private GameObject target;
public GameObject[] myBotsPrefab;
private int num = 0;
public AudioClip selectSound;
public AudioClip NGSound;
public Material NGMark;
public GameObject[] botIcons;
private int[] botCounts = new int[6];
// ★追加
public TextMeshProUGUI[] botCountLabels;
private void Start()
{
for (int i = 0; i < botIcons.Length; i++)
{
if (i == num)
{
botIcons[i].SetActive(true);
}
else
{
botIcons[i].SetActive(false);
}
}
botCounts[0] = 2; // MyBot_A
botCounts[1] = 2; // MyBot_B
botCounts[2] = 2; // MyBot_C
botCounts[3] = 2; // MyBot_D
botCounts[4] = 2; // MyBot_E
botCounts[5] = 2; // MyBot_F
// ★追加
// ゲームスタート時の各MyBotの残機数を表示する
for (int i = 0; i < botCountLabels.Length; i++)
{
botCountLabels[i].text = "" + botCounts[i];
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse1))
{
num = (num + 1) % myBotsPrefab.Length;
AudioSource.PlayClipAtPoint(selectSound, Camera.main.transform.position);
for (int i = 0; i < botIcons.Length; i++)
{
if (i == num)
{
botIcons[i].SetActive(true);
}
else
{
botIcons[i].SetActive(false);
}
}
}
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
target = hit.collider.gameObject;
if (Input.GetKeyDown(KeyCode.Mouse0))
{
if (target.tag == "Block")
{
if (botCounts[num] < 1)
{
AudioSource.PlayClipAtPoint(NGSound, Camera.main.transform.position);
return;
}
GameObject bot = Instantiate(myBotsPrefab[num], target.transform.position, Quaternion.identity);
target.tag = "NG";
target.GetComponent<MeshRenderer>().material = NGMark;
botCounts[num] -= 1;
// ★追加
// セットしたMyBotの残機数の表示を減らす。
botCountLabels[num].text = "" + botCounts[num];
}
else
{
AudioSource.PlayClipAtPoint(NGSound, Camera.main.transform.position);
}
}
}
}
}
MyBotのセット⑥(残機数をUIで表示する)