Skip to content

Instantly share code, notes, and snippets.

@Dssdiego
Last active May 1, 2020 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dssdiego/452d8a3da454038acab00631842aa0f0 to your computer and use it in GitHub Desktop.
Save Dssdiego/452d8a3da454038acab00631842aa0f0 to your computer and use it in GitHub Desktop.
Simple Save System for Themes in Unity
using UnityEngine;
[CreateAssetMenu(menuName = "Settings/Theme")]
public class GameTheme : ScriptableObject
{
public Color foregroundColor;
public Color backgroundColor;
}
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment