Skip to content

Instantly share code, notes, and snippets.

@TakaakiIchijo
Last active January 5, 2024 03:27
Show Gist options
  • Save TakaakiIchijo/da9dbf704b7398828887139c7513f948 to your computer and use it in GitHub Desktop.
Save TakaakiIchijo/da9dbf704b7398828887139c7513f948 to your computer and use it in GitHub Desktop.
A piece of UniRx extensions for Netcode for GameObjects
using System;
using Unity.Netcode;
namespace UniRx
{
public static class UnityNetcodeUniRxExtensions
{
public static IObservable<(T previousValue, T newValue)> AsObservable<T>(this NetworkVariable<T> networkVariable)
{
return Observable.FromEvent<NetworkVariable<T>.OnValueChangedDelegate, (T, T)>(
h => (previousValue, newValue) => h((previousValue, newValue)),
h => networkVariable.OnValueChanged += h,
h => networkVariable.OnValueChanged -= h);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment