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

Unity Serializer to save only a variable

Discussion in 'Scripting' started by Dilanation, Apr 6, 2015.

  1. Dilanation

    Dilanation

    Joined:
    Apr 6, 2015
    Posts:
    2
    I want to use unity serializer by whydoidoit to save only a variable in a script and not a whole gameobject. I have looked through the get started guide, forums, and the web and I could not find how to do this. I am fairly new to unity and coding in general so I am sorry if this is a dumb thing to ask.

    A public static Dictionary will hold the data for the player which is the only thing I need saved and loaded. How would I go about doing this with Unity Serializer?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class gameData : MonoBehaviour {
    6.  
    7.     public static Dictionary<Vector3, Dictionary<int,int>> playerData = new Dictionary<Vector3, Dictionary<int,int>>();
    8.  
    9.     public static bool saveGame = false;
    10.  
    11.     void Awake () {
    12.  
    13.         // Load the info for playerData
    14.  
    15.     }
    16.  
    17.     void Update () {
    18.         if(saveGame){
    19.  
    20.             // Save the info for playerData
    21.  
    22.             saveGame = false;
    23.         }
    24.     }
    25. }
    26.  
     
    Last edited: Apr 6, 2015
  2. farhanblu

    farhanblu

    Joined:
    Dec 29, 2014
    Posts:
    49
    For the purpose of storing primitive variables, you might want to use PlayerPrefs.
     
  3. Dilanation

    Dilanation

    Joined:
    Apr 6, 2015
    Posts:
    2
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. using System.Linq;
    6.  
    7. public class gameData : MonoBehaviour {
    8.  
    9.     public static Dictionary<Vector3, Dictionary<int,int>> playerData = new Dictionary<Vector3, Dictionary<int,int>>();
    10.     public static bool saveGame = false;
    11.     string playerSave;
    12.  
    13.     void Awake () {
    14.         playerSave = PlayerPrefs.GetString("gameFile").ToDictionary(v => v, v => v);
    15.     }
    16.  
    17.     void Update () {
    18.         if(saveGame){
    19.             saveGame = false;
    20.             playerSave = playerData.ToString();
    21.             PlayerPrefs.SetString("gameFile", playerSave);
    22.             PlayerPrefs.Save();
    23.         }
    24.     }
    25.  
    26. }
    How would I put this into PlayerPrefs? I tried using .ToString to change it to string and save it as string and .ToDictionary to change it back to dictionary but I can't get the .ToDictionary to work correctly. The playerData variable will aslo get very large and this website http://www.dotnetperls.com/todictionary says that .ToDictionary is very slow.
     
    Last edited: Apr 6, 2015
  4. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    US already stores data into PlayerPrefs.

    Take a look at the LevelSerializer.cs script. It should contain a function that lets you serialize one GameObject tree. Go from there.