Advertisement
evelynshilosky

SaveSlot - Part 25

Nov 15th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7.  
  8. public class SaveSlot : MonoBehaviour
  9. {
  10.     private Button button;
  11.     private TextMeshProUGUI buttonText;
  12.    
  13.     public int slotNumber;
  14.  
  15.     public GameObject alertUI;
  16.     Button yesBTN;
  17.     Button noBTN;
  18.  
  19.  
  20.     private void Awake()
  21.     {
  22.         button = GetComponent<Button>();
  23.         buttonText = transform.Find("Text (TMP)").GetComponent<TextMeshProUGUI>();
  24.  
  25.         yesBTN = alertUI.transform.Find("YesButton").GetComponent<Button>();
  26.         noBTN = alertUI.transform.Find("NoButton").GetComponent<Button>();
  27.     }
  28.  
  29.     public void Start()
  30.     {
  31.         button.onClick.AddListener(() =>
  32.         {
  33.             if (SaveManager.Instance.IsSlotEmpty(slotNumber))
  34.             {
  35.                 SaveGameConfirmed();
  36.             }
  37.             else
  38.             {
  39.                 DisplayOverrideWarning();
  40.             }
  41.         }
  42.         );
  43.     }
  44.  
  45.     private void Update()
  46.     {
  47.         if (SaveManager.Instance.IsSlotEmpty(slotNumber))
  48.         {
  49.             buttonText.text = "Empty";
  50.         }
  51.         else
  52.         {
  53.             buttonText.text = PlayerPrefs.GetString("Slot" + slotNumber + "Description");
  54.         }
  55.     }
  56.      
  57.      public void DisplayOverrideWarning()
  58.     {
  59.         alertUI.SetActive(true);
  60.  
  61.         yesBTN.onClick.AddListener(() => {
  62.             SaveGameConfirmed();
  63.             alertUI.SetActive(false);
  64.         });
  65.  
  66.         noBTN.onClick.AddListener(() => {
  67.             alertUI.SetActive(false);
  68.         });
  69.     }
  70.  
  71.     private void SaveGameConfirmed()
  72.     {
  73.         SaveManager.Instance.SaveGame(slotNumber);
  74.  
  75.         DateTime dt = DateTime.Now;
  76.         string time = dt.ToString("yyyy-MM-dd HH:mm");
  77.  
  78.         string description = "Saved Game " + slotNumber + " | " + time;
  79.  
  80.         buttonText.text = description;
  81.  
  82.         PlayerPrefs.SetString("Slot" + slotNumber + "Description", description);
  83.  
  84.         SaveManager.Instance.DeselectButton();
  85.     }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement