ショップ・システムの作成①(ゲームモードとショップモードを切り替える)
![F972af4e 6a03 4f40 a969 bec37c95538c](https://codegenius.org/uploads/slide/image/7994/f972af4e-6a03-4f40-a969-bec37c95538c.jpeg)
![6df82e66 7100 41fe a283 bc17abafb1cf](https://codegenius.org/uploads/slide/image/7995/6df82e66-7100-41fe-a283-bc17abafb1cf.jpeg)
![3c5c4559 0fb6 424d 988e 5da14856b3be](https://codegenius.org/uploads/slide/image/7996/3c5c4559-0fb6-424d-988e-5da14856b3be.jpeg)
![3b2753f4 fdc0 4294 a214 7d7e39f9f74e](https://codegenius.org/uploads/slide/image/7997/3b2753f4-fdc0-4294-a214-7d7e39f9f74e.jpeg)
![3d5a6745 9fc7 4c84 a416 564247fca0ec](https://codegenius.org/uploads/slide/image/7998/3d5a6745-9fc7-4c84-a416-564247fca0ec.jpeg)
モードの切り替え
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;
}
}
}
![Cc24bd90 6f7b 4698 ad9a a97d1af2f185](https://codegenius.org/uploads/slide/image/7999/cc24bd90-6f7b-4698-ad9a-a97d1af2f185.jpeg)
![Da7cdcc3 b07d 47d1 af5d a50096f54091](https://codegenius.org/uploads/slide/image/8000/da7cdcc3-b07d-47d1-af5d-a50096f54091.jpeg)
![84af071d 8d58 4274 a027 9ab48c5d4af5](https://codegenius.org/uploads/slide/image/8001/84af071d-8d58-4274-a027-9ab48c5d4af5.jpeg)
モードの切替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);
}
}
}
}
}
![21313cdc 2f52 4fc7 a7b7 56348b3a4ea4](https://codegenius.org/uploads/slide/image/8002/21313cdc-2f52-4fc7-a7b7-56348b3a4ea4.jpeg)
【2021版】TowerD Ⅱ(全17回)
他のコースを見る![F972af4e 6a03 4f40 a969 bec37c95538c](https://codegenius.org/uploads/slide/image/7994/f972af4e-6a03-4f40-a969-bec37c95538c.jpeg)
![6df82e66 7100 41fe a283 bc17abafb1cf](https://codegenius.org/uploads/slide/image/7995/6df82e66-7100-41fe-a283-bc17abafb1cf.jpeg)
![3c5c4559 0fb6 424d 988e 5da14856b3be](https://codegenius.org/uploads/slide/image/7996/3c5c4559-0fb6-424d-988e-5da14856b3be.jpeg)
![3b2753f4 fdc0 4294 a214 7d7e39f9f74e](https://codegenius.org/uploads/slide/image/7997/3b2753f4-fdc0-4294-a214-7d7e39f9f74e.jpeg)
![3d5a6745 9fc7 4c84 a416 564247fca0ec](https://codegenius.org/uploads/slide/image/7998/3d5a6745-9fc7-4c84-a416-564247fca0ec.jpeg)
モードの切り替え
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;
}
}
}
![Cc24bd90 6f7b 4698 ad9a a97d1af2f185](https://codegenius.org/uploads/slide/image/7999/cc24bd90-6f7b-4698-ad9a-a97d1af2f185.jpeg)
![Da7cdcc3 b07d 47d1 af5d a50096f54091](https://codegenius.org/uploads/slide/image/8000/da7cdcc3-b07d-47d1-af5d-a50096f54091.jpeg)
![84af071d 8d58 4274 a027 9ab48c5d4af5](https://codegenius.org/uploads/slide/image/8001/84af071d-8d58-4274-a027-9ab48c5d4af5.jpeg)
モードの切替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);
}
}
}
}
}
![21313cdc 2f52 4fc7 a7b7 56348b3a4ea4](https://codegenius.org/uploads/slide/image/8002/21313cdc-2f52-4fc7-a7b7-56348b3a4ea4.jpeg)
ショップ・システムの作成①(ゲームモードとショップモードを切り替える)