I'm trying to create a simple new setting that can be altered by the player in a custom SettingsUI,
I'm not sure what the correct way to implement this is. The previous programmer working on this project simply made a custom global variable which kind of works, but then it requires saving the global variables/game state to save the settings, which isn't a desired behavior. So, I began investigating further and found that NaniNovel has 3 separate ways to save states, including a "Settings" state which saves into Settings.json and seems like the correct solution.
The problem I'm having is correctly getting my new custom Configuration to automatically serialize and correctly save into the Settings.json.
I read over https://naninovel.com/guide/state-management very thoroughly but the example code in there for serializing a custom Setting or Global seems incomplete and impossible to directly follow.
I'm also not positive how I'm supposed to access the data.
Here's what my code looks like:
Code: Select all
using Naninovel;
using UnityEngine;
[EditInProjectSettings]
[System.Serializable]
public class ColorTheme : Configuration
{
[System.Serializable]
public enum ColorThemeEnum
{
Dark,
Light
}
[Header("Color Theme Settings")]
[Tooltip("Selected color theme.")]
public ColorThemeEnum selectedTheme = ColorThemeEnum.Dark;
//Completely unsure of everything below this... just trying to figure out how to correctly copy https://naninovel.com/guide/state-management#custom-state
private IStateManager stateManager;
private void Awake()
{
stateManager = Engine.GetService<IStateManager>();
}
ColorTheme MyColorTheme
{
get => stateManager.SettingsState.GetState<ColorTheme>();
set => stateManager.SettingsState.SetState<ColorTheme>(value);
}
}
I'm attempting to access my custom settings like this:
Code: Select all
IStateManager stateManager = Engine.GetService<IStateManager>();
ColorTheme colorTheme = stateManager.SettingsState.GetState<ColorTheme>();
Debug.Log(colorTheme.selectedTheme);
However, I'm receiving a null reference exception because the GetState<ColorTheme> call is returning null
Can someone please help guide me. Ideally with a simplified example of how I'm supposed to do this.
Thank you!