Public Properties Class?

In general when I write games/apps (.net) I make a global storage class that is just a collection of public properties and I seralize the class so I can save the data locally. Anyway, I googled around and could not find anyone using this practice to store their data. I’m very new to Unity still so it possible to build a class/script and store information in such a fashion? Thanks in advance.

Here is a sample, can I do this?

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

[System.Serializable]
public class DataStorage : MonoBehaviour
{
    private string _Name;
    private float _Health;
    private float _Damage;
    private float _Armor;

    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

    public float Health
    {
        get { return _Health; }
        set { _Health = value; }
    }

    public float Damage
    {
        get { return _Damage; }
        set { _Damage = value; }
    }

    public float Armor
    {
        get { return _Armor; }
        set { _Armor = value; }
    }
}

Can you, yes. Should you…Well, that might be a different question. There are a bunch of different ways to store and save data, so what works for your game may be different than what I might choose to use.

Otherwise, there isn’t anything wrong with what you have. Just note as a monobehaviour, it becomes a component that will be added to a gameobject (which may be what you want) vs just being a class that holds data and created with the word new.

1 Like

Hmm well is there a best practice for saving data in mono? I’m coming from .Net so I don’t know whats best practice in Mono.

He was just referring to inheriting from MonoBehaviour, and saying it may or may not be what you want. :slight_smile:

As in, if it’s just data, you can store that in a regular C# script, without inheriting*.

1 Like

I do something very similar. I just do it on a vanilla C# class. I also don’t bother with properties unless they are doing something. (In games I default to the most performant code, unless I have a reason to do otherwise.)

I then run the entire thing through the JSON utility and dump it in a text file. It makes for a very simple and straight forward save system.

1 Like

Why would you write it to Json then convert to plain text? You can just write it to plain text without converting to Json. Not sure I understand why your converting twice? Sorry Im newb just trying to understand your logic. I’m sure their is a good reason.

Normally with json you have a class that holds your data, so you serialize that into a string and write that to a text file. Then when you read it in, you read in the text and deserialize it back into a variable that is the type of that class. This is normal. Otherwise, you have to write your own parser to pull values out or use something that can create a jsonobject (which I think is slower to access the data).

We do similar stuff for some of our game data as well.

Basically, the json step converts the data into the string. Json itself is text really. So he’s not converting it twice. He converts once to a string and then writes it out.

hmm interesting concept. Thank you for sharing and explaining.

The reason I use JSON instead of serialising directly is that JSONUtilty uses Unitys built in serialiser. This has a few advantages.

  • It can serialise references to assets robustly. So if I change an asset name in the editor, it will still work.
  • It’s robust against mild changes to the class structure.
  • It behaves the same way as the unity inspector, which means I have a pretty decent idea of exactly how and what will and won’t serialise. And I know when it’s going to throw a fit.
  • JSON is human readable. Which is brilliant for debugging. It also gives you a head start if you decide to support modding.
  • I don’t have to write the serialiser.

Now in fairness, my experience is only in Unity, I’ve never worked in a regular C# environment. How would you normally serialise a class? Before JSONUtility was a thing I would use a binary formatter. But I wasn’t super fond of that approach.