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 IronGirl · Mar 22, 2016 at 01:17 AM · unity 5loadsave data

Save Load Data unity

I ve write this code, i tried to save and load Data into Unity. Here is my code

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using System.Collections.Generic;
 using System.Linq;
 
 
 
 public class GameControl : MonoBehaviour
 {
 public static GameControl control;
 
 public float health =10;
 public float experience=10;
 
 public  Dictionary<string, object> Name_Dic = new Dictionary<string, object>() 
 {
     {"laura",""},
     {"steph ",""},
     {"Ryan",""},      
 };
 
 void Start()
 {
     control = this;
 
 
 }
 
 void OnGUI()
 {
     GUI.Label(new Rect(10,10,100,30),"Health"+health);
     GUI.Label(new Rect(10,40,100,30),"Experience"+experience);
 
 
 }
 
 public void Save()
 {
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Create(Application.persistentDataPath +"/playerInfo.dat");
 
     PlayerData data = new PlayerData();
     data.experience = experience;
     data.health = health;
     data.Name_Dic = Name_Dic;
 
     bf.Serialize(file, data);
 
     file.Close();
 }
 
 public void Load()
 {
     if(File.Exists(Application.persistentDataPath+"/playerInfo.dat"))
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Open(Application.persistentDataPath+"/playerInfo.dat",FileMode.Open);
         PlayerData data = (PlayerData)bf.Deserialize(file);
         file.Close();
 
         health = data.health;
         experience = data.experience;
         Name_Dic = data.Name_Dic;
     }
 }
 }
 
 [Serializable]
 
 class PlayerData
 {
 public float health;
 public float experience;
 
 public  Dictionary<string, object> Name_Dic = new Dictionary<string, object>() 
 {
     {"laura",""},
     {"steph ",""},
     {"Ryan",""},      
 };
 
 public  Dictionary<string, string> Dialogs_Dic = new Dictionary<string, string>() 
 {
     {"A1", "Hello"},
     {"A2", "Nice"},
     {"A3", "Test"},
 };
 }

The problem now that when i click on save button , i save the health and experience int because when i click on load button i finc that these two variables are saved and loaded. My problem is with the dictionary, i can't know if when i click on button save the dictionary values are saved or not. How can i now if the dictionary values are saved when i click on function saved and loaded if i click on button load.Is there a solution for that, thanks for your help

Comment
Add comment · Show 4
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 Cherno · Mar 22, 2016 at 09:50 AM 0
Share

You could go through each $$anonymous$$eyValuePair and list it.

avatar image IronGirl Cherno · Mar 22, 2016 at 10:01 AM 0
Share

Hello,

Thanks for your answer . If i added keyvaluepair , in the Load function it is after the Comment part in the code. when i cliick on Save button in the code and i click on load button i receive "laura","",, "steph ","", "Ryan","", as response. So the dictionary has been saved and loaded?is that true? Thaks for your answer

 public void Load()
  {
      if(File.Exists(Application.persistentDataPath+"/playerInfo.dat"))
      {
          BinaryFormatter bf = new BinaryFormatter();
          FileStream file = File.Open(Application.persistentDataPath+"/playerInfo.dat",File$$anonymous$$ode.Open);
          PlayerData data = (PlayerData)bf.Deserialize(file);
          file.Close();
  
          health = data.health;
          experience = data.experience;
          Name_Dic = data.Name_Dic;
          // Added Code
          foreach ($$anonymous$$eyValuePair<string, object> pair in data.Name_Dic)
             {
                 Debug.Log(String.Format("{0}, {1}", pair.$$anonymous$$ey, pair.Value));
             }
      }
  }
avatar image Cherno IronGirl · Mar 22, 2016 at 10:38 AM 0
Share

Yes, the dictionary has been loaded successfully :)

Show more comments

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

63 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

Related Questions

unity fontainebleau importing error 0 Answers

Save and load system not working please help 1 Answer

Saving Data on platform cloud 0 Answers

Loading prefabs with script references from assetbundle 0 Answers

Using save options to create saves for toggle buttons, but won't compile! 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