マネーポイントを画面に表示する(マネーポイントを消費する仕組みの実装)


マネーポイントを管理する
using TMPro;
using UnityEngine;
public class PointManager : MonoBehaviour
{
public TextMeshProUGUI redPointLabel;
public TextMeshProUGUI bluePointLabel;
public int redPoint { get; set; }
public int bluePoint { get; set; }
void Start()
{
redPoint = 5000;
bluePoint = 5000;
redPointLabel.text = "" + redPoint.ToString("D4");
bluePointLabel.text = "" + bluePoint.ToString("D4");
}
public void ReduceRedPoint(int amount)
{
redPoint -= amount;
redPointLabel.text = "" + redPoint.ToString("D4");
}
public void ReduceBluePoint(int amount)
{
bluePoint -= amount;
bluePointLabel.text = "" + bluePoint.ToString("D4");
}
}


マネーポイントを消費する仕組みの実装
using System.Collections;
using UnityEngine;
public class BotSpawn : MonoBehaviour
{
// ★追加
// インスペクター上でチームを選択できるようにする
public enum TeamType { Blue, Red };
public TeamType team;
public PointManager pm;
public SpawnDatabase database;
public GameObject botPrefab;
public GameObject spawnPoint;
void Start()
{
StartCoroutine(nameof(SpawnRoutine));
}
private IEnumerator SpawnRoutine()
{
yield return new WaitForSeconds(2f);
for (int i = 0; i < database.spawnList.Count; i++)
{
for (int j = 0; j < database.spawnList[i].repeatCount; j++)
{
int speed = database.spawnList[i].moveSpeed;
int HP = database.spawnList[i].HP;
// ★追加
int botPrice = speed * HP * 10; // Bot1体の価格
if (team == TeamType.Blue)
{
// 青チームのマネーポイントをBotの価格分だけ減らす
pm.ReduceBluePoint(botPrice);
}
else
{
pm.ReduceRedPoint(botPrice);
}
GameObject bot = Instantiate(botPrefab, spawnPoint.transform.position, transform.rotation);
bot.GetComponent<BotMove>().moveSpeed = speed;
bot.GetComponent<BotHP>().HP = HP;
yield return new WaitForSeconds(1f);
}
yield return new WaitForSeconds(2f);
}
}
}



【Unity6版】TowerDX(全 回)
他のコースを見る

マネーポイントを管理する
using TMPro;
using UnityEngine;
public class PointManager : MonoBehaviour
{
public TextMeshProUGUI redPointLabel;
public TextMeshProUGUI bluePointLabel;
public int redPoint { get; set; }
public int bluePoint { get; set; }
void Start()
{
redPoint = 5000;
bluePoint = 5000;
redPointLabel.text = "" + redPoint.ToString("D4");
bluePointLabel.text = "" + bluePoint.ToString("D4");
}
public void ReduceRedPoint(int amount)
{
redPoint -= amount;
redPointLabel.text = "" + redPoint.ToString("D4");
}
public void ReduceBluePoint(int amount)
{
bluePoint -= amount;
bluePointLabel.text = "" + bluePoint.ToString("D4");
}
}


マネーポイントを消費する仕組みの実装
using System.Collections;
using UnityEngine;
public class BotSpawn : MonoBehaviour
{
// ★追加
// インスペクター上でチームを選択できるようにする
public enum TeamType { Blue, Red };
public TeamType team;
public PointManager pm;
public SpawnDatabase database;
public GameObject botPrefab;
public GameObject spawnPoint;
void Start()
{
StartCoroutine(nameof(SpawnRoutine));
}
private IEnumerator SpawnRoutine()
{
yield return new WaitForSeconds(2f);
for (int i = 0; i < database.spawnList.Count; i++)
{
for (int j = 0; j < database.spawnList[i].repeatCount; j++)
{
int speed = database.spawnList[i].moveSpeed;
int HP = database.spawnList[i].HP;
// ★追加
int botPrice = speed * HP * 10; // Bot1体の価格
if (team == TeamType.Blue)
{
// 青チームのマネーポイントをBotの価格分だけ減らす
pm.ReduceBluePoint(botPrice);
}
else
{
pm.ReduceRedPoint(botPrice);
}
GameObject bot = Instantiate(botPrefab, spawnPoint.transform.position, transform.rotation);
bot.GetComponent<BotMove>().moveSpeed = speed;
bot.GetComponent<BotHP>().HP = HP;
yield return new WaitForSeconds(1f);
}
yield return new WaitForSeconds(2f);
}
}
}



マネーポイントを画面に表示する(マネーポイントを消費する仕組みの実装)