Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

IMPORTANT fast reply needed!

Discussion in 'Scripting' started by ajsnarr98, Jan 20, 2016.

  1. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    I use binary formaters to serialize data in my game, so I use the import staments:
    Code (CSharp):
    1. using System.Runtime.Serialization.Formatters.Binary;
    2. using System.IO;
    These import statements are perfectly reasonable and I get no error when running my game in Unity, but when I try to compile the project Unity tells me "The type or namespace name 'Formatters' does not exist in the namespace 'System.Runtime.Serialization' (are you missing an assembly reference?)"

    How do I fix this? I need to submit a demo version in 30 minutes and I don't know what to do.. :eek:
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    What platform are you compiling for? Some platforms don't have the full library.

    It's also worth checking the version of .NET that the name space is from. Unitys version is pretty old.
     
  3. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    In your player settings for the offending platform, check what you have set for "Api Compatibility Level" you may need to set it to ".NET 2.0" rather than ".NET 2.0 subset"
     
    smile59 likes this.
  4. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    I found a solution later, I ended up saving my data in json files instead.
     
    GarthSmith likes this.
  5. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    I think the issue that you were having is that you were including too much.

    So this:

    Code (csharp):
    1. using System.Runtime.Serialization.Formatters.Binary;
    would become this:

    Code (csharp):
    1. using System.Runtime.Serialization;
     
  6. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    I tried that, it did not provide the required classes for binary serialization. The reason I had an error when compiling with .Formatters.Binary I found out was that, having to do with the fact the fact that I was building for the windows store, the windows store does not support certain library classes. As a result I had to settle for saving everything in json format.
     
    Last edited: Jan 20, 2016
    The-Little-Guy likes this.
  7. Zythus

    Zythus

    Joined:
    Jun 28, 2014
    Posts:
    30
    I already followed the tutorial on the data persistence, created BinaryFormatter and it compiled flawlessly in Unity, then, when trying to build into Windows Store, boom
    "The type or namespace name 'Formatters' does not exist in the namespace 'System.Runtime.Serialization' (are you missing an assembly reference?)"
    Any simple way of fixing that or saving stuff in json format is one of my most reasonable options?
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    you need to work around.
     
  9. Zythus

    Zythus

    Joined:
    Jun 28, 2014
    Posts:
    30
    To be honest there are hundreds of tutorials on JSON, better and worse ones, ones using local databases, websites, apache servers, phpmyadmin and so on. Think I'm more confused now, after reading and watching so many videos on YouTube about that. Don't even know if I should use XML or JSON, what really the difference is. People recommend both of them, I did some digging into XML years ago, but I'm not sure which one would be better.

    So my question would be: is there a good, easy, reliable tutorial to explain how to set up XML or JSON in order to save Score/HighScore and player's chosen character from a different scene?

    Also found something like BSON and JSON .NET for Unity (paid), could I use one of them?
     
  10. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73

    1. Store everything you want to save in a file in a single class (this class can have objects of other classes inside it if need be)
    2. The class being serialized must have a "[serializable]" tag on it, and the classes of any objects stored inside it must also have the serializable tag.
    3. Example Script:
    Code (CSharp):
    1. using System;
    2.  
    3. [Serializable]
    4. public class MyData
    5. {
    6.     public float[] a;
    7.     public int b;
    8.     public OtherData c;
    9.  
    10.     public MyData()
    11.     { }
    12. }
    Code (CSharp):
    1. using System;
    2.  
    3. [Serializable]
    4. public class OtherData
    5. {
    6.     string something;
    7.  
    8.     public OtherData()
    9.         { }
    10. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.IO;
    5.  
    6. public class MyScript : Monobehavior
    7. {
    8.     MyData data;
    9.  
    10.     //save data in json format
    11.     private void SaveData()
    12.     {
    13.         string fileName = Application.persistentDataPath + "\mydata.json"
    14.  
    15.         if (File.Exists(fileName))
    16.         {
    17.             File.Delete(fileName);
    18.         }
    19.         using (FileStream file = File.Create(fileName))
    20.         {
    21.             using (StreamWriter outputFile = new StreamWriter(file))
    22.             {
    23.                 outputFile.Write(JsonUtility.ToJson(data));
    24.             }
    25.         }
    26.     }
    27.  
    28.     //load json file into class
    29.     private MyData LoadData()
    30.     {
    31.         string fileName = Application.persistentDataPath + "\mydata.json";
    32.  
    33.         if (File.Exists(fileName))
    34.         {
    35.             using (FileStream file = File.Open(fileName, FileMode.Open))
    36.             {
    37.                 string content;
    38.                 using (StreamReader reader = new StreamReader(file))
    39.                 {
    40.                     content = reader.ReadToEnd();
    41.                 }
    42.                 MyData d = JsonUtility.FromJson<MyData>(content);
    43.  
    44.                 return d;
    45.             }
    46.         }
    47.         else //file does not exit
    48.         {
    49.             // here you should throw some kind of error message
    50.          
    51.             // this is so the function "returns a value"
    52.             return new MyData();
    53.         }
    54. }
     
    Zythus likes this.