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 /
  • Help Room /
avatar image
0
Question by FritzPeace · Apr 27, 2016 at 08:36 PM · c#androidsavingpersistencebinaryformatter

Data not Saving in Android Using Unity Persistance Saving Training.

Hey guys! I am trying to save some data in my game, but it just isn't working. I want it to be stored in the device itself, and not on the sd-card. That is why I use the Internal Only selection, on the Player Settings.

Below is my script. I tried fixing it, looking everywhere, but got no answers. I did choose to use Binary Formatter instead of Player Prefs because I believe it is safer, and easier to use. Perhaps I shouldn't be using it at all... In the Editor everything works fine, and data is saved. The problem is building at Android. By the way, I didn't test yet on iOS, but wouldn't be surprised if it didn't work.


 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class Datakeeper : MonoBehaviour {
     #region Define Stuff
     [Header("Variables")]
     public static int score;
     public static int highscore;
     public static int money;
     public static DateTime lastTime;
     public static Vector3 cameraPos;
     public static bool gotGift;
     public static bool gotEgg;
     public static bool watchedAd;
     public static bool destroyPlayerOnMenuScene;
 
     [Header("Player Manager")]
     public static Dictionary<string, bool> birdsKeeper;
     public GameObject[] birdList;
     public static string selectedBird;
     public static float PlayerSliderValue;
     public static Vector3 originalPlayerPos = new Vector3(-2, 0.5f, 5);
     public static Vector3 playerPos = new Vector3(-2, 0.5f, 5);
 
     [Header("Background Manager")]
     public static Dictionary<string, bool> bgKeeper;
     public GameObject[] bgList;
     public static string selectedBg;
     public static bool backgroundMoving = true;
     public static bool ResetBgPos = false;
 
 
     [Header("Save Config")]
     private string fileName = "/info.data";
 
     public enum Status { SELECTED, SUCCESS, FAILURE};
     #endregion
 
     #region Starting
     void Start()
     {
         Load();
         if (Application.platform == RuntimePlatform.IPhonePlayer)
         {
             Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
         }
         DontDestroyOnLoad(gameObject);
         if(birdsKeeper == null)
         {
             InitializeBirdDictionary();
             lastTime = DateTime.UtcNow.AddDays(-2);
         }
 
         if(bgKeeper == null)
         {
             InitializeBgDictionary();
         }
     }
 
     void InitializeBirdDictionary()
     {
         birdsKeeper = new Dictionary<string, bool>();
         //Birds should be named: "bird 0", "bird 10", "bird 15", etc.
         //All birds prefabs should be found inside the birdList.
         for (int i = 0; i < birdList.Length; i++) //For each bird add a bird dictionary key named "bird " + "number" like "bird 1"
         {
             if(i == 0)//If it is the default bird make it true
             {
                 birdsKeeper.Add("bird " + i.ToString(), true);
             }
             else
             {
                 birdsKeeper.Add("bird " + i.ToString(), false);
             }
         }
         if (selectedBird == null || selectedBird == "")
         {
             selectedBird = "bird 0";
         }
     }
 
     void InitializeBgDictionary()
     {
         bgKeeper = new Dictionary<string, bool>();
         //bgs should be named: "bg 0", "bg 10", "bg 15", etc.
         //All bgs prefabs should be found inside the bgList.
         for (int i = 0; i < bgList.Length; i++) //For each bg add a bg dictionary key named "bg " + "number" like "bg 1"
         {
             if (i == 0)//If it is the default bg make it true
             {
                 bgKeeper.Add("bg " + i.ToString(), true);
                 if (selectedBg == null || selectedBg == "")
                 {
                     selectedBg = "bg 0";
                 }
             }
             else
             {
                 bgKeeper.Add("bg " + i.ToString(), false);
             }
         }
     }
     #endregion
     #region Load/Save
 
     void OnLevelWasLoaded()
     {
         Save();
     }
 
     void OnApplicationQuit()
     {
         Save();
     }
 
     void OnApplicationPause()
     {
         Save();
     }
 
     public void Save()
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + fileName);
 
         Data data = new Data();
         //Variables
         data.highscore = highscore;
         data.money = money;
         data.lastTime = lastTime;
         //Player
         data.birdsKeeper = birdsKeeper;
         data.selectedBird = selectedBird;
         data.PlayerSliderValue = PlayerSliderValue;
         //Background
         data.bgKeeper = bgKeeper;
         data.selectedBg = selectedBg;
 
         bf.Serialize(file, data);
         file.Close();
     }
 
     public void Load()
     {
         if (File.Exists(Application.dataPath + fileName))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + fileName, FileMode.Open);
             Data data = (Data)bf.Deserialize(file);
             file.Close();
             //Variables
             highscore = data.highscore;
             money = data.money;
             lastTime = data.lastTime;
             //Player
             birdsKeeper = data.birdsKeeper;
             selectedBird = data.selectedBird;
             PlayerSliderValue = data.PlayerSliderValue;
             //Background
             bgKeeper = data.bgKeeper;
             selectedBg = data.selectedBg;
         }
         else
         {
             lastTime = DateTime.UtcNow;
         }
     }
 #endregion
 
 #region Invokable Voids
     public static void AddScore(int value)
     {
         score += value;
         if (score > highscore)
         {
             highscore = score;
         }
     }
 
     public static void AddMoney(int value)
     {
         money += value;
     }
 
     public void AddMoney_(int value)
     {
         money += value;
     }
 
     public static Status BuyBird(string birdName, int birdCost)
     {
         if (birdsKeeper[birdName])
         {
             selectedBird = birdName;
             return Status.SELECTED;
         }
         else
         {
             if (money >= birdCost)
             {
                 money -= birdCost;
                 birdsKeeper[birdName] = true;
                 selectedBird = birdName;
                 return Status.SUCCESS;
             }
             else
             {
                 return Status.FAILURE;
             }
         }
     }
 
     public static bool HasBird(string birdName)
     {
         if (birdsKeeper[birdName])
         {
             return true;
         }
         else
         {
             return false;
         }
     }
 
     public static Status BuyBg(string bgName, int bgCost)
     {
         if (birdsKeeper[bgName])
         {
             selectedBird = bgName;
             return Status.SUCCESS;
         }
         else
         {
             if (money >= bgCost)
             {
                 money -= bgCost;
                 bgKeeper[bgName] = true;
                 return Status.SUCCESS;
             }
             else
             {
                 return Status.FAILURE;
             }
         }
     }
 
     public static void StartBgMovement()
     {
         backgroundMoving = true;
         ResetBgPos = true;
     }
     public static void StopBgMovement()
     {
         backgroundMoving = false;
     }
 #endregion
 }
 
 [Serializable]
 public class Data
 {
     //Variables
     public int highscore;
     public int money;
     public DateTime lastTime;
 
     //Player Manager
     public Dictionary<string, bool> birdsKeeper;
     public string selectedBird;
     public float PlayerSliderValue;
 
     //Background Manager
     public Dictionary<string, bool> bgKeeper;
     public string selectedBg;
 }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by XRenkoX · May 02, 2016 at 08:20 PM

Im currently having the same issue.

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 FritzPeace · May 02, 2016 at 09:20 PM 0
Share

I think ill have to buy easysave2 or something like that.

avatar image XRenkoX · May 03, 2016 at 12:07 AM 0
Share

I got it to work. You have to save before OnApplicationQuit()-- I think this function is broken. But writing and reading from a file works for me android and desktop. I made some GUI buttons like in the tutorial and tested it out. But i definitely think OnApplicationQuit() is broken. Does anyone know if the scene$$anonymous$$anager takes care of this too? Im going to look right meow and report back.

avatar image FritzPeace · May 09, 2016 at 01:01 PM 0
Share

@XRenkoX In my case, OnEnable, OnDisable, Start, Awake, LevelWasLoaded, and all other functions would not be working. Still, if you get any success tell me :D

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

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

Related Questions

Waiting for Saving/Loading? 2 Answers

Android persistentdatapath Can be read but not written. 0 Answers

BinaryFormatter - Save and Load lists of data? 0 Answers

PlayerPrefs not working on android 1 Answer

Gyroscope Samsung S7 doesn't work properly 4 Answers


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