- Home /
How do I save a custom class of variables to playerprefs?
Hi guys, I have a custom class which I need to save individually across 5 characters and 5 jobs which can be used by any character, anytime.
[System.Serializable]
public string char;
public string job;
public class Job{
public bool crush;
public bool parry;
public bool slam;
public bool magic;
public bool evadeUp;
}
public Job[] allJobs;
Question is, how do I save this class or array?
The following is my previous attempt:
void SaveSkill ()
{
if (char == "FirstChar" && job == "Warrior")
{
if (crush == true)
PlayerPrefs.setInt("FirstcharWarriorCrush", 1);
}
}
Which will require me to cycle through every combination of character and job and skill. About 100 playerprefs.....
Can anyone help me?
That's not really appropriate for PlayerPrefs. Use some other method, like saving to a file.
Wouldn't saving to a file, permit the player to do whatever they want to it? Or share the file with other people.
Could you elaborate more? Thanks
Answer by iwaldrop · Jan 07, 2014 at 06:50 AM
Eric, of course, hit the nail on the head. Below is a very simple serializer which you can use to save the file. This will not serialize all objects, because certain types (like Textures) aren't serializable using this method. However, for simple data it is quite effective. If you need to encrypt it, check out System.Security.Cryptography.
using System;
using System.IO;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
public class Serializer
{
public static T Load<T>(string filename) where T: class
{
if (File.Exists(filename))
{
try
{
using (Stream stream = File.OpenRead(filename))
{
BinaryFormatter formatter = new BinaryFormatter();
return formatter.Deserialize(stream) as T;
}
}
catch (Exception e)
{
Debug.Log(e.Message);
}
}
return default(T);
}
public static void Save<T>(string filename, T data) where T: class
{
using (Stream stream = File.OpenWrite(filename))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, data);
}
}
}
To use it:
Serializer.Save<ExampleClass>(filenameWithExtension, exampleClass);
ExampleClass exampleClass = Serializer.Load<ExampleClass>(filenameWithExtension));
Works great for simple class serialization. Just what I need. Thanks
You could also separate serialization of the class from the concern of storing it persistently: Using the serialization mechanism shown here (using BinaryFormatter), serialize your class into a stream and then save/load using a PlayerPrefs class
Does anyone know where the files are saved when you're using the build? I can only find it in my project folder when I use the editor
Answer by Kos-Dvornik · Feb 20, 2016 at 03:00 PM
You can
1.create custom serializable class 2. serialize it into bytearray 3. convert bytearrray into base64 string 4. Save that string into PlayerPrefs like PlayerPrefs.SetString("GameData", mBase64string )
Do backward stuff for Load.
Your answer
Follow this Question
Related Questions
Serializing and restoring full Animation state 0 Answers
Unable to get save score between levels working 1 Answer
Player Prefs Mixed Array 1 Answer
Save player position when trigger is destroyed 1 Answer
Error serializing a class to save / load 2 Answers