Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I created a system which makes saving variables more convenient, with a custom save-file format

Discussion in 'Assets and Asset Store' started by Vrroom, Oct 30, 2022.

?

Do you want this?

  1. Yes!

    0 vote(s)
    0.0%
  2. Yes, I'd even pay for it!

    0 vote(s)
    0.0%
  3. No, are you mad?

    0 vote(s)
    0.0%
  4. **Throws boxers at screen**

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. Vrroom

    Vrroom

    Joined:
    Nov 15, 2016
    Posts:
    1
    Hi All!

    I created a system which makes saving variables more convenient, with a custom save-file format.

    Initially, I was working on an idle-game template. I figured I needed a convenient way of saving (lots of) variables.

    With that in mind, I created the .save file format. I'm sure it's not the most unique file-structure, but it works AND it's pretty small.

    This system would allow the user to save/load any variable (supported types below) by simply adding the [Saveable] attribute. The system takes care of the rest.It can save/load public and private fields, without having to predefine all variables you want to add to the save file.

    Current supported types:

    bool
    float
    int
    string
    Vector3
    Vector4

    Let me know if you're interested in such a system; With enough interest I'll finish it asap and publish it ;)

    Attached is an image of the save-file and the usage.
     

    Attached Files:

  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,533
    There are already plenty of viable solutions built-in or available as add-ons.
    • PlayerPrefs
    • JsonUtility
    • ScriptableObjects (read-only at runtime but values could be saved as Json)
    • ...
    Rather than having a couple fields, instead I would make a struct or class with savable fields such that you can save them all at once and you don't need any filtering attributes. Example code (incomplete):

    Code (CSharp):
    1. public class MyClass : MonoBehaviour
    2. {
    3.     [Serializable]
    4.     public struct Saveables
    5.     {
    6.         public int aValue;
    7.         public float anotherValue;
    8.         public string someText;
    9.         public bool thatFlag;
    10.         public Vector3 thePosition;
    11.         public Quaternion theRotation;
    12.         public Color32 myColor;
    13.         public byte[] gridCoords;
    14.        // etc etc
    15.     }
    16.  
    17.     private Saveables _saveables = new();
    18.  
    19.     private void OnDestroy()
    20.     {
    21.         File.WriteAllText(path, JsonUtility.ToJson(_saveables));
    22.     }
    23. }
    24.  
    This supports all Serializable types, including nested types and their fields.