Как сделать выбор персонажа?
У меня есть сцена в которой можно выбрать персонажа, открываются они за рекорды. К примеру у тебя рекорд времени в игре 25 секунд и тогда откроется персонаж который открывается при 25 секундах. Пока персонаж не открыт, он имеет черный цвет (типа он еще не открыт) Происходит это так
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class IsEnabled : MonoBehaviour
{
public int NeedToUnlock;
public Color NewColor;
SpriteRenderer SpriteR;
public Sprite Pink_Monster;
public SpriteRenderer PinkMonster;
void Start()
{
if (PlayerPrefs.GetInt("SaveScore") < NeedToUnlock)
{
GetComponent<SpriteRenderer>().color = Color.black;
}
}
В игре это выглядит так
Мне нужно чтобы при нажатии на кнопку "Выбрать" и тогда в игре будет персонаж которого ты выбрал. К примеру я выбрал белого персонажа и в игре он появился (игра находиться на другой сцене). Объясните как это сделать?
Ответы (1 шт):
Для начала сделаем скрипт покупки.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShopCar : MonoBehaviour
{
private int numberPlayer; //Здесь хранится номер выбранного персонажа
// Start is called before the first frame update
void Start()
{
numberPlayer = PlayerPrefs.GetInt("Player"); //Закладываем значение выбранного персонажа в PlayerPrefs
}
public void PlayerOwl() //каждый персонаж это отдельная функция чтобы можно было потом назначить их на кнопки
{
numberPlayer = 1; // Закладываем в переменную номер игрока
PlayerPrefs.SetInt("Player", numberPlayer); // Закладываем номер выбранного героя в PlayerPrefs, беря информацию из NumberPlayer
}
public void PlayerPink()
{
numberPlayer = 2;
PlayerPrefs.SetInt("Player", numberPlayer);
}
public void PlayerDude()
{
numberPlayer = 3;
PlayerPrefs.SetInt("Player", numberPlayer);
}
public void PlayerTiny()
{
numberPlayer = 4;
PlayerPrefs.SetInt("Player", numberPlayer);
}
public void PlayerRed()
{
numberPlayer = 5;
PlayerPrefs.SetInt("Player", numberPlayer);
}
public void PlayerYellow()
{
numberPlayer = 6;
PlayerPrefs.SetInt("Player", numberPlayer);
}
}
Потом делаем скрипт который будет находиться уже в самой игре (другая сцена)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NumberCar : MonoBehaviour
{
private int numberPlayer;
public GameObject Player1; // в каждую переменную ложим обьект который будет включаться при выборе в другом скрипте
public GameObject Player2;
public GameObject Player3;
public GameObject Player4;
public GameObject Player5;
public GameObject Player6;
// Start is called before the first frame update
void Start()
{
numberPlayer = PlayerPrefs.GetInt("Player"); // получаем значение заложенное в PlayerPrefs, в приведущем скрипте
}
// Update is called once per frame
void Update()
{
if(numberPlayer == 1) //если значение в PlayerPrefs равно 1 (тоесть номер выбранного игрока 1) то включаем нужного игрока и отключаем остальных (на сцене уже стоят все игроки мы просто оставляем нужного
{
Player1.SetActive(true);
Player2.SetActive(false);
Player3.SetActive(false);
Player4.SetActive(false);
Player5.SetActive(false);
Player6.SetActive(false);
}
if (numberPlayer == 2)
{
Player2.SetActive(true);
Player1.SetActive(false);
Player3.SetActive(false);
Player4.SetActive(false);
Player5.SetActive(false);
Player6.SetActive(false);
}
if (numberPlayer == 3)
{
Player3.SetActive(true);
Player1.SetActive(false);
Player2.SetActive(false);
Player4.SetActive(false);
Player5.SetActive(false);
Player6.SetActive(false);
}
if (numberPlayer == 4)
{
Player4.SetActive(true);
Player1.SetActive(false);
Player2.SetActive(false);
Player3.SetActive(false);
Player5.SetActive(false);
Player6.SetActive(false);
}
if (numberPlayer == 5)
{
Player5.SetActive(true);
Player1.SetActive(false);
Player2.SetActive(false);
Player3.SetActive(false);
Player4.SetActive(false);
Player6.SetActive(false);
}
if (numberPlayer == 6)
{
Player6.SetActive(true);
Player1.SetActive(false);
Player2.SetActive(false);
Player3.SetActive(false);
Player4.SetActive(false);
Player5.SetActive(false);
}
}
}
Думаю можно было бы сделать проще, но меня устроил и такой вариант

