Creating your own connectors
This package provides you some premade storage connectors, but you might want to create your own custom storage connectors. You can create your own connectors easily.
1. Create your connector script
Create a new .cs
file. If you want to follow the official naming strategy you should name it <<Custom-name>>StorageConnector.cs
(for example: FancyStorageConnector.cs
, AwesomeStorageConnector
, SuperFantasticStorageConnector
).
2. Extend it with the AStorageConnector
class
Extend your connector class with the AStorageConnector
abstract class. It will require you to implement some methods.
Example
public class FancyStorageConnector : AStorageConnector
{
public FancyStorageConnector(string serviceName) : base(serviceName) { }
public override void Write(string key, string value) => /* Write logic */;
public override string ReadString(string key) => /* Read logic (be aware: you must return null if the value is not present) */;
public override void Clear(string key) => /* Remove key logic */;
public override bool HasKey(string key) => /* Keys check logic */;
public override void Import(string value) => /* Imports a JSON string */;
public override string Export() => /* Exports data as a JSON string */;
}
When you develop a custom Storage Connector you have to keep in mind that depending on the serviceName and the slot you will need to operate with different data.
See this example (based on the PlayerprefsStorageConnector.cs
):
public class FancyStorageConnector : AStorageConnector
{
public FancyStorageConnector(string serviceName) : base(serviceName) { }
/* ... other overrided methods ... */
public override void Write(string key, string value) {
/* You can access GetSlot and serviceName as they are inherited from AStorageConnector */
string slot = base.GetSlot();
string path = base.serviceName + "-" + slot + "-" + key; // We generate the path to the PlayerPrefs item combining the serviceName, slot and the key.
PlayerPrefs.SetString(path, JsonConvert.SerializeObject(data));
}
/* ... other overrided methods ... */
}
3. Storing different datatypes
All storage connectors allow us to use different datatypes. You can perform specific datatype operations by using the corresponding method. For example: if you want to read a float
value you should use the ReadFloat
method (when writing, type is automatically inferred, so you would use Write
as well).
In the example you can see that only string
methods are defined. This does not mean that you cannot use other datatypes, because the other methods are automatically generated by the AStorageConnector
class. All other methods by default are extensions of the string
methods (they perform I/O operations using the string
methods and casting values to the desired datatype).
If you don't want the class to manage the other datatypes, you can override those methods. See here the list of methods that can be overrided.
Read method | Write method |
---|---|
string ReadString(string key) | void Write(string key, string value) |
int ReadInt(string key) | void Write(string key, int value) |
float ReadFloat(string key) | void Write(string key, float value) |
bool ReadBool(string key) | void Write(string key, bool value) |
T ReadObject<T>(string key) where T : class | void WriteObject<T>(string key, T value) where T : class |