Home > Net >  How to store information offline. C#, Unity
How to store information offline. C#, Unity

Time:09-05

I'm working on an application in unity that solves chemical problems. I need to store information about each chemical element offline. For example: hydrogen [mass 1, group 1...], oxygen[mass 16, group 6...] and so on. What do I need to use?

CodePudding user response:

ScriptableObjects are a great fit for this situation:

[CreateAssetMenu]
public class Element : ScriptableObject
{
    [SerializeField]
    private int mass;

    [SerializeField]
    private int group;

    public int Mass => mass;
    public int Group => group;
}

You can create an asset to hold information about each element.

CodePudding user response:

The probably simplest solution would be to use a serialization library, like json .net, these can convert your objects to a serialized stream that can be saved to file. Attributes can typically be used to control how the object will be serialized.

The other major option is to use a database, either a stand-alone database like postgres, or a in-process database like sqlite. The later makes things like deployment easier, but introduces some limitations, like not supporting multiple concurrent applications. In either case you would typically use an "Object Relational Mapper" (ORM), like Entity Framework. This is able to convert your objects directly to database tables.

Files are typically simpler to use, and suitable if you want to store few,larger blobs of data that rarely change. Databases are more suitable if you have many more smaller objects that you want to search among, or when persisting data more frequently.

Note that this is general advice, Unity might have some built in persistence that might or might not be suitable for your particular case.

  • Related