Currency System

Documentation · Get it on the Unity Asset Store

Settings

System will automatically generate CurrencySettings scriptable object under Assets/Resources. Do not move it. Even if it is moved, system will generate a new one there and use it.

Currency System settings inspector

1. Currencies

Here you need to define your currencies.

2. Keys

These keys are used to store currencies' data. By default, it doesn't need a configuration and data is stored at PlayerPrefs. If any other method is needed, you need to implement an IRepository that suits your needs. Only then you may need to change these keys to match your services' ids or keys.

3. Custom


API Usage

// To get currency
int goldAmount = CurrencyManager.Get("Gold");

// To set currency
CurrencyManager.Set("Gold", 100);

// To add currency
int newTotalAfterAddition = CurrencyManager.Add("Gold", 100);

// To remove currency
int newTotalAfterRemoval = CurrencyManager.Remove("Gold", 100);

// To check if has enough
bool hasEnough = CurrencyManager.Has("Gold", 100);

// To delete key data
CurrencyManager.Clear("Gold");

// To get capacity
int keyCapacity = CurrencyManager.GetCapacity("Key");

// To set capacity
CurrencyManager.SetCapacity("Key", 10);

// To check if currency has capacity option
bool hasGoldCapacity = CurrencyManager.HasCapacity("Gold"); // false
bool hasKeyCapacity = CurrencyManager.HasCapacity("Key"); // true


// Events
// Triggered on any currency amount change
CurrencyManager.OnAmountChanged += (type, currentAmount, changeAmount) => {
    // Do something
};

// Triggered on any currency capacity change
CurrencyManager.OnCapacityChanged += (type, capacity) => {
    // Do something
};
// Don't forget to unsubscribe

Components

1. Currency Button

Currency Button component

Its state is changed automatically on currency amounts change.

Settings:

Resources:

Events:

2. Currency Collectable

Currency Collectable component

3D version is identical to 2D. The only difference is the collider type it needs.

Settings:

Events:

3. Currency Label

Currency Label component

There are two ready implementations for:

New ones can be implemented via ICurrencyLabel.

Settings:


Implementing Custom Repository

You just need to implement this simple interface and change Repository Class to it at the settings.

namespace GO
{
    public interface IRepository
    {
        void Set(string type, int value);
        int Get(string type, int defaultValue);
        void Clear(string type);
    }
}