My save and load function
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System;
using UnityEngine.UI;
public class SaveLoadManager : MonoBehaviour
{
[SerializeField] TMP_InputField inputField;
[SerializeField] Transform saveButton;
string inputText;
private void Awake()
{
if (PlayerPrefs.HasKey("SelectedSave"))
{
string saveName = PlayerPrefs.GetString("SelectedSave");
ES3AutoSaveMgr.Current.settings.path = saveName;
ES3AutoSaveMgr.Current.Load();
}
saveButton.GetComponent<Button>().onClick.AddListener(() =>
{
updateInputText(inputField.text + ".es3");
});
}
void Update()
{
}
void updateInputText(string inputText)
{
this.inputText = inputText;
Debug.Log(this.inputText);
saveGame();
}
void saveGame()
{
ES3AutoSaveMgr.Current.settings.path = this.inputText.ToString();
ES3AutoSaveMgr.Current.Save();
}
}
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System;
using System.Linq;
using UnityEngine.SceneManagement;
public class LoadSystem : MonoBehaviour
{
[SerializeField] Transform loadButtonTemplate;
float offsetValue = -70;
float xValue = 120.0f;
System.DateTime newestDateTime = new System.DateTime(0);
private void Awake()
{
PlayerPrefs.DeleteKey("SelectedSave");
var saveFiles = ES3.GetFiles().OrderByDescending(f => ES3.GetTimestamp(f)).ToArray();
loadButtonTemplate.gameObject.SetActive(false);
int index = 0;
for (int i = saveFiles.Length - 1; i >= 0; i--)
{
var filename = saveFiles[i];
Transform button = Instantiate(loadButtonTemplate, transform);
button.gameObject.SetActive(true);
button.GetComponent<RectTransform>().anchoredPosition = new Vector2(xValue, offsetValue * index);
index++;
string fileName = System.IO.Path.GetFileNameWithoutExtension(filename);
button.Find("fileName").GetComponent<TextMeshProUGUI>().text = fileName;
button.GetComponent<Button>().onClick.AddListener(() =>
{
//string selectedFilename = filename;
string selectedFilename = button.Find("fileName").GetComponent<TextMeshProUGUI>().text.ToString();
PlayerPrefs.SetString("SelectedSave", selectedFilename);
GameSceneManager.load(GameSceneManager.Scene.gameScene);
});
}
}
}