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
0
Question by ungalyant2 · Jan 27, 2012 at 04:17 AM · listssavingserializable

Serializing lists

Hey guys, so I've looked around for an answer to my query, and I've found some, but they don't seem to work, I have a class I'm trying to save player profiles with, and then I have a class that's a string list that will contain all the names of the profile files so the game will know the names of what to load.

My list class is in the same C# script as the loading class, but I don't think that matters, it looks like this

 [System.Serializable()]

public class savedProfiles { public List names = new List(); }

public class _GameSaveLoad: MonoBehaviour {

// An example where the encoding can be found is at // http://www.eggheadcafe.com/articles/system.xml.xmlserialization.asp // We will just use the KISS method and cheat a little and use // the examples from the web page since they are fully described

   // This is our local private members
   bool _ShouldSave, _ShouldLoad,_SwitchSave,_SwitchLoad;
   string _FileLocation, fileName;
   string _PlayerName;
    string _data;
 List<UserData> loadedProfiles = new List<UserData>();
 savedProfiles profiles = new savedProfiles();
 
 public List<UserData> LoadedProfiles
 {
     get {return loadedProfiles;}
 }
     
 public int Save(UserData file)
 {
     if (loadedProfiles.Count == null)
     {
         return 1;
         Debug.Log("Profiles list is null or empty");
     }
     else
     {
         
         for (int i = 0; i < loadedProfiles.Count; i ++)
         {
             //finding same named file to replace old file with new
             if (file._iUser.profileName == loadedProfiles[i]._iUser.profileName)
             {
                 loadedProfiles[i] = file;
             }
         }
         
         //Updating the list of profiles
          fileName = "profiles.xml";        
         _data = SerializeObject(profiles);
            CreateXML(fileName);
     
         //Updating each profile
            fileName = "loadedProfiles.xml";        
            _data = SerializeObject(loadedProfiles);
            CreateXML(fileName);
         return 0;
     }
 }
 
 public int NewSave(UserData file)
 {
     for (int i = 0; i < loadedProfiles.Count; i ++)
     {
         //If a profile with the same name already exists, return early with error
         if (file._iUser.profileName == loadedProfiles[i]._iUser.profileName)
         {
             return 1;
         }
     }
     
     loadedProfiles.Add(file);
     profiles.names.Add(file._iUser.profileName);
     _FileLocation=Application.dataPath;
     
     
     //Updating the list of profiles
      fileName = "profiles.xml";        
     _data = SerializeObject(profiles);
        CreateXML(fileName);
     
     //Updating each profile
      fileName = "loadedProfiles.xml";        
     _data = SerializeObject(loadedProfiles);
        CreateXML(fileName);
     
     return 0;
 }

     string UTF8ByteArrayToString(byte[] characters)
 {
       UTF8Encoding encoding = new UTF8Encoding();
       string constructedString = encoding.GetString(characters);
       return (constructedString);
 }
   
 byte[] StringToUTF8ByteArray(string pXmlString)
 {
       UTF8Encoding encoding = new UTF8Encoding();
       byte[] byteArray = encoding.GetBytes(pXmlString);
      return byteArray;
 }
   
 string SerializeObject(object pObject)
 {
       string XmlizedString = null;
     MemoryStream memoryStream = new MemoryStream();
     XmlSerializer xs = new XmlSerializer(typeof(UserData));
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
     xs.Serialize(xmlTextWriter, pObject);
       memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
       XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
       return XmlizedString;
 }
   
 void CreateXML(string fileName)
 {
     
       StreamWriter writer;
       FileInfo t = new FileInfo(_FileLocation+"\\"+ fileName);
       if(!t.Exists)
       {
          writer = t.CreateText();
       }
       else
       {
         
            t.Delete();
         writer = t.CreateText();
     }
       writer.Write(_data);
       writer.Close();
       Debug.Log("File written.");
 }

but it when I try to use the saving code it states that "the type of arguement object 'saved profiles is not a primitive" what can I do?

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 syclamoth · Jan 27, 2012 at 04:25 AM 1
Share

Are you trying to save this in playerprefs? Playerprefs doesn't support this kind of thing. You need to use some kind of IO functionality to save the information to a file.

Could you please edit your question to include the relevant parts of your saving code?

avatar image ungalyant2 · Jan 27, 2012 at 04:44 AM 0
Share

as should now be more obvious, I'm not using player prefs as I have a fair amount of data to store, I'm using X$$anonymous$$L files, sorry if there's too much code to make sense of now. For a large part of it I'm using serialization code that someone posted on the unity wiki, but I've also removed and added in functions to better suit what I'm doing.

avatar image ungalyant2 · Jan 31, 2012 at 04:25 AM 0
Share

If I could get any further assistance on this it would be greatly appreciated

0 Replies

· Add your reply
  • Sort: 

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

6 People are following this question.

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

Related Questions

Serializing a GameObject, or otherwise saving a scene 1 Answer

Saving a list to json 1 Answer

Storing list aka "save function" 3 Answers

Saving Just Five Top Scores? 1 Answer

How to save list of Sprites? 1 Answer


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