-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from shiena/feature/support-r3
Support R3
- Loading branch information
Showing
23 changed files
with
375 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# R3 | ||
|
||
By integrating [R3](https://github.com/Cysharp/R3) into your project, it adds extension methods compatible with Reactive Extensions (Rx). | ||
|
||
If you have installed R3 via the Package Manager, the following functionality will be automatically added. However, if you have imported UniRx via a unitypackage or similar method, you'll need to add `LITMOTION_SUPPORT_R3` to `Settings > Player > Scripting Define Symbols`. | ||
|
||
### Creating Motion as an Observable | ||
|
||
You can create motion as an `IObservable<T>` using the `ToObservable()` method: | ||
|
||
```cs | ||
var observable = LMotion.Create(0f, 5f, 2f).ToObservable(); | ||
observable.Subscribe(x => | ||
{ | ||
Debug.Log(x); | ||
}) | ||
.AddTo(gameObject); | ||
``` | ||
|
||
### Binding to ReactiveProperty | ||
|
||
There's a provided extension method to bind the created motion to a `ReactiveProperty<T>`: | ||
|
||
```cs | ||
var reactiveProperty = new ReactiveProperty<float>(); | ||
LMotion.Create(0f, 10f, 2f) | ||
.BindToReactiveProperty(reactiveProperty); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# R3 | ||
|
||
プロジェクトに[R3](https://github.com/Cysharp/R3)を導入することでReactive Extensions(Rx)に対応した拡張メソッドが追加されます。 | ||
|
||
R3をPackage Managerから導入した場合は自動で以下の機能が追加されます。unitypackage等で導入した場合は、`Project Settings > Player > Scripting Define Symbols`に`LITMOTION_SUPPORT_R3`を追加する必要があります。 | ||
|
||
### モーションをObservableとして作成 | ||
|
||
`ToObservable()`を使用することで、モーションを`IObservable<T>`として作成できます。 | ||
|
||
```cs | ||
var observable = LMotion.Create(0f, 5f, 2f).ToObservable(); | ||
observable.Subscribe(x => | ||
{ | ||
Debug.Log(x); | ||
}) | ||
.AddTo(gameObject); | ||
``` | ||
|
||
### ReactivePropertyへバインド | ||
|
||
作成したモーションを`ReactiveProperty<T>`にバインドする拡張メソッドが用意されています。 | ||
|
||
```cs | ||
var reactiveProperty = new ReactiveProperty<float>(); | ||
LMotion.Create(0f, 10f, 2f) | ||
.BindToReactiveProperty(reactiveProperty); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
src/LitMotion/Assets/LitMotion/Runtime/External/R3/LitMotionR3Extensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#if LITMOTION_SUPPORT_R3 | ||
using R3; | ||
|
||
namespace LitMotion | ||
{ | ||
public static class LitMotionR3Extensions | ||
{ | ||
/// <summary> | ||
/// Create the motion as Observable. | ||
/// </summary> | ||
/// <typeparam name="TValue">The type of value to animate</typeparam> | ||
/// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam> | ||
/// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam> | ||
/// <param name="builder">This builder</param> | ||
/// <returns>Observable of the created motion.</returns> | ||
public static Observable<TValue> ToObservable<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder) | ||
where TValue : unmanaged | ||
where TOptions : unmanaged, IMotionOptions | ||
where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions> | ||
{ | ||
var subject = new Subject<TValue>(); | ||
var callbacks = builder.BuildCallbackData(subject, static (x, subject) => subject.OnNext(x)); | ||
callbacks.OnCompleteAction += () => subject.OnCompleted(); | ||
callbacks.OnCancelAction += () => subject.OnCompleted(); | ||
var scheduler = builder.buffer.Scheduler; | ||
var entity = builder.BuildMotionData(); | ||
|
||
builder.Schedule(scheduler, ref entity, ref callbacks); | ||
return subject; | ||
} | ||
|
||
/// <summary> | ||
/// Create a motion data and bind it to ReactiveProperty. | ||
/// </summary> | ||
/// <typeparam name="TValue">The type of value to animate</typeparam> | ||
/// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam> | ||
/// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam> | ||
/// <param name="builder">This builder</param> | ||
/// <param name="progress">Target object that implements IProgress</param> | ||
/// <returns>Handle of the created motion data.</returns> | ||
public static MotionHandle BindToReactiveProperty<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder, ReactiveProperty<TValue> reactiveProperty) | ||
where TValue : unmanaged | ||
where TOptions : unmanaged, IMotionOptions | ||
where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions> | ||
{ | ||
Error.IsNull(reactiveProperty); | ||
return builder.BindWithState(reactiveProperty, static (x, target) => | ||
{ | ||
target.Value = x; | ||
}); | ||
} | ||
} | ||
} | ||
#endif |
11 changes: 11 additions & 0 deletions
11
src/LitMotion/Assets/LitMotion/Runtime/External/R3/LitMotionR3Extensions.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/LitMotion/Assets/LitMotion/Tests/Runtime/ExternalExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace LitMotion.Tests.Runtime | ||
{ | ||
static class ExternalExtensions | ||
{ | ||
#if LITMOTION_TEST_R3 | ||
public static R3.Observable<TValue> ToR3Observable<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder) | ||
where TValue : unmanaged | ||
where TOptions : unmanaged, IMotionOptions | ||
where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions> | ||
{ | ||
return LitMotionR3Extensions.ToObservable(builder); | ||
} | ||
#endif | ||
|
||
#if LITMOTION_TEST_UNIRX | ||
public static System.IObservable<TValue> ToRxObservable<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder) | ||
where TValue : unmanaged | ||
where TOptions : unmanaged, IMotionOptions | ||
where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions> | ||
{ | ||
return LitMotionUniRxExtensions.ToObservable(builder); | ||
} | ||
#endif | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/LitMotion/Assets/LitMotion/Tests/Runtime/ExternalExtensions.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#if LITMOTION_TEST_R3 | ||
using System; | ||
using System.Collections; | ||
using UnityEngine; | ||
using UnityEngine.TestTools; | ||
using R3; | ||
using NUnit.Framework; | ||
|
||
namespace LitMotion.Tests.Runtime | ||
{ | ||
public class R3Test | ||
{ | ||
readonly CompositeDisposable disposables = new(); | ||
|
||
[OneTimeTearDown] | ||
public void OneTimeTearDown() | ||
{ | ||
disposables.Dispose(); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator Test_ToObservable() | ||
{ | ||
bool completed = false; | ||
LMotion.Create(0f, 10f, 2f) | ||
.WithOnComplete(() => completed = true) | ||
.ToR3Observable() | ||
.Subscribe(x => Debug.Log(x)) | ||
.AddTo(disposables); | ||
while (!completed) yield return null; | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator Test_BindToReactiveProperty() | ||
{ | ||
var reactiveProperty = new ReactiveProperty<float>(); | ||
reactiveProperty.AddTo(disposables); | ||
|
||
bool completed = false; | ||
LMotion.Create(0f, 10f, 2f) | ||
.WithOnComplete(() => completed = true) | ||
.BindToReactiveProperty(reactiveProperty) | ||
.ToDisposable() | ||
.AddTo(disposables); | ||
|
||
reactiveProperty.Subscribe(x => | ||
{ | ||
Debug.Log(x); | ||
}) | ||
.AddTo(disposables); | ||
|
||
while (!completed) yield return null; | ||
} | ||
} | ||
} | ||
#endif |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.