This library provides a very simple Key Value Store (not Server) for Unity. This KVS is helpful for the following purposes.
- Transferring data between scenes
- Save and load the data
- Very simple API and implementation
- Easy integration with external serializers
- Integration with two internal serializers and six external serializers by default
- JsonUtility
- XmlSerializer
- Json.net
- System.Text.Json
- NetSerializer
- Protobuf-net
- MessagePack
- MemoryPack
There are two ways to install the package: using the UPM (Unity Package Manager) or importing the .unitypackage.
- Open the Package Manager window.
- Click the add (+) button in the status bar.
- Select Add package from git URL.
- Enter "
https://github.com/kumaS-nu/Simple-KVS.git?path=Simple-KVS/Packages/Simple KVS
" or "git@github.com:kumaS-nu/Simple-KVS.git?path=Simple-KVS/Packages/Simple KVS
". - Click Add.
For more information, see the Official page (Installing from a Git URL - Unity).
- If you have not installed OpenUPM-CLI, run the following command to install OpenUPM-CLI. (Node.js 12 is required.)
npm install -g openupm-cli
- Go to the unity project folder
- Run the following command to install "NuGet importer for Unity" in your project.
openupm add org.kumas.simple-kvs
For more information, see the Official page (Getting Started with OpenUPM-CLI - OpenUPM)
This KVS has InMemoryKVS<T>
, InMemoryKVS4UnityObject<T>
, InFileKVS<S>
and InFileKVS4UnityObject<S>
.
Store data in memory. This class cannot store the data inherited from Unity.Object
(such as MonoBehaviour
). Instead, use InMemoryKVS4UnityObject<T>
for these data.
var data = new Data { someData = "someData" };
// Store the data in memory. Keys are recorded by each type.
InMemoryKVS<Data>.Set("data_key", data);
// Get the data from memory. Note that data is deleted in memory when getting data by default.
var received = InMemoryKVS<Data>.Get("data_key");
// Of course, you can also use TryGet().
var (isStored, value) = InMemoryKVS<Data>.TryGet("data_key");
InMemoryKVS4UnityObject<T>
is almost the same as InMemoryKVS<T>.
However, the storing data must implement ISerializable.
class Sample : MonoBehaviour, ISerializable
{
private string someData;
public object Serialize()
{
// Serialize some data that does not inherit Unity.Object.
}
public void Deserialize(object data)
{
// Apply the stored data to this instance.
}
}
// Store the data in memory. Keys are recorded by each type.
InMemoryKVS4Unity<Sample>.Set("data_key", data);
var rcv = gameObject.GetComponent<Sample>();
// Get the data from memory. Note that data is deleted in memory when getting data by default.
InMemoryKVS4UnityObject<Sample>.Get("data_key", rcv);
// Of course, you can also use TryGet().
var isStored = InMemoryKVS4UnityObject<Sample>.TryGet("data_key", rcv);
Save the data to a file. The generics type specifies the serializer to use and must inherit from ISerializer
. InFileKVS<S>
cannot store the data inherited from Unity.Object
(such as MonoBehaviour
). Instead, use InFileKVS4UnityObject<S>
for these data.
var data = new Data { someData = "someData" };
// Save the data in file. Keys are recorded by each type.
InFileKVS<JsonUtilitySerializer>.Set<Data>("data_key", data);
// Load data from file.
var received = InFileKVS<JsonUtilitySerializer>.Get<Data>("data_key");
// Of course, you can also use TryGet().
var (isStored, value) = InFileKVS<JsonUtilitySerializer>.TryGet<Data>("data_key");
// Asynchronous API is also available.
var received2 = await InFileKVS<JsonUtilitySerializer>.GetAsync<Data>("data_key");
InFileKVS4UnityObject<S>
is almost the same as InFileKVS<S>
. The generics type specifies the serializer to use and must inherit from ISerializer4UnityObject
.
var data = gameObject.GetComponent<MonoData>();
// Save the data in file. Keys are recorded by each type.
InFileKVS4UnityObject<JsonUtilitySerializer>.Set<Data>("data_key", data);
var rcv = gameObject.GetComponent<MonoData>();
// Load data from file.
InFileKVS4UnityObject<JsonUtilitySerializer>.Get<Data>("data_key", rcv);
// Of course, you can also use TryGet().
var isStored = InMemoryKVS<JsonUtilitySerializer>.TryGet<Data>("data_key", rcv);
// Asynchronous API is also available.
await InMemoryKVS<JsonUtilitySerializer>.GetAsync<Data>("data_key", rcv);
For more information or contributions, please see https://kumaS-nu.github.io/Simple-KVS .
This package is under MIT License.