Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 dotEff · Nov 11, 2016 at 08:34 AM · c#androidserializationdatapersistentdatapath

Serialization Data not working on Android?

I'm upgrading my game to be able to save fastest completion time for each level using serialization. I've tested on the editor and on PC as standalone and it works fine. Running it on Android however, it doesn't seem like it is writing the data thus leaving fastest time record empty.

Here's the coding that deals with the saving and loading the best time:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class PlayTimer : MonoBehaviour {
 
     public Text timerText;
     public bool gameEnd = false;
     public Text currentBestTime;
     public Text yourTime;
 
     private float startTime;
     public string finalTime;
 
     float t;
 
     float minutes;
     float seconds;
     float milliseconds;
     float totalMs;
     float totalBest;
 
     float bestMinutes;
     float bestSeconds;
     float bestMilliseconds;
 
     public int level;
     public float targetGold;
     public float targetSilver;
 
     public GameObject goldMedal;
     public GameObject silverMedal;
     public GameObject bronzeMedal;
 
     TimeData timer = new TimeData();
 
     void Start () {
         startTime = Time.time;
         Load ();
     }
     
     // Update is called once per frame
     void Update () {
 
         if (gameEnd) {
             finalTime = timerText.text;
             yourTime.text = timerText.text;
             totalMs = ((minutes * 60) * 1000) + (seconds * 1000) + milliseconds;
 
             if (totalMs <= targetGold) {
                 goldMedal.SetActive (true);
             } else if (totalMs <= targetSilver) {
                 silverMedal.SetActive (true);
             } else {
                 bronzeMedal.SetActive (true);
             }
 
             if (totalMs < totalBest) {
                 //Debug.Log (totalMs);
                 //Debug.Log (totalBest);
                 Save ();
             }
             return;
         }
 
         t = Time.time - startTime;
 
         minutes = ((int)t / 60);
         seconds = (t % 60);
         milliseconds = (t * 1000);
 
         milliseconds = milliseconds % 1000;
 
         timerText.text = minutes.ToString("00") + ":" + seconds.ToString("00") + ":" + milliseconds.ToString("00");
     }
 
     public void Save(){
         //Debug.Log ("Saving...");
         BinaryFormatter bf = new BinaryFormatter ();
         FileStream stream = File.Open (Application.persistentDataPath + "/timer.sav", FileMode.OpenOrCreate);
 
         timer.minute[level] = minutes;
         timer.second[level] = seconds;
         timer.millisecond[level] = milliseconds;
 
         if (totalMs <= targetGold) {
             timer.medal [level] = 3;
         } else if (totalMs <= targetSilver) {
             timer.medal [level] = 2;
         } else {
             timer.medal [level] = 1;
         }
 
         bf.Serialize (stream, timer);
         stream.Close (); //finalize the saving
     }
 
     public void Load(){
         //Debug.Log ("Loading...");
         if (File.Exists (Application.persistentDataPath + "/timer.sav")) {
             BinaryFormatter bf = new BinaryFormatter ();
             FileStream stream = File.Open (Application.persistentDataPath + "/timer.sav", FileMode.Open, FileAccess.Read);
 
             timer = (TimeData)bf.Deserialize(stream);
 
             stream.Close ();
 
             bestMinutes = timer.minute[level];
             bestSeconds = timer.second[level];
             bestMilliseconds = timer.millisecond[level];
 
             totalBest = ((bestMinutes * 60) * 1000) + (bestSeconds * 1000) + bestMilliseconds;
 
             if (bestMilliseconds > 0.0) {
                 SetBestTime (true);
             }
 
         } else {
             SetBestTime (false);
             //Debug.LogError ("File does not exist.");
         }
     }
 
     public void SetBestTime(bool Set){
         if (Set) {
             currentBestTime.text = "Best Time:\t" + bestMinutes.ToString("00") + ":" + bestSeconds.ToString("00") + ":" + bestMilliseconds.ToString("00");
         } else {
             currentBestTime.text = "Best Time:\t--:--:--";
         }
     }
 }
 
 [Serializable]
 public class TimeData {
 
     public float[] minute;
     public float[] second;
     public float[] millisecond;
     public int[] medal;
 
     public TimeData() {
         minute = new float[50];
         second = new float[50];
         millisecond = new float[50];
         medal = new int[50];
     }
 
 }

I've been trying to find solution and fixes like use persistentDataPath, making sure that the Write Access is set to External, double checking the AdroidManifest.xml and still to no avail.

This is my first time doing serialization on mobile so I'm not sure what else I could be missing since the test on PC seems to be working fine. Is there something I'm missing? I need to have this feature working by end of the month.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

Answer by dotEff · Nov 19, 2016 at 03:01 PM

Seems like no one could answer it. Anyhow, I've decided to switch to PlayerPref instead and was able to structure it to be able to save information for each levels as PlayerPref can only save one "file" for the whole project, I add an increment number onto the filename allowing the game to save specific data for specific level.

Hope this will help others looking into saving multiple information per level.

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

Answer by Spartiate0033 · Nov 19, 2016 at 08:07 PM

Hey, I think I have a better answer for you then

I wrote an Android data save several months ago and I see your problem almost immediately. When it comes to Android, you are actually going to want to use something called Streaming Assets Path. Why? When trying to access an APK file, because it is an archive, it becomes difficult to access (for example, FileInfo does not support archives)

I recommend using Streaming Assets because it is fairly easy to access on Android and allows you to access things like custom level files, player data, etc.

Also, player prefs is extremly unsafe. While technically anything can be broken into, there are dozens of player prefs readers and makes your game a lot less safe. Give these a read.

http://answers.unity3d.com/questions/156531/is-playerprefs-secure.html http://answers.unity3d.com/questions/182755/playerprefs-lmitations.html http://answers.unity3d.com/questions/527328/playerprefs-credebility-advantagesdisadvantages.html

This is a snippet from what I have in my own Save/Load system. (This being the load function)

 if (Utility.OSManager.Instance.GetOSType() == Utility.OSManager.OSType.Android)
                     {
                         Debug.Log("OS TYPE is Android");
 
                         string item = "Levels/" + name + "." + extension;
                         string androidFilePath = System.IO.Path.Combine(Application.streamingAssetsPath, item);
                         WWW www = new WWW(androidFilePath);
                         
 
                         while (www.isDone == false)
                         {
 
                         }
 
                         string realPath = Application.persistentDataPath + name;
                         System.IO.File.WriteAllBytes(realPath, www.bytes);
 
                         androidFilePath = realPath;
 
                         if (File.Exists(androidFilePath))
                         {
                             Debug.Log("File found!");
                             BinaryFormatter bf = new BinaryFormatter();
 
                             // 1. Construct a SurrogateSelector object
                             SurrogateSelector ss = new SurrogateSelector();
                             // 2. Add the ISerializationSurrogates to our new SurrogateSelector
                             AddSurrogates(ref ss);
 
                             // 3. Have the formatter use our surrogate selector
                             bf.SurrogateSelector = ss;
 
                             FileStream file = File.Open(androidFilePath, FileMode.Open);
 
                             SaveGame loadedGame = (SaveGame)bf.Deserialize(file);
 
                             file.Close();
 
                             return loadedGame;
                         }
                         else
                         {
                             return null;
                         }
                     }

  
Comment
Add comment · Show 1 · 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 dotEff · Nov 20, 2016 at 12:58 PM 0
Share

Thanks Spartiate0033... I'll check on the solution you gave for future game where saving needs to become much more secured.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Saved informations not removed after uninstall on Android 2 Answers

C# Serialization is working fine in Unity but not on Android 2 Answers

Saving and Loading Various Things 1 Answer

how to serialize unity variables? (Sprite, Image ...) 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