• Manual
  • API Documentation
  • Guides
  • Saving and Loading Readonly Properties

    Show / Hide Table of Contents
    • Introduction
    • Installation
    • Getting Started
    • Demos
    • The Save System API
    • Auto Save
    • Platforms
    • Security
    • Serialization
    • Supported Data Types
    • Migration
    • FAQ
    • Changelog
    • Extend
      • Overview
      • Converter
      • Encryption
      • Storage
    • Guides
      • Overview
      • Configuring Settings
      • Using Auto Save
      • Basic Saving and Loading
      • Saving and Loading Raw Data
      • Deleting the Data
      • Saving and Loading Images
      • Saving and Loading Scene Objects
      • Saving and Loading Assets
      • Saving and Loading Readonly Properties
    • Integrations
      • Overview
      • Google Play Games
      • Xbox Live
      • Bolt
      • Firebase
      • PlayFab
      • PlayMaker
      • Steam Auto-Cloud
      • Steamworks .NET
      • Facepunch Steamworks
      • Text Mesh Pro
    • Storage
      • Overview
      • Backup
      • Catalog
      • Meta Data
      • Built-in Storages
        • File
        • PlayerPrefs
        • Memory
      • Third-party Storages
        • Firebase
          • Realtime Database
          • Cloud Storage
          • Firestore
        • PlayFab
          • Entity Objects
          • Entity Files
          • User Data
        • Steam
          • Steamworks .NET
          • Facepunch Steamworks
        • Google Play
          • Saved Game
        • Xbox Live
          • Connected Storage

    Saving and Loading Readonly Properties

    Each readonly property requires a constructor parameter with the same name in order to be saved and loaded.

    For example, in the below script, we have a class called ReadonlyClass, it has a readonly property called myReadonlyProperty:

    public class ReadonlyClass {
    
        private readonly string myReadonlyProperty;
    
    }
    

    But after a save and load attempt, you'll notice that the myReadonlyProperty is saved but it is not loaded, that's because we don't have any constructors that accepts the readonly property as an parameter in order to assign it, so to make it work, we have to add a constructor to our class:

    public class ReadonlyClass {
    
        private readonly string myReadonlyProperty;
    
        public ReadonlyClass(string myReadonlyProperty) {
            this.myReadonlyProperty = myReadonlyProperty;
        }
    
    }
    

    Now the readonly property will be saved and loaded properly.

    Note

    The name of the constructor parameter should be the same as the name of the readonly property for matching the parameter position and its name in serialization process.

    • Improve this Doc
    • 0 Comments