2
0

UnityUIComponentExtensions.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // for uGUI(from 4.6)
  2. #if !(UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5)
  3. using System;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace UniRx
  7. {
  8. public static partial class UnityUIComponentExtensions
  9. {
  10. public static IDisposable SubscribeToText(this IObservable<string> source, Text text)
  11. {
  12. return source.SubscribeWithState(text, (x, t) => t.text = x);
  13. }
  14. public static IDisposable SubscribeToText<T>(this IObservable<T> source, Text text)
  15. {
  16. return source.SubscribeWithState(text, (x, t) => t.text = x.ToString());
  17. }
  18. public static IDisposable SubscribeToText<T>(this IObservable<T> source, Text text, Func<T, string> selector)
  19. {
  20. return source.SubscribeWithState2(text, selector, (x, t, s) => t.text = s(x));
  21. }
  22. public static IDisposable SubscribeToInteractable(this IObservable<bool> source, Selectable selectable)
  23. {
  24. return source.SubscribeWithState(selectable, (x, s) => s.interactable = x);
  25. }
  26. /// <summary>Observe onClick event.</summary>
  27. public static IObservable<Unit> OnClickAsObservable(this Button button)
  28. {
  29. return button.onClick.AsObservable();
  30. }
  31. /// <summary>Observe onValueChanged with current `isOn` value on subscribe.</summary>
  32. public static IObservable<bool> OnValueChangedAsObservable(this Toggle toggle)
  33. {
  34. // Optimized Defer + StartWith
  35. return Observable.CreateWithState<bool, Toggle>(toggle, (t, observer) =>
  36. {
  37. observer.OnNext(t.isOn);
  38. return t.onValueChanged.AsObservable().Subscribe(observer);
  39. });
  40. }
  41. /// <summary>Observe onValueChanged with current `value` on subscribe.</summary>
  42. public static IObservable<float> OnValueChangedAsObservable(this Scrollbar scrollbar)
  43. {
  44. return Observable.CreateWithState<float, Scrollbar>(scrollbar, (s, observer) =>
  45. {
  46. observer.OnNext(s.value);
  47. return s.onValueChanged.AsObservable().Subscribe(observer);
  48. });
  49. }
  50. /// <summary>Observe onValueChanged with current `normalizedPosition` value on subscribe.</summary>
  51. public static IObservable<Vector2> OnValueChangedAsObservable(this ScrollRect scrollRect)
  52. {
  53. return Observable.CreateWithState<Vector2, ScrollRect>(scrollRect, (s, observer) =>
  54. {
  55. observer.OnNext(s.normalizedPosition);
  56. return s.onValueChanged.AsObservable().Subscribe(observer);
  57. });
  58. }
  59. /// <summary>Observe onValueChanged with current `value` on subscribe.</summary>
  60. public static IObservable<float> OnValueChangedAsObservable(this Slider slider)
  61. {
  62. return Observable.CreateWithState<float, Slider>(slider, (s, observer) =>
  63. {
  64. observer.OnNext(s.value);
  65. return s.onValueChanged.AsObservable().Subscribe(observer);
  66. });
  67. }
  68. /// <summary>Observe onEndEdit(Submit) event.</summary>
  69. public static IObservable<string> OnEndEditAsObservable(this InputField inputField)
  70. {
  71. return inputField.onEndEdit.AsObservable();
  72. }
  73. #if (UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2)
  74. /// <summary>Observe onValueChange with current `text` value on subscribe.</summary>
  75. public static IObservable<string> OnValueChangeAsObservable(this InputField inputField)
  76. {
  77. return Observable.CreateWithState<string, InputField>(inputField, (i, observer) =>
  78. {
  79. observer.OnNext(i.text);
  80. return i.onValueChange.AsObservable().Subscribe(observer);
  81. });
  82. }
  83. #else
  84. /// <summary>Observe onValueChanged with current `text` value on subscribe.</summary>
  85. public static IObservable<string> OnValueChangedAsObservable(this InputField inputField)
  86. {
  87. return Observable.CreateWithState<string, InputField>(inputField, (i, observer) =>
  88. {
  89. observer.OnNext(i.text);
  90. return i.onValueChanged.AsObservable().Subscribe(observer);
  91. });
  92. }
  93. #endif
  94. #if UNITY_5_3_OR_NEWER
  95. /// <summary>Observe onValueChanged with current `value` on subscribe.</summary>
  96. public static IObservable<int> OnValueChangedAsObservable(this Dropdown dropdown)
  97. {
  98. return Observable.CreateWithState<int, Dropdown>(dropdown, (d, observer) =>
  99. {
  100. observer.OnNext(d.value);
  101. return d.onValueChanged.AsObservable().Subscribe(observer);
  102. });
  103. }
  104. #endif
  105. }
  106. }
  107. #endif