call oninitialstate after state has been added to local buffer, add examples to quick-start docs
parent
4c359345be
commit
db4b0017f2
|
|
@ -1,6 +0,0 @@
|
||||||
---
|
|
||||||
title: Quick Start
|
|
||||||
---
|
|
||||||
|
|
||||||
1. [Install the package](/)
|
|
||||||
2.
|
|
||||||
|
|
@ -1,46 +1,8 @@
|
||||||
# VEL-Connect
|
# VEL-Connect
|
||||||
|
|
||||||
VEL-Connect is a collection of utility scripts useful for VR Unity projects.
|
VEL-Connect is a persistent shared storage mechanism for Unity projects. It can be used as a key-value store as a networked replacement for PlayerPrefs, to share a profile of user data across multiple devices, or to easily switch between different user profiles on a single device.
|
||||||
|
|
||||||
## Installation
|
Important pages:
|
||||||
|
|
||||||
Install the UPM package in Unity:
|
- [Installation](/installation)
|
||||||
|
- [Quick Start](/quick-start)
|
||||||
=== "**Option 1:** Add the VEL package registry"
|
|
||||||
|
|
||||||
{ align=right }
|
|
||||||
|
|
||||||
Using the scoped registry allows you to easily install a specific version of the package by using the Version History tab.
|
|
||||||
|
|
||||||
- In Unity, go to `Edit->Project Settings...->Package Manager`
|
|
||||||
- Under "Scoped Registries" click the + icon
|
|
||||||
- Add the following details, then click Apply
|
|
||||||
- Name: `VEL` (or anything you want)
|
|
||||||
- URL: `https://npm.ugavel.com`
|
|
||||||
- Scope(s): `edu.uga.engr.vel`
|
|
||||||
- Install the package:
|
|
||||||
- In the package manager, select `My Registries` from the dropdown
|
|
||||||
- Install the `VEL-Connect` package.
|
|
||||||
|
|
||||||
=== "**Option 2:** Add the package by git url"
|
|
||||||
|
|
||||||
1. Open the Package Manager in Unity with `Window->Package Manager`
|
|
||||||
- Add the local package:
|
|
||||||
- `+`->`Add package from git URL...`
|
|
||||||
- Set the path to `https://github.com/velaboratory/VEL-Connect`
|
|
||||||
|
|
||||||
To update the package, click the `Update` button in the Package Manager, or delete the `packages-lock.json` file.
|
|
||||||
|
|
||||||
=== "**Option 3:** Add the package locally"
|
|
||||||
|
|
||||||
1. Clone the repository on your computer:
|
|
||||||
`git clone git@github.com:velaboratory/VEL-Connect.git`
|
|
||||||
- Open the Package Manager in Unity with `Window->Package Manager`
|
|
||||||
- Add the local package:
|
|
||||||
- `+`->`Add package from disk...`
|
|
||||||
- Set the path to `VEL-Connect/package.json` on your hard drive.
|
|
||||||
|
|
||||||
To update the package, use `git pull` in the VEL-Connect folder.
|
|
||||||
|
|
||||||
|
|
||||||
Then check out the [samples](guide/samples.md), or follow the [quick start](guide/quick-start.md).
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
|
||||||
|
Install the UPM package in Unity:
|
||||||
|
|
||||||
|
=== "**Option 1:** Add the VEL package registry"
|
||||||
|
|
||||||
|
{ align=right }
|
||||||
|
|
||||||
|
Using the scoped registry allows you to easily install a specific version of the package by using the Version History tab.
|
||||||
|
|
||||||
|
- In Unity, go to `Edit->Project Settings...->Package Manager`
|
||||||
|
- Under "Scoped Registries" click the + icon
|
||||||
|
- Add the following details, then click Apply
|
||||||
|
- Name: `VEL` (or anything you want)
|
||||||
|
- URL: `https://npm.ugavel.com`
|
||||||
|
- Scope(s): `edu.uga.engr.vel`
|
||||||
|
- Install the package:
|
||||||
|
- In the package manager, select `My Registries` from the dropdown
|
||||||
|
- Install the `VEL-Connect` package.
|
||||||
|
|
||||||
|
=== "**Option 2:** Add the package by git url"
|
||||||
|
|
||||||
|
1. Open the Package Manager in Unity with `Window->Package Manager`
|
||||||
|
- Add the local package:
|
||||||
|
- `+`->`Add package from git URL...`
|
||||||
|
- Set the path to `https://github.com/velaboratory/VEL-Connect`
|
||||||
|
|
||||||
|
To update the package, click the `Update` button in the Package Manager, or delete the `packages-lock.json` file.
|
||||||
|
|
||||||
|
=== "**Option 3:** Add the package locally"
|
||||||
|
|
||||||
|
1. Clone the repository on your computer:
|
||||||
|
`git clone git@github.com:velaboratory/VEL-Connect.git`
|
||||||
|
- Open the Package Manager in Unity with `Window->Package Manager`
|
||||||
|
- Add the local package:
|
||||||
|
- `+`->`Add package from disk...`
|
||||||
|
- Set the path to `VEL-Connect/package.json` on your hard drive.
|
||||||
|
|
||||||
|
To update the package, use `git pull` in the VEL-Connect folder.
|
||||||
|
|
||||||
|
Then check out the [quick start guide](quick-start.md).
|
||||||
|
|
@ -0,0 +1,107 @@
|
||||||
|
---
|
||||||
|
title: Quick Start
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
1. [Install the package](/)
|
||||||
|
2. Add the VelConnectManager script to an object in your scene. If you transition between scenes in your application, mark the object as `DontDestroyOnLoad`
|
||||||
|
3. Set the `Vel Connect Url` field on the component to a valid velconnect server. `https://velconnect-v4.ugavel.com` is useful for VEL projects.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### Setting data
|
||||||
|
|
||||||
|
To set user data in VEL-Connect use the static function `SetUserData`.
|
||||||
|
|
||||||
|
You can add a single key and value:
|
||||||
|
```cs
|
||||||
|
VELConnectManager.SetUserData("key1", "val1");
|
||||||
|
```
|
||||||
|
|
||||||
|
Or set multiple keys with the dictionary syntax:
|
||||||
|
```cs
|
||||||
|
VELConnectManager.SetUserData(new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "key2", "val2" },
|
||||||
|
{ "key3", "val3" }
|
||||||
|
});
|
||||||
|
```
|
||||||
|
Data will be set instantly locally, then pushed to the server. You don't have to wait for VEL-Connect to initialize at the beginning of your game to set data.
|
||||||
|
|
||||||
|
### Getting data
|
||||||
|
|
||||||
|
Fetching data from a remote server can be more tricky because it won't be available immediately when the game starts. Data can also be set from other applications (such as a dashboard or other users in the case of room data), so change listeners are useful.
|
||||||
|
|
||||||
|
To fetch a single value from a key:
|
||||||
|
```cs
|
||||||
|
string value1 = VELConnectManager.GetUserData("key1");
|
||||||
|
```
|
||||||
|
The latest local value will be returned. This will always return null in `Start()` because no data has been fetched yet, so you could wrap this call in the `OnInitialState` callback:
|
||||||
|
```cs
|
||||||
|
VELConnectManager.OnInitialState += state =>
|
||||||
|
{
|
||||||
|
VELConnectManager.GetUserData("key1");
|
||||||
|
};
|
||||||
|
```
|
||||||
|
If the data was already on the server before the start of your application, the correct value will be returned.
|
||||||
|
|
||||||
|
|
||||||
|
#### Change listeners
|
||||||
|
|
||||||
|
If you want to subscribe to changes in a key you can set up change listeners:
|
||||||
|
```cs
|
||||||
|
VELConnectManager.AddUserDataListener("key1", this, value =>
|
||||||
|
{
|
||||||
|
Debug.Log($"key1: {value}");
|
||||||
|
}, true);
|
||||||
|
```
|
||||||
|
Passing in `this` binds the lifetime of the listener to the lifetime of the current script. It is often tedious to make sure to unsubscribe to all of your listeners OnDisable or OnDestroy to prevent the event emitter from sending events to objects that no longer exist, but VEL-Connect will remove listeners when their `keepAliveObject` parameter becomes null. The last parameter in this function (`true` in the example) tells VEL-Connect to activate the callback immediately or when the first value is received. You can add the listener on `Start()` and the first invokation of the callback will have the previous value of the server.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Full example:
|
||||||
|
```cs
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
using VELConnect;
|
||||||
|
|
||||||
|
public class VELConnectTesting : MonoBehaviour
|
||||||
|
{
|
||||||
|
private IEnumerator Start()
|
||||||
|
{
|
||||||
|
VELConnectManager.OnInitialState += state =>
|
||||||
|
{
|
||||||
|
Debug.Log($"[OnInitialState] key1: {VELConnectManager.GetUserData("key1")}");
|
||||||
|
};
|
||||||
|
|
||||||
|
VELConnectManager.AddUserDataListener("key1", this, value =>
|
||||||
|
{
|
||||||
|
Debug.Log($"[Listener] key1: {value}");
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
VELConnectManager.AddUserDataListener("key2", this, value =>
|
||||||
|
{
|
||||||
|
Debug.Log($"[Listener] key2: {value}");
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
yield return new WaitForSeconds(1f);
|
||||||
|
|
||||||
|
VELConnectManager.SetUserData("key1", "val1");
|
||||||
|
|
||||||
|
VELConnectManager.SetUserData(new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "key1", "val1" },
|
||||||
|
{ "key2", "val2" },
|
||||||
|
});
|
||||||
|
|
||||||
|
yield return new WaitForSeconds(1f);
|
||||||
|
|
||||||
|
VELConnectManager.SetUserData("key1", "val1_later");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
@ -1,15 +1,17 @@
|
||||||
:root {
|
:root {
|
||||||
--md-primary-fg-color: #666;
|
--md-primary-fg-color: #7a2020;
|
||||||
|
--md-primary-fg-color--light: #ffffff;
|
||||||
|
--md-primary-fg-color--dark: #e4002b;
|
||||||
--md-primary-bg-color: hsla(0, 0%, 100%, 1);
|
--md-primary-bg-color: hsla(0, 0%, 100%, 1);
|
||||||
|
--md-primary-bg-color--light: hsla(0, 0%, 100%, 0.7);
|
||||||
|
|
||||||
/* --md-accent-fg-color: #ffffff;
|
/* --md-accent-fg-color: #ffffff;
|
||||||
--md-accent-fg-color--transparent: #ffffff11;
|
--md-accent-fg-color--transparent: #ffffff11;
|
||||||
--md-accent-bg-color: hsla(0, 0%, 100%, 1);
|
--md-accent-bg-color: hsla(0, 0%, 100%, 1);
|
||||||
--md-accent-bg-color--light: hsla(0, 0%, 100%, 0.7); */
|
--md-accent-bg-color--light: hsla(0, 0%, 100%, 0.7); */
|
||||||
}
|
}
|
||||||
|
|
||||||
[data-md-color-scheme="slate"] {
|
[data-md-color-scheme="slate"] {
|
||||||
--md-hue: 34;
|
--md-hue: 34;
|
||||||
--md-default-bg-color: #111;
|
--md-default-bg-color: #191818;
|
||||||
--md-code-bg-color: #252525;
|
--md-code-bg-color: #252525;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -261,25 +261,7 @@ namespace VELConnect
|
||||||
state = JsonConvert.DeserializeObject<State>(json);
|
state = JsonConvert.DeserializeObject<State>(json);
|
||||||
if (state == null) return;
|
if (state == null) return;
|
||||||
|
|
||||||
bool isInitialState = false;
|
bool isInitialState = lastState == null;
|
||||||
|
|
||||||
// first load stuff
|
|
||||||
if (lastState == null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
OnInitialState?.Invoke(state);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Debug.LogError(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
isInitialState = true;
|
|
||||||
// lastState = state;
|
|
||||||
// return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// if (state.device.modified_by != DeviceId)
|
// if (state.device.modified_by != DeviceId)
|
||||||
{
|
{
|
||||||
|
|
@ -479,6 +461,18 @@ namespace VELConnect
|
||||||
{
|
{
|
||||||
Debug.LogError("Pairing code nulllll");
|
Debug.LogError("Pairing code nulllll");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isInitialState)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
OnInitialState?.Invoke(state);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.LogError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|
@ -798,6 +792,9 @@ namespace VELConnect
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Unpairs this device from the current user.
|
||||||
|
/// </summary>
|
||||||
public static void Unpair()
|
public static void Unpair()
|
||||||
{
|
{
|
||||||
if (instance.state?.device != null)
|
if (instance.state?.device != null)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue