ショップ・システムの作成①(ゲームモードとショップモードを切り替える)





モードの切り替え
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShopMode : MonoBehaviour
{
public Canvas mainCanvas;
public Canvas shopCanvas;
private bool isShop = false;
private void Start()
{
mainCanvas.enabled = true;
shopCanvas.enabled = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Q) && isShop == false) // ボタンは自由に変更可能
{
mainCanvas.enabled = false; // メインキャンバスをオフにする。
shopCanvas.enabled = true; // ショップキャンバスをオンにする。
Time.timeScale = 0; // MyBotや敵Bot等の動きを止める(ポイント)
isShop = true;
}
else if(Input.GetKeyDown(KeyCode.Q) && isShop == true)
{
mainCanvas.enabled = true;
shopCanvas.enabled = false;
Time.timeScale = 1;
isShop = false;
}
}
}



モードの切替2
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
for (int i = 0; i < botCountLabels.Length; i++)
{
botCountLabels[i].text = "" + botCounts[i];
}
}
void Update()
{
// ★追加
// ShopMode中は行動できないようにする。
if (Time.timeScale != 1)
{
return;
}
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;
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;
public class ShopMode : MonoBehaviour
{
public Canvas mainCanvas;
public Canvas shopCanvas;
private bool isShop = false;
private void Start()
{
mainCanvas.enabled = true;
shopCanvas.enabled = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Q) && isShop == false) // ボタンは自由に変更可能
{
mainCanvas.enabled = false; // メインキャンバスをオフにする。
shopCanvas.enabled = true; // ショップキャンバスをオンにする。
Time.timeScale = 0; // MyBotや敵Bot等の動きを止める(ポイント)
isShop = true;
}
else if(Input.GetKeyDown(KeyCode.Q) && isShop == true)
{
mainCanvas.enabled = true;
shopCanvas.enabled = false;
Time.timeScale = 1;
isShop = false;
}
}
}



モードの切替2
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
for (int i = 0; i < botCountLabels.Length; i++)
{
botCountLabels[i].text = "" + botCounts[i];
}
}
void Update()
{
// ★追加
// ShopMode中は行動できないようにする。
if (Time.timeScale != 1)
{
return;
}
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;
botCountLabels[num].text = "" + botCounts[num];
}
else
{
AudioSource.PlayClipAtPoint(NGSound, Camera.main.transform.position);
}
}
}
}
}

ショップ・システムの作成①(ゲームモードとショップモードを切り替える)