• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by AozakiKyuuji · Jan 07, 2014 at 04:36 AM · c#playerprefsarraysserializationsave

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Jan 07, 2014 at 05:41 AM 0
Share

That's not really appropriate for PlayerPrefs. Use some other method, like saving to a file.

avatar image AozakiKyuuji · Jan 07, 2014 at 06:10 AM 0
Share

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

avatar image Eric5h5 · Jan 07, 2014 at 06:20 AM 0
Share

So does PlayerPrefs.

2 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

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

 
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Andre Barbosa · May 02, 2014 at 12:49 PM 0
Share

Works great for simple class serialization. Just what I need. Thanks

avatar image liortal · May 02, 2014 at 12:56 PM 0
Share

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

avatar image AngryCSharpNerd · Jun 29, 2015 at 03:11 AM 0
Share

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

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Issues with either PlayerPrefs.SetVariable or UnityEvent.Invoke 1 Answer

How to save different values with same script? 1 Answer

Drag and Drop PlayerPrefs 2 Answers

Using PlayerPrefs to save a C# Generic List 1 Answer

Unity Saved Serialized Levels 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges