MCTG
MCTG

Reputation: 61

Save preferences and use them in other platform

I wanted to ask if it's possible to save preferences in unity editor and use them when project is built and access them from other platform.

For example, I have set n=5 then saved it using playerprefs in unity editor and then I want to get the same integer on other platform when the project is build.

Is there any possible way to achieve this type of problem?

Upvotes: 0

Views: 308

Answers (2)

YAC
YAC

Reputation: 435

Sounds like what you want is turning your data (the value of n in your example) into assets in your builds.
PlayerPrefs does not fit your need because it does not create assets at the build time that could be loaded back during run time. Instead, it is used to serialize data generated during run time to users' devices and use across application sessions (by session I mean the time from launch to quit an application).

My suggestions would be finding someway to serialize your data into assets that Unity can read. There are at least two ways of doing such thing:

  1. Using ScriptableObject. ScriptableObject is a class that inherits from UnityEngine.Object that is designed for serializing data as assets. Here is a simple example:

    [CreateAssetMenu("my_data.asset", "My Data")]
    public class Data : ScriptableObject
    {
        [SerializeField]
        private int _n;
    
        [SerializeField]
        private string _someString;
    
        public int N { get { return _n; }}
        public string SomeString { get { return _someString; }}
    }
    

    The normal usage would be defining your data class , say Data, that inherits from ScriptableObject. Then, you put desired data into it as serialized fields (_n and _someString here). The attribute CreateAssetMenu is used to create a menu item in the Unity editor for you to create the asset.
    After doing this, you can use the data in your MonoBehaviour by referencing it through a field marked with [SerializeField] just like any other Unity assets (like materials or sprites):

    public class SomeMono : MonoBehaviour
    {
        [SerializeField]
        private Data _data;
    
        void Awake()
        {
            Debug.Log("Data.SomeString is " + _data.SomeString);
        }
    }
    
  2. Serialize the data into your own format (JSON, BSON, XML, whatever you like) and save them and read back as TextAsset. Here is an example:

    public class SomeMono2 : MonoBehaviour
    {
        [SerializeField]
        private TextAsset _text;
    
        void Awake()
        {
            // Assumes that the data is just a plain string sequence.
            // However, it could be any text data you like.
            Debug.Log(_text.text);
            // Or you can serialize your data as bytes
            // byte[] byteData = _text.bytes
        }
    }
    

    Please note that Unity only automatically identifies files with extension txt (and maybe json? not quite sure about this one) as text assets.

Upvotes: 1

Brandon Miller
Brandon Miller

Reputation: 1584

Sounds like you'd be better off using JSON and serializing your data. In the desktop, Unity saves PlayerPrefs to the registry. In Android, it is saved to a manifest file. So, in other words.. No, PlayerPrefs is not a cross-platform kinda thing. Furthermore, if this is "save-game" data, you should know that PlayerPrefs is used to store simple data like an Options menu. For saving an entire save game file you are going to want to use something a little more robust.

Upvotes: 2

Related Questions