Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kkukshtel
Last active September 17, 2020 19:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kkukshtel/ebaff3953d6db3567b81652bc55101b2 to your computer and use it in GitHub Desktop.
Save kkukshtel/ebaff3953d6db3567b81652bc55101b2 to your computer and use it in GitHub Desktop.
Super easy saving/loading of binary data for Unity games
using UnityEngine;
using System.Collections.Generic;
using System;
using System.IO;
using System.Text;
// Super easy saving/loading of binary data for Unity games
// by @kkukshtel
// License: Do whatever!
public enum SaveType
{
//Add more types here
Map,
Match,
Options
}
public static class SaveLoad
{
// Make more properites like this to easily access file names
public static List<string> Maps
{
get { return getFilesOfType(SaveType.Map); }
}
//Points to User/AppData/LocalLow/DefaultCompany/YourGame/...
public static string dataPath = Application.persistentDataPath;
// Add other directories here to indicate where you want to save
public static Dictionary<SaveType, string> gameDirectories = new Dictionary<SaveType, string>
{
{SaveType.Map, dataPath + "/maps/"},
{SaveType.Match, dataPath + "/matches/"},
{SaveType.Options, dataPath + "/options/"},
};
// Add your file extensions here - they can be anything!
public static Dictionary<SaveType, string> gameFileExtensions = new Dictionary<SaveType, string>
{
{SaveType.Map, ".map"},
{SaveType.Match, ".match"},
{SaveType.Options, ".options"},
};
// Call this at the start of your game if you want unity to make your directories for you to ensure they are there
public static void CreateGameDirectories()
{
foreach (KeyValuePair<SaveType, string> directory in gameDirectories)
{
Directory.CreateDirectory(directory.Value);
}
}
//Call this to save the data of your game. highly recommend using JsonUtility.ToJson to populate the data variable
public static void SaveData(string data, string nameOfFile, SaveType typeOfFile)
{
string file = fileName(nameOfFile, typeOfFile);
try
{
//overwrite protection
if (File.Exists(file))
{
Debug.Log(file + " already exists.");
return;
}
Debug.Log("writing " + file);
// create the file
using (FileStream fs = File.Create(file))
{
Byte[] info = new UTF8Encoding(true).GetBytes(data);
fs.Write(info, 0, info.Length);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
// Call this to load the data of your game. highly recommend wrapping this call with JSON utility like so:
// YourSerializeableClass loadedObject = JsonUtility.FromJson<YourSerializeableClass>(SaveLoad.LoadData(nameOfThingToLoad, SaveType.Map));
public static string LoadData(string nameOfFile, SaveType typeOfFile)
{
string file = fileName(nameOfFile, typeOfFile);
string data = "";
try
{
//make sure file exists
if (File.Exists(file))
{
Debug.Log("loading : " + file);
using (StreamReader sr = File.OpenText(file))
{
data = sr.ReadToEnd();
return data;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return null;
}
//Gets the full directory filename for a given filename
static string fileName(string fileName, SaveType fileType)
{
return gameDirectories[fileType] + fileName + gameFileExtensions[fileType];
}
//Get all files in your save directory of file type
//great for filling out menus
public static List<string> GetFilesOfType(SaveType fileType)
{
Debug.Log($"gettting files at {gameDirectories[fileType]} of extension type {gameFileExtensions[fileType]}");
DirectoryInfo dir = new DirectoryInfo(gameDirectories[fileType]);
FileInfo[] info = dir.GetFiles("*" + gameFileExtensions[fileType]);
List<string> fileNames = new List<string>();
foreach (FileInfo f in info)
{
fileNames.Add(Path.GetFileNameWithoutExtension(f.FullName));
}
return fileNames;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment