Untitled

mail@pastecode.io avatar
unknown
csharp
15 days ago
2.5 kB
7
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class DateTracker : MonoBehaviour
{
    public GameObject countdownRoomPrefab;
    public GameObject countdownRoomPrefabParent;

    public GameObject settingsPanel;

    public TMP_InputField roomName;
    public GameObject roomMonth;
    public GameObject roomDay;

    private int selectedButton;

    public void SpawnNewButton()
    {
        GameObject g = Instantiate(countdownRoomPrefab, Vector2.zero, Quaternion.identity, countdownRoomPrefabParent.GetComponent<RectTransform>());
        g.name = "CountdownRoom" + GameObject.FindGameObjectsWithTag("CountdownRoom").Length;
        g.transform.GetChild(2).GetComponent<Button>().onClick.AddListener(() => EditButton(g));
        selectedButton = GameObject.FindGameObjectsWithTag("CountdownRoom").Length;

        settingsPanel.SetActive(true);
    }

    public void EditButton(GameObject button)
    {
        selectedButton = int.Parse(Regex.Replace(button.name, "[^0-9]", ""));

        settingsPanel.SetActive(true);

        roomName.text = PlayerPrefs.GetString("name" + selectedButton.ToString(), roomName.text);
        roomMonth.GetComponent<TMP_Dropdown>().value = PlayerPrefs.GetInt("months" + selectedButton.ToString(), 1);
        roomDay.GetComponent<TMP_Dropdown>().value = PlayerPrefs.GetInt("days" + selectedButton.ToString(), 1);
    }

    public void ClosePanelAndSetValues()
    {
        settingsPanel.SetActive(false);

        GameObject.FindGameObjectsWithTag("CountdownRoom")[selectedButton - 1].gameObject.transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = roomName.text;
        GameObject.FindGameObjectsWithTag("CountdownRoom")[selectedButton - 1].gameObject.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = roomMonth.transform.GetChild(0).GetComponent<TextMeshProUGUI>().text + " " + roomDay.transform.GetChild(0).GetComponent<TextMeshProUGUI>().text;
        roomName.text = "";
        roomMonth.GetComponent<TMP_Dropdown>().value = 0;
        roomDay.GetComponent<TMP_Dropdown>().value = 0;

        PlayerPrefs.SetInt("month" + selectedButton.ToString(), roomMonth.GetComponent<TMP_Dropdown>().value);
        PlayerPrefs.SetInt("day" + selectedButton.ToString(), roomDay.GetComponent<TMP_Dropdown>().value);
        PlayerPrefs.SetString("name" + selectedButton.ToString(), roomName.text);
    }
}
Leave a Comment