Class BrushUserToolState<TBrushTarget, TSettings>
Base class for brush user tool states that manage configurable settings and target objects, enabling persistent storage and retrieval of user preferences for brush tools.
Implements
Inherited Members
Namespace: Fwt.HexTerrains.UserTools.SM
Assembly: fwt.hexterrains.dll
Syntax
public abstract class BrushUserToolState<TBrushTarget, TSettings> : BrushUserToolState<TBrushTarget>, ISmState where TBrushTarget : class
Type Parameters
| Name | Description |
|---|---|
| TBrushTarget | The type of the object that the brush tool operates on. Must be a reference type. |
| TSettings | The type representing the settings or configuration data for the brush tool. |
Remarks
This class extends brush tool state functionality by supporting customizable settings that can be saved and loaded, typically using Unity's PlayerPrefs system. It is intended to be subclassed for specific brush tool implementations that require user-configurable options. Settings are automatically initialized and persisted when the tool is started or stopped.
Constructors
BrushUserToolState(ISmState)
Declaration
protected BrushUserToolState(ISmState parent)
Parameters
| Type | Name | Description |
|---|---|---|
| ISmState | parent |
Properties
DataSource
The data source containing the settings for the brush user tool.
Declaration
[SerializeField]
public virtual TSettings DataSource { get; set; }
Property Value
| Type | Description |
|---|---|
| TSettings |
SettingsPlayerPrefsKeyName
Gets the key name used to store settings in PlayerPrefs for this instance.
Declaration
protected virtual string SettingsPlayerPrefsKeyName { get; }
Property Value
| Type | Description |
|---|---|
| string |
Remarks
The default implementation returns the fully qualified type name of the instance. Override this property to customize the key name if multiple instances require distinct storage or to avoid key collisions.
Methods
InitDefaultSettings()
Initializes the default settings for the current instance. This method is intended to be overridden in derived classes to configure initial values or options as needed.
Declaration
protected virtual void InitDefaultSettings()
Remarks
Override this method in a subclass to provide custom initialization logic for default settings. The base implementation does not perform any actions.
InitSettings()
Initializes the settings for the brush user tool by setting default values and loading
Declaration
protected virtual void InitSettings()
LoadSettings()
Loads configuration settings for the current instance. Derived classes override this method to implement custom settings initialization.
Declaration
protected virtual void LoadSettings()
Remarks
Override this method in a subclass to provide specific logic for loading or initializing settings. This method is called during the setup or initialization phase and does not perform any actions in the base implementation.
LoadSettingsValueOrDefault<TValue>(string, TValue)
Retrieves the value of the specified setting if it exists; otherwise, returns the provided default value.
Declaration
protected virtual TValue LoadSettingsValueOrDefault<TValue>(string valueName, TValue defaultValue = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | valueName | The name of the setting to load. |
| TValue | defaultValue | The value to return if the setting is not found. If not specified, the default value for |
Returns
| Type | Description |
|---|---|
| TValue | The value of the setting if it exists; otherwise, the specified default value. |
Type Parameters
| Name | Description |
|---|---|
| TValue | The type of the setting value to retrieve. |
LoadSettingsValue<TValue>(string, TValue)
Retrieves a settings value from persistent storage using the specified key and type.
Declaration
protected virtual TValue LoadSettingsValue<TValue>(string valueName, TValue value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | valueName | The name of the settings value to load. This is used as the key in persistent storage. |
| TValue | value | The default value to return if the settings value is not found in persistent storage. |
Returns
| Type | Description |
|---|---|
| TValue | The value associated with the specified key, converted to the requested type. If the key does not exist, the default value is returned. |
Type Parameters
| Name | Description |
|---|---|
| TValue | The type of the value to retrieve from persistent storage. |
Remarks
Supported types include int, float, string, bool, and types that can be deserialized from JSON. For unsupported types, the value is loaded using JSON deserialization. The method is virtual and can be overridden to customize value retrieval.
PrepareToRun()
Performs any necessary initialization or setup required before executing the main operation. Called by state machine once when state becomes active. If state is disabled and then enabled again, this method will be called again.
Declaration
public override void PrepareToRun()
Overrides
Remarks
Override this method to implement preparation logic specific to the derived class. This method should be called prior to running the core functionality to ensure all prerequisites are met.
PrepareToStop()
Performs any necessary actions to prepare the object for stopping or shutdown. Called when the state is about to be deactivated.
Declaration
public override void PrepareToStop()
Overrides
Remarks
Implementations should ensure that all resources are released and any ongoing operations are safely terminated before the object is stopped. This method is typically called prior to disposing or shutting down the object to ensure a clean transition.
SaveSettings()
Saves the current settings to the underlying storage or configuration source.
Declaration
protected virtual void SaveSettings()
Remarks
Override this method in a derived class to implement custom logic for persisting settings. This method does not perform any action in the base implementation.
SaveSettingsValue<TValue>(string, TValue)
Saves a settings value with the specified name and value to persistent storage. The value is stored using a format appropriate for its type.
Declaration
protected virtual void SaveSettingsValue<TValue>(string valueName, TValue value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | valueName | The name of the setting to save. This is used as the key in persistent storage and must not be null or empty. |
| TValue | value | The value to save for the specified setting. The value's type determines how it is stored. |
Type Parameters
| Name | Description |
|---|---|
| TValue | The type of the value to be saved. Supported types include int, float, string, bool, and serializable objects. |
Remarks
For supported primitive types (int, float, string, bool), the value is stored directly. For other types, the value is serialized to JSON before being saved. If the value type is not serializable, saving may fail. This method is virtual and can be overridden to customize storage behavior.
TryLoadSettingsValue<TValue>(string, out TValue)
Attempts to load a settings value of the specified type from persistent storage.
Declaration
protected virtual bool TryLoadSettingsValue<TValue>(string valueName, out TValue value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | valueName | The name of the settings value to retrieve. |
| TValue | value | When this method returns, contains the loaded value if found; otherwise, the default value for the type. |
Returns
| Type | Description |
|---|---|
| bool | true if the value was found and loaded successfully; otherwise, false. |
Type Parameters
| Name | Description |
|---|---|
| TValue | The type of the value to load from settings. Supported types include int, float, string, bool, and types that can be deserialized from JSON. |
Remarks
This method checks for the existence of the specified settings value in persistent storage and attempts to load it as the requested type. For types other than int, float, string, or bool, the value is expected to be stored as a JSON string and will be deserialized. If the value does not exist or cannot be loaded, the out parameter is set to the default value for the type.