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
1
Question by psykojello2 · May 11, 2014 at 08:49 AM · windows phonebinaryformatterwindows store

Read write data for Windows Store/Phone C#

Unity for Windows Store/Phone don't support System.IO and BinaryFormatters. How do I do the following without them:

I'm trying to serialize my GameProgress (level scores) which is basically:

 GameProgress gameProgress = List<LevelProgress>();
 LevelProgress:
  ---- levelName (string)
  ---- time (float)
  ---- score (int)

For other platforms, I do this:

 BinaryFormatter bf = new BinaryFormatter();
 FileStream fs = File.Create(filename);
 bf.Serialize(fs, gameProgress);
 fs.Close();


I'm having trouble with compiling a plugin for Windows that handles this for me as the namespaces clash with other plugins I'm using.

I was hoping to use UnityEngine.Windows.File.WriteAllBytes() and ReadAllBytes() instead, but could not figure out how to convert my data to an array of Bytes.

Does someone have a code example they can show me?

Comment
Add comment · Show 2
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 $$anonymous$$ · May 11, 2014 at 08:56 AM 0
Share

You can use the PlayerPrefs.

avatar image psykojello2 · May 11, 2014 at 09:36 AM 0
Share

I could do that, but it gets pretty cumbersome for a complex data type. I would have to save dictionary keys such as "World1Level1Time", "World1Level2Score" etc.

Also, there's the 1$$anonymous$$B limit. I don't think I'd hit that right now, but that could limit my save data in the future.

1 Reply

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

Answer by psykojello2 · Jun 22, 2014 at 11:00 PM

I'm going to answer my own question. It took me a month, but I finally have a solution that works.

The solution is to create a Windows Phone Plugin, but you also need to create a dummy plugin for Unity to use. Here's a guide on how to do this: http://docs.unity3d.com/Manual/wp8-plugins-guide-csharp.html

However, I found it difficult to create the plugin because I did not have a professional license of Visual Studio. I ended up using Visual Studio Express 2012 to create the Unity plugin and Visual Studio 2012 for Windows Phone to create the WP plugin. I then copied the WP project into the VS 2012 solution and was able to work on both together.

Credit for the serialization code goes to Carlos Figueira on this thread .

PLUGIN CODE

In my Real DLL project, I have a class called FileIO, with 2 functions:

 using System.Runtime.Serialization;
 
 namespace UnityWindowsPhonePlugin
 {
 
     public class FileIO
     {
         
         public static byte[] SerializeObject<T>(T obj)
         {
             using (MemoryStream ms = new MemoryStream())
             {
                 using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(ms))
                 {
                     DataContractSerializer dcs = new DataContractSerializer(typeof(T));
 
                     dcs.WriteObject(writer, obj);
                     writer.Flush();
                     return ms.ToArray();
                 }
             }
         }
 
         public static T DeserializeObject<T>(byte[] obj )
         {
             using (MemoryStream memoryStream = new MemoryStream(obj))
             {
                 using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(memoryStream, XmlDictionaryReaderQuotas.Max))
                 {
                     DataContractSerializer dcs = new DataContractSerializer(typeof(T));
                     return (T)dcs.ReadObject(reader);
                 }
             }
 
         }
 
     }
 }


For my Fake DLL, I implement fake versions of the same classes.

 namespace UnityWindowsPhonePlugin
 {
     public class FileIO
     {
 
         public static byte[] SerializeObject<T>(T obj) 
         {
             return new byte[0];
         }
 
         public static T DeserializeObject<T>(byte[] obj) 
         {
             return default(T);
         }
     }
 }


UNITY CODE

Copy your Real DLL to Assets\Plugins\WP8, and your fake DLL to Assets\Plugins.

In Unity, this is how I save my game progress:

 #if UNITY_WINRT
         byte[] bytes = UnityWindowsPhonePlugin.FileIO.SerializeObject<GameProgress>(this);
         File.WriteAllBytes(getSaveFile(), bytes);
 #else
         BinaryFormatter bf = new BinaryFormatter();
         FileStream fs = File.Create(getSaveFile());
         bf.Serialize(fs, this);
         fs.Close();
 #endif



And I load my Game Progress like this:

 byte[] bytes = File.ReadAllBytes(saveFile);        
 GameProgress gp = UnityWindowsPhonePlugin.FileIO.DeserializeObject<GameProgress>(bytes);

Hope that helps others who are stuck with the same problem I was. Let me know if you need help with the plugin. I might be able to send you my VS project.

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 Jojo Batista · Apr 15, 2015 at 02:16 PM 1
Share

Could you attach the dll and the dummy? thanks! (I'll try to create the plugin myself tho)

avatar image ben_page · Feb 21, 2016 at 12:32 AM 0
Share

Is there any way one could use Sharp Serializer for this purpose? I am currently attempting your solution for my project. Apparently the plug in is able to replace the purpose of the built in binary formatted, although I'm not sure how... (I'm new to serializes and am starting work on a project that has been in development). I'm going to keep stabbing away at it, but if you happen to notice this comment, any sort of help would be much obliged :)

Thanks Ben

avatar image psykojello2 ben_page · Mar 29, 2016 at 08:19 AM 0
Share

Hi Ben, just saw your comment. Unfortunately I'm not too familiar with Sharp serializer or any of the plugin stuff really :) In fact I abandoned Windows Phone since my last post. It just became too much work trying to get plugins to behave. I'll just build for W10 at some point.

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

23 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

Related Questions

Does Unity 5 support Windows Phone Networking? 1 Answer

Building error when building for windows phone(UWP). 1 Answer

Windows Phone Ads 0 Answers

Format of .scene files etc 1 Answer

Empty Application.persisentDataPath on Android device causing UnauthorizedAccessException 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