Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.5 kB
26
Indexable
Never
public class OutfitChanger : MonoBehaviour
{
    [Header("Sprite To Change")]
    public SpriteRenderer playerSkin;

    [Header("Sprites to Cycle Through")]
    public List<Sprite> options = new List<Sprite>();

    private int currentOption = 0;

    // Start is called before the first frame update
    void Start()
    {
        if (!PlayerPrefs.HasKey("currentOption"))
        {
            currentOption = 0;
        }

        else
        {
            Load();
        }

        playerSkin.sprite = options[currentOption];
    }

    public void NextOption()
    {
        currentOption++;
        if(currentOption >= options.Count)
        {
            currentOption = 0; // Reset if cycled through entire list
        }

        playerSkin.sprite = options[currentOption];
        Save();
    }

    public void BackOption()
    {
        currentOption--;
        if(currentOption < 0)
        {
            currentOption = options.Count - 1; // Reset if cycled through entire list
        }

        playerSkin.sprite = options[currentOption];
        Save();
    }

    public void Randomize()
    {
        currentOption = Random.Range(0, options.Count - 1);
        playerSkin.sprite = options[currentOption];
    }

    private void Load()
    {
        currentOption = PlayerPrefs.GetInt("currentOption");
    }

    private void Save()
    {
        PlayerPrefs.SetInt("currentOption", currentOption);
    }
}