Skip to content

Commit

Permalink
Add Dropdown.OnValueChangedAsObservable neuecc#236
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Aug 19, 2018
1 parent 2b42026 commit 9602cbb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Assets/Plugins/UniRx/Scripts/Async/UnityAsyncExtensions.uGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ public static async UniTask<string> OnEndEditAsync(this InputField inputField)
return await handler.OnEndEditAsync();
}
}

public static IAsyncValueChangedEventHandler<int> GetAsyncValueChangedEventHandler(this Dropdown dropdown)
{
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy());
}

public static async UniTask<int> OnValueChanged(this Dropdown dropdown)
{
using (var handler = dropdown.GetAsyncValueChangedEventHandler())
{
return await handler.OnValueChangedAsync();
}
}
}

public interface IAsyncClickEventHandler : IDisposable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ public static IObservable<string> OnValueChangedAsObservable(this InputField inp
});
}
#endif

/// <summary>Observe onValueChanged with current `value` on subscribe.</summary>
public static IObservable<int> OnValueChangedAsObservable(this Dropdown dropdown)
{
return Observable.CreateWithState<int, Dropdown>(dropdown, (d, observer) =>
{
observer.OnNext(d.value);
return d.onValueChanged.AsObservable().Subscribe(observer);
});
}
}
}

Expand Down

0 comments on commit 9602cbb

Please sign in to comment.