Skip to content

Quick Start

This guide provides step-by-step instructions for configuring Cobra Settings in Unity, allowing you to manage game or app settings effectively through Google Drive or local files. Ensure that the Cobra Settings asset is installed in your Unity project before proceeding.

Preparing Your Settings

1. Define Your Settings Classes

Create Plain Old C# Object (POCO) classes for your settings. These classes will store and manage your settings data. You can create as many classes as needed.

Example Settings Class
namespace ByteCobra.Settings.Examples
{
    public class DifficultySettings
    {
        public string SceneName { get; set; }
        public int Difficulty { get; set; }
    }
}

2. Create Your Settings JSON File

Construct a JSON file based on your defined classes. This file can be uploaded to Google Drive or saved locally within your project or build.

Example JSON File

{
    "DifficultySettings": {
            "SceneName": "Mountain Climb",
            "Difficulty": 4
        }
}

3. Configure File Access

  • For Google Drive: Make the file public and copy its sharing URL.
  • For Local Files: Note the relative path to the file within your project.

Integration in Unity

  • Open your Unity project and navigate to Assets/Byte Cobra/Settings/Resources/AppSettings.
  • In the Unity Editor, locate the AppSettingsObject Inspector.

For Google Drive:

  • Paste the sharing URL from Google Drive into the Source field.
  • Set the StorageType to GoogleDrive in the AppSettings scriptable object inspector.

For Local Files:

  • Enter the relative path to your local JSON settings file in the Source field.
  • Choose LocalFile as the StorageType in the AppSettings scriptable object inspector.

Optional: Insert a JSON string in the Fallback Settings field to serve as a backup configuration if the primary source fails.

Loading Settings

Here's an example of how you can load settings in your own scripts:

private void Awake()
{
    DifficultySettings difficultySettings = AppSettings.Read<DifficultySettings>();
}

This will by default try to read the value of the key with the same name as the class name (DifficultySettings) so your JSON must have the same key. It is possible to override the key name if you have to.

For more complex JSON structures, like separate keys for different difficulty levels:

{
    "Easy": [
        {
            "SceneName": "Forest Adventure",
            "Difficulty": 3
        },
        {
            "SceneName": "Desert Run",
            "Difficulty": 2
        },
        {
            "SceneName": "Mountain Climb",
            "Difficulty": 4
        }
    ],

    "Hard": [
        {
            "SceneName": "Forest Adventure",
            "Difficulty": 3
        },
        {
            "SceneName": "Desert Run",
            "Difficulty": 2
        },
        {
            "SceneName": "Mountain Climb",
            "Difficulty": 4
        }
    ],
}

Load them like this:

private async void Awake()
{
    List<DifficultySettings> easySettings = 
        AppSettings.Read<List<DifficultySettings>>("Easy");

    List<DifficultySettings> hardSettings = 
        AppSettings.Read<List<DifficultySettings>>("Hard");
}

Follow these instructions to integrate Cobra Settings with your Unity project, enabling efficient management of settings via Google Drive or local files.