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. Dismiss Notice

Scriptable objects, can I use them to save/load while the game goes?

Discussion in 'Scripting' started by Deleted User, Feb 5, 2016.

  1. Deleted User

    Deleted User

    Guest

    I've been trying to understand how to use scriptable objects correctly and efficiently, and when i thought I understand something I've overcame one problem that bothers me. So when I have e.g. class that contains players health, exprience etc. I want to make some funcion that saves it. I came easy for me to create a scriptable object that stores this information, and then I've created a saving and loading methods, where I use AssetDatabaseto create an asset and to load it. And it worked well, but... AssetDatabase is part of UnityEditor namespace, which cannot be included in final build (that's what I've managed to establish so far). So my question is; is there any possibility to use scriptable objects to save and load such data while the game goes, or they are just not for that and the best way to store this data is storing it as a binary file using c# serializaion?

    Here is how i do it:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Simple_class {
    6.     public int simple_int;
    7.     public string simple_string;
    8. }
    9.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class Simple_scriptable_obj : ScriptableObject {
    5.     public List <Simple_class> simple_list_to_store_data = new List <Simple_class>();
    6.  
    7. }
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections.Generic;
    4.  
    5. public class Game_manager : MonoBehaviour {
    6.     public List <Simple_class> simple_list = new List <Simple_class>();
    7.  
    8.     void Update () {
    9.         if (Input.GetKeyDown (KeyCode.F5))
    10.             Save ();
    11.         if (Input.GetKeyDown (KeyCode.F9))
    12.             Load ();
    13.     }
    14.     public void Save () {
    15.         Simple_scriptable_obj savedData = ScriptableObject.CreateInstance <Simple_scriptable_obj> ();
    16.         savedData.simple_list_to_store_data = simple_list;
    17.         AssetDatabase.CreateAsset (savedData, "Assets/savedData.asset");
    18.         AssetDatabase.SaveAssets ();
    19.         AssetDatabase.Refresh ();
    20.     }
    21.     public void Load () {
    22.         Simple_scriptable_obj loadedData = AssetDatabase.LoadAssetAtPath <Simple_scriptable_obj> ("Assets/savedData.asset");
    23.         simple_list = loadedData.simple_list_to_store_data;
    24.     }
    25. }
    26.  
    And as i said it wors ok in editor; I can save all changes by pressing F5 and load them with F9 but i cannot build the game, because of using UnityEditor...
     
  2. sloopidoopi

    sloopidoopi

    Joined:
    Jan 2, 2010
    Posts:
    244
    Deleted User likes this.
  3. Deleted User

    Deleted User

    Guest

  4. netlander

    netlander

    Joined:
    Nov 22, 2011
    Posts:
    28
    You can serialise Scriptable Objects: