πŸ’°Cash Manager

Used to perform player in-game cash/money operations

How to use

add using Game.Core;

By default there CashManager prefab replaced only in _Loader scene, but it will be auto-created if it's null, because it's MonoSingleton inherited.

You can change runtime cash by it's inspector buttons and cash amount field:

Inspector available operations on CashManager object

Add Or Remove Cash Amount EDITOR is value to add / spend; Editor Currency what currency to add / spend; Delete cash data button resets all currencies to default value; Log local manager currency values displays all currencies values for test purposes.

How add more currencies

Open CashManager.cs and there you will find Currencies enum. You need to add new value enam here:

Added Keys as currency example

If you want to define default non-zero value -> go to Assets/_Main/Configs/Currencies Settings asset and set default value:

SaveData default Cash and Hard values both as 0

In config above you can set "Start Value" and "Icon" for each currency. Sprite in "Icon" is used by CashValueUpdater if it has "icon" reference in UI.

API

/// <summary>
/// arg1 is new cash amount<br>
/// arg2 is cash delta change</br>
/// arg3 is what currency to update
/// </summary>
public static UnityEvent<float, Currencies> OnCurrencyUpdated = new();

public float GetAmount(Currencies currency);

/// <summary>
/// Write all currency values to save data
/// </summary>
public void SaveStates();

/// <summary>
/// Back all currency values to save data values
/// </summary>
public void ResetStates();

/// <summary>
/// Set target currency with start value
/// </summary>
public void SetCurrencyStartValue(Currencies currency, bool save);

/// <summary>
/// Set target value for currency
/// </summary>
public void SetCurrencyValue(Currencies currency, float value, bool save);

/// <summary>
/// Get clean copy of all currencies of the game (with target default value 0 as default)
/// </summary>
public Dictionary<Currencies, float> GetAllCurrenciesDictionaryCleanCopy(float defaultValue = 0f);

public float GetAmount(Currencies currency);

/// <summary>
/// Note: it doesnt write to save data, if you need use <see cref="AddAndSave(float, Currencies)"/>
/// </summary>
public void Add(float amount, Currencies currency);
public void AddAndSave(float amount, Currencies currency);
/// <summary>
/// Adds cash by currency, doesn't invoke update event (UI), but changes CashManager currency value
/// </summary>
/// <param name="withSave">also write transaction to save data so action will be saved</param>
public void AddWithoutUpdateEvent(float amount, Currencies currency, bool withSave = true);

/// <summary>
/// Note: it doesnt write to save data, if you need use <see cref="SpendAndSave(float, Currencies)"/>
/// </summary>
public void Spend(float amount, Currencies currency);
public void SpendAndSave(float amount, Currencies currency);

public bool IsEnough(float forAmount, Currencies currency);
/// <param name="withSave">also write transaction to save data so action will be saved</param>
public bool TrySpend(float amount, Currencies currency, bool withSave = true);

Now you need to implement functions call and event handle.

Examples of CashManager usage can be found in Assets/_Examples/Cash Manager/

Last updated