Do scriptable objects work on Android devices

739 Views Asked by At

I have created a simple scriptable object with a list, 1 button and something to the list, second load its into the text ui. When I test this on Unity, it works when I already build the app from the build settings, android on the other hand doesnt. Anyone know why, or does it even work on android? Thanks!

2 main scripts: https://drive.google.com/file/d/1QS9SjzEJ5BdIhu3PPpg-ePSqktm_6Um8/view?usp=sharing, https://drive.google.com/file/d/1T9iV_zK8NlvCI2jFkEEAafkrMFAsoFMa/view?usp=sharing EDIT: proof: https://drive.google.com/file/d/1rkN4W-u3G6-Uj7ReSkVRVtLKI4zgCwEh/view?usp=sharing mp4 file btw

1

There are 1 best solutions below

0
On

Scriptable Objects are platform-independent which means they work on any platform.

Here's what Unity's official Scriptable Object's documentation says:

When you use the Editor, you can save data to ScriptableObjects while editing and at run time because ScriptableObjects use the Editor namespace and Editor scripting. In a deployed build, however, you can’t use ScriptableObjects to save data, but you can use the saved data from the ScriptableObject Assets that you set up during development.

In order to save data to Scriptable Object (when you click on Save button in UI) you have to save the Scriptable Object to player prefs.

Something like this:

  1. create auxiliary class i.e SaveSystemData as a separate file and mark it as [Serializable] (must do this) and add lists from Scriptable object from it
    [Serializable]
    public sealed class SaveSystemData
    {
        public List<string> List1;
        public List<string> List2;
        public List<string> List3;
        public List<string> List4;
        public List<string> List5;
    }
  1. reference that class in your Scriptable object instead of lists
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName ="ScriptableSaver", menuName ="SaveSystem")]
public class SaveSystem : ScriptableObject
{
    public SaveSystemData saveSystemData;
}
  1. save SaveSystemData to JSON and put it into player prefs when you click on Save button in UI
private const string SAVED_DATA = "SavedData";
    
public void SaveGame()
{
    SOScript.saveSystemData.List1 = DictionaryWords;
    SOScript.saveSystemData.List2 = DictionaryMeaning;
    SOScript.saveSystemData.List3 = DictionaryInRussian;
    SOScript.saveSystemData.List4 = DictionaryAddInfo;
    SOScript.saveSystemData.List5 = DictionaryAVN;
    PlayerPrefs.SetString(SAVED_DATA, JsonUtility.ToJson(SOScript.saveSystemData))
}
  1. load SaveSystemData from JSON
public void LoadGame()
{
    if (PlayerPrefs.HasKey(SAVED_DATA))
    {
         SOScript.saveSystemData = JsonUtility.FromJson<SaveSystemData>(PlayerPrefs.GetString(SAVED_DATA));
     }
     DictionaryWords = SOScript.saveSystemData.List1;
     DictionaryMeaning = SOScript.saveSystemData.List2;
     DictionaryInRussian = SOScript.saveSystemData.List3;
     DictionaryAddInfo = SOScript.saveSystemData.List4;
     DictionaryAVN = SOScript.saveSystemData.List5;
}

That's it in general