Answer the question
In order to leave comments, you need to log in
Give an idea how to simplify the shop code?
There is a code, not very good, but completely mine in which I understand everything.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Shop : MonoBehaviour
{
public static string BuyZero = "true";
public static string BuyOne = "false";
public static string BuyTwo = "false";
/*коротко про то почему string а не bollean:
Мне нужно сохранять весь прогресс в playerPrefs, а метода setBoll там нету.*/
public static int nomUse = 0;
public Image imZero;
public Image imOne;
public Image imTwo;
public Sprite buySprite;
public Sprite soldSprite;
public Sprite usedSprite;
public Button buttonZero;
public Button buttonOne;
public Button buttonTwo;
public GameObject skinZero;
public GameObject skinOne;
public GameObject skinTwo;
public Text enoughMoney;
private Color color;
private void Start()
{
imZero = buttonZero.GetComponent<Image>();
imOne = buttonOne.GetComponent<Image>();
imTwo = buttonTwo.GetComponent<Image>();
}
private void SetBuyAnaUse(int use)
{
switch (use)
{
case 0:
imZero.sprite = usedSprite;
if (BuyOne == "true")
{
imOne.sprite = soldSprite;
}
else
{
imOne.sprite = buySprite;
}
if (BuyTwo == "true")
{
imTwo.sprite = soldSprite;
}
else
{
imTwo.sprite = buySprite;
}
break;
case 1:
imOne.sprite = usedSprite;
if (BuyZero == "true")
{
imZero.sprite = soldSprite;
}
else
{
imZero.sprite = buySprite;
}
if (BuyTwo == "true")
{
imTwo.sprite = soldSprite;
}
else
{
imOne.sprite = buySprite;
}
break;
case 2:
imTwo.sprite = usedSprite;
if (BuyOne == "true")
{
imOne.sprite = soldSprite;
}
else
{
imOne.sprite = buySprite;
}
if (BuyZero == "true")
{
imZero.sprite = soldSprite;
}
else
{
imZero.sprite = buySprite;
}
break;
}
nomUse = use;
switch (nomUse)
{
case 0:
skinZero.SetActive(true);
skinOne.SetActive(false);
skinTwo.SetActive(false);
break;
case 1:
skinZero.SetActive(false);
skinOne.SetActive(true);
skinTwo.SetActive(false);
break;
case 2:
skinZero.SetActive(false);
skinOne.SetActive(false);
skinTwo.SetActive(true);
break;
PlayerController.SaveProgress();
}
}
public void BuyZeroF()
{
SetBuyAnaUse(0);
imZero.sprite = usedSprite;
}
public void BuyOnesF()
{
if (BuyOne == "true")
{
SetBuyAnaUse(1);
imOne.sprite = usedSprite;
}
else
{
if (PlayerController.coinTake >= 50)
{
BuyOne = "true";
SetBuyAnaUse(1);
PlayerController.coinTake -= 20;
PlayerController.coinText.text = "Ваши денежки: " + (PlayerController.coinTake);
return;
}
if (PlayerController.coinTake < 20)
{
enoughMoney.text = "Вам не хватает " + (20 - PlayerController.coinTake) + " монет!";
StartCoroutine(DelayAndNull());
return;
}
}
}
public void BuyTwoF()
{
if (BuyTwo == "true")
{
SetBuyAnaUse(2);
imTwo.sprite = usedSprite;
}
else
{
if (PlayerController.coinTake >= 100)
{
BuyTwo = "true";
SetBuyAnaUse(2);
PlayerController.coinTake -= 50;
PlayerController.coinText.text = "Ваши денежки: " + (PlayerController.coinTake);
return;
}
if (PlayerController.coinTake < 50)
{
enoughMoney.text = "Вам не хватает " + (50 - PlayerController.coinTake) + " монет!";
StartCoroutine(DelayAndNull());
return;
}
}
}
/// <summary>
/// Исчезновение текста
/// </summary>
/// <returns></returns>
IEnumerator DelayAndNull()
{
yield return new WaitForSeconds(0.5f);
enoughMoney.text = string.Empty;
}
}
Answer the question
In order to leave comments, you need to log in
Learn data structures and arrays.
What is this footcloth about?
case 0:
skinZero.SetActive(true);
skinOne.SetActive(false);
skinTwo.SetActive(false);
break;
case 1:
skinZero.SetActive(false);
skinOne.SetActive(true);
skinTwo.SetActive(false);
break;
case 2:
skinZero.SetActive(false);
skinOne.SetActive(false);
skinTwo.SetActive(true);
break;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question