using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public static class SaveSystem
{
private static int countName;
public static string Load(string FileName)
{
string content = File.ReadAllText(FileName););
return content;
}
private static string ScreenShotName(int width, int height)
{
return string.Format("{0}/Saved Games/SaveSlot{1} SavedGameSlot_{2}x{3}_{4}/SavedGameSlot_{1}x{2}_{3}.png",
Application.persistentDataPath,
countName,
width, height, System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
}
public static void Save(string saveString, int resWidth, int resHeight)
{
string filename = ScreenShotName(resWidth, resHeight);
string directory = Path.GetDirectoryName(filename);
Directory.CreateDirectory(directory);
ScreenCapture.CaptureScreenshot(filename);
string fileName = Path.Combine(directory, "savegame.txt");
File.WriteAllText(fileName, saveString);
countName ;
}
}
The problem is when there are no saved game directories at all if Saved Games directory is empty then i want the variable countName value to be 0 and then each time when saving the game to increase the directories names numbers : SaveSlot1, SaveSlot2, SaveSlot3....
The problem is when i'm running the game and then if there already saved games directories like SaveSlot1, SaveSlot2, SaveSlot3 i want that next time it will save the next directory will be SaveSlot4 but now what it does is that each time i'm running the game it's creating SaveSlot0 directory because each time countName is 0.
This is example of the problem screenshot of the directories :
The last one is SaveSlot1 because i saved the game twice in a row but then each time i'm running the game over again the variable countName does not know to start counting from the last created directory.
If the last one for example start with SaveSlot5 then next time i'm running the game and saving the new directory should be starting with SaveSlot6 and not SaveSlot0
And if the directory Saved Games is empty then make countName to start from 0.
Update :
I tried this way : But where do i call the LoadNumber method ?
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public static class SaveSystem
{
private static int countName;
public static string Load(string FileName)
{
string content = File.ReadAllText(FileName);
return content;
}
private static string ScreenShotName(int width, int height)
{
return string.Format("{0}/Saved Games/SaveSlot{1} SavedGameSlot_{2}x{3}_{4}/SavedGameSlot_{1}x{2}_{3}.png",
Application.persistentDataPath,
countName,
width, height, System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
}
public static void Save(string saveString, int resWidth, int resHeight)
{
string filename = ScreenShotName(resWidth, resHeight);
string directory = Path.GetDirectoryName(filename);
Directory.CreateDirectory(directory);
ScreenCapture.CaptureScreenshot(filename);
string fileName = Path.Combine(directory, "savegame.txt");
File.WriteAllText(fileName, saveString);
countName ;
SaveNumber();
}
public static void SaveNumber()
{
PlayerPrefs.SetInt("countName", countName);
}
public static void LoadNumber()
{
countName = PlayerPrefs.GetInt("countName");
}
}
CodePudding user response:
Store countName in PlayerPrefs
instead of just a variable. Saving count
in a variable just for something in a session but not life-time (PlayerPrefs
is not life-time too but mostly)