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?

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));

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.