Dssdiego
5/1/2020 - 5:47 PM

Save System

Simple Save System for Themes in Unity

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public static class SaveSystem 
{
    static string path = Application.persistentDataPath + "/theme.txt"; 
    
    public static void SaveTheme(GameTheme theme)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        
        FileStream stream = new FileStream(path, FileMode.Create);
        
        ThemeData data = new ThemeData(theme);
        
        formatter.Serialize(stream, data);
        stream.Close();
    }

    public static ThemeData LoadTheme()
    {
        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            ThemeData data = formatter.Deserialize(stream) as ThemeData;
            stream.Close();

            return data;
        }

        Debug.LogError($"Save File not found in {path}");
        return null;
    }
}
using System;

[Serializable]
public class ThemeData 
{
    public float[] foregroundColor;
    public float[] backgroundColor;

    public ThemeData(GameTheme theme)
    {
        foregroundColor = new float[4];
        foregroundColor[0] = theme.foregroundColor.r;
        foregroundColor[1] = theme.foregroundColor.g;
        foregroundColor[2] = theme.foregroundColor.b;
        foregroundColor[3] = theme.foregroundColor.a;

        backgroundColor = new float[4];
        backgroundColor[0] = theme.backgroundColor.r;
        backgroundColor[1] = theme.backgroundColor.g;
        backgroundColor[2] = theme.backgroundColor.b;
        backgroundColor[3] = theme.backgroundColor.a;
    }
}
using UnityEngine;

[CreateAssetMenu(menuName = "Settings/Theme")]
public class GameTheme : ScriptableObject
{
    public Color foregroundColor;
    public Color backgroundColor;
}