Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
11
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.

Follow this Question

Answers Answers and Comments

25 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 avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges