Last active
May 1, 2020 17:49
-
-
Save Dssdiego/452d8a3da454038acab00631842aa0f0 to your computer and use it in GitHub Desktop.
Simple Save System for Themes in Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
[CreateAssetMenu(menuName = "Settings/Theme")] | |
public class GameTheme : ScriptableObject | |
{ | |
public Color foregroundColor; | |
public Color backgroundColor; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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