H
H
Herman Coffman2020-06-29 21:02:27
C++ / C#
Herman Coffman, 2020-06-29 21:02:27

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;
    }
}


This is a code for a store of 3 characters, and I must say right away that I have no ideas at all how to make it convenient to add new characters.
Give an idea how to make it all more convenient.

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mikhanoid, 2020-06-29
@mikhanoid

Make a collection of characters: array, list, hash, ..?

F
freeExec, 2020-06-29
@freeExec

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;

Enable selected - disable others. Namely, we sort through all the elements, if it is the current one, then turn it on, otherwise turn it off. That's it, there are two lines of code.
And everything else is in the same vein.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question