Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What is the most efficient method of saving a class?

Discussion in 'Scripting' started by AozakiKyuuji, Jan 7, 2014.

  1. AozakiKyuuji

    AozakiKyuuji

    Joined:
    Sep 21, 2012
    Posts:
    15
    Hi guys, I have a custom class which I need to save individually across 5 characters and 5 different jobs which can be equipped to any character.
    Code (csharp):
    1.  
    2.     [System.Serializable]
    3.     public string char;
    4.     public string job;
    5.      
    6.     public class Skill{
    7.      
    8.     public bool     crush;
    9.     public bool     parry;
    10.     public bool     slam;
    11.     public bool     magic;
    12.     public bool     evadeUp;
    13.      
    14.     }
    15.      
    16.     public Skill[] allSkills;
    17.  
    Question is, how do I save this class or array?

    The following is my previous attempt:

    Code (csharp):
    1. void SaveSkill ()
    2.     {
    3.     if (char == "FirstChar"  job == "Warrior")
    4.     {
    5.          if (crush == true)
    6.              PlayerPrefs.setInt("FirstcharWarriorCrush", 1);
    7.  
    8.          if (parry== true)
    9.              PlayerPrefs.setInt("FirstcharWarriorParry", 1);
    10.     }
    11.     }
    Which will require me to cycle through every combination of character and job and skill. About 100 playerprefs...

    Please do help. Thank you!
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Frankly... I would turn that object into a JSon and save it as a String.

    I never used it, but there's a free JSon parser here: http://wiki.unity3d.com/index.php/SimpleJSON

    Another way would be by reflection and building prefs from the reflected values.
     
  3. AozakiKyuuji

    AozakiKyuuji

    Joined:
    Sep 21, 2012
    Posts:
    15
    I see... Thanks! I've read through that wiki and I like what I see. Though it didn't specifically mention how to convert stuff into JSon.

    Though you've never used it, I still have to ask if you have any ideas...

    If you can't answer this, no problem. I hope someone who has used it may be able to help.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    We are talking about runtime serialization. Like which job a player took, or his current stats. AssetDatabase is Editor only.
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    I read the question as a design-time thing, but if it's runtime then you're certainly right.
     
  7. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568