0
\$\begingroup\$

I have this save system that saves screenshots in consecutively-numbered files. I store the number of the last number used in PlayerPrefs, so that the next save continues from there.

But I'm not sure where to call the LoadNumber method to load the last used number, since this is a static class with no Start() function.

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");
    }
}
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

You can read PlayerPrefs lazily, the first time someone tries to read the current count:

static int? _count;

public int count {
    get {
        if (!_count.HasValue) {
            _count = PlayerPrefs.GetInt("countName", 0);
        }
        return _count.Value;
    }
    set {
        _count = value;
    }
}

Or you can read it eagerly as the game boots up, after all scripts in your starting scene have gotten their Awake() message, using the RuntimeInitializeOnLoadMethodAttribute:

[RuntimeInitializeOnLoadMethod]
static void LoadNumber()
{
    countName = PlayerPrefs.GetInt("countName", 0);
}
\$\endgroup\$
1
\$\begingroup\$

I think it can be simple like this,

public static int CountName
{
    get => PlayerPrefs.GetInt ("countName", defaultValue);
    set => PlayerPrefs.SetInt ("countName", value);
}

This is as simple as like changing normal variable value.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This will work, but be slightly more expensive for variables you use frequently, since it will take the slow path of looking up into PlayerPrefs with a string key every time. The version I showed keeps a local cache of the variable, so the slow path only needs to be taken for the initial load, and when you initiate a save. \$\endgroup\$
    – DMGregory
    Sep 7, 2022 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .