「Unity/UniRx/複数イベント」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→Unitを使い、引数を使わない場合) |
|||
行1: | 行1: | ||
+ | ==単純にトリガーをforeachで処理する場合== | ||
+ | <pre> | ||
+ | using UnityEngine; | ||
+ | using UniRx; | ||
+ | using UniRx.Triggers; | ||
+ | public class MultiAddComponentScene : MonoBehaviour | ||
+ | { | ||
+ | void Start() | ||
+ | { | ||
+ | ObservableEventTriggers(); | ||
+ | } | ||
+ | // 単純にトリガーをforeachで処理する場合 | ||
+ | public List<ObservableEventTrigger> observableEventTriggers; | ||
+ | void ObservableEventTriggers() | ||
+ | { | ||
+ | foreach (var eventTrigger in observableEventTriggers) { | ||
+ | eventTrigger | ||
+ | .OnPointerEnterAsObservable() | ||
+ | .Subscribe(pointerEventData => Debug.Log("PointerEnter " + pointerEventData.position)) // 触れた時 PointerExit (666.0, 1013.0) | ||
+ | .AddTo(gameObject); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | #オブジェクトを複数(とりあえず2つ)用意して、ObservableEventTriggerをAddComponentしておく。 | ||
+ | #observableEventTriggersにトリガーとなるオブジェクトを、Inspectorから、ドラッグする | ||
+ | |||
+ | ==1つのObservableに複数イベントを統合して処理する場合== | ||
==単純にトリガーをforeachで処理する場合== | ==単純にトリガーをforeachで処理する場合== | ||
<pre> | <pre> | ||
行33: | 行61: | ||
using UniRx.Triggers; | using UniRx.Triggers; | ||
using System; | using System; | ||
− | using UnityEngine. | + | using UnityEngine.Eventsystems; |
public class MultiAddComponentScene : MonoBehaviour | public class MultiAddComponentScene : MonoBehaviour | ||
{ | { | ||
行75: | 行103: | ||
参考:https://www.hanachiru-blog.com/entry/2018/09/21/151426 | 参考:https://www.hanachiru-blog.com/entry/2018/09/21/151426 | ||
+ | |||
+ | ===PointerEventDataを引数として渡す場合=== | ||
+ | <pre> | ||
+ | using UnityEngine; | ||
+ | using UniRx; | ||
+ | using UniRx.Triggers; | ||
+ | using System; | ||
+ | using UnityEngine.Eventsystems; | ||
+ | public class MultiAddComponentScene : MonoBehaviour | ||
+ | { | ||
+ | void Start() | ||
+ | { | ||
+ | OnePointerEventDataObservableTriggers(); | ||
+ | } | ||
+ | // 1つのObservableに複数イベントを統合して処理し、PointerEventDataを引数として渡す場合 | ||
+ | public List<EventTrigger> eventTriggers; | ||
+ | void OnePointerEventDataObservableTriggers() | ||
+ | { | ||
+ | IObservable<PointerEventData> triggerOnPointerEnterAsObservable; | ||
+ | triggerOnPointerEnterAsObservable = Observable.Create<PointerEventData>(observer => | ||
+ | { | ||
+ | foreach (var trigger in eventTriggers) | ||
+ | { | ||
+ | var pointerEnter = new EventTrigger.Entry | ||
+ | { | ||
+ | eventID = EventTriggerType.PointerEnter | ||
+ | }; | ||
+ | pointerEnter.callback.AddListener(data => | ||
+ | { | ||
+ | observer.OnNext((PointerEventData)data); | ||
+ | }); | ||
+ | trigger.triggers.Add(pointerEnter); | ||
+ | } | ||
+ | return Disposable.Empty; | ||
+ | }); | ||
+ | triggerOnPointerEnterAsObservable | ||
+ | .Subscribe(pointerEventData => Debug.Log("PointerEnter " + pointerEventData.position)) | ||
+ | .AddTo(gameObject); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | 参考:https://docs.unity3d.com/ja/2018.4/ScriptReference/Eventsystems.EventTrigger.html | ||
+ | |||
+ | ===GameObjectを引数として渡す場合=== | ||
+ | <pre> | ||
+ | public class MultiAddComponentScene : MonoBehaviour | ||
+ | { | ||
+ | void Start() | ||
+ | { | ||
+ | OneGameObjectObservableTriggers(); | ||
+ | } | ||
+ | public List<EventTrigger> eventTriggers; | ||
+ | // 1つのObservableに複数イベントを統合して処理し、GameObjectを引数として渡す場合 | ||
+ | void OneGameObjectObservableTriggers() | ||
+ | { | ||
+ | IObservable<GameObject> triggerOnPointerEnterAsObservable; | ||
+ | triggerOnPointerEnterAsObservable = Observable.Create<GameObject>(observer => | ||
+ | { | ||
+ | foreach (var trigger in eventTriggers) | ||
+ | { | ||
+ | var pointerEnter = new EventTrigger.Entry | ||
+ | { | ||
+ | eventID = EventTriggerType.PointerEnter | ||
+ | }; | ||
+ | pointerEnter.callback.AddListener(data => | ||
+ | { | ||
+ | observer.OnNext(trigger.gameObject); | ||
+ | }); | ||
+ | trigger.triggers.Add(pointerEnter); | ||
+ | } | ||
+ | return Disposable.Empty; | ||
+ | }); | ||
+ | triggerOnPointerEnterAsObservable | ||
+ | .Subscribe(obj => Debug.Log("PointerEnter " + obj.name)) | ||
+ | .AddTo(gameObject); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
===PointerEventDataを引数として渡す場合=== | ===PointerEventDataを引数として渡す場合=== |
2021年10月14日 (木) 22:29時点における版
目次
単純にトリガーをforeachで処理する場合
using UnityEngine; using UniRx; using UniRx.Triggers; public class MultiAddComponentScene : MonoBehaviour { void Start() { ObservableEventTriggers(); } // 単純にトリガーをforeachで処理する場合 public List<ObservableEventTrigger> observableEventTriggers; void ObservableEventTriggers() { foreach (var eventTrigger in observableEventTriggers) { eventTrigger .OnPointerEnterAsObservable() .Subscribe(pointerEventData => Debug.Log("PointerEnter " + pointerEventData.position)) // 触れた時 PointerExit (666.0, 1013.0) .AddTo(gameObject); } } }
- オブジェクトを複数(とりあえず2つ)用意して、ObservableEventTriggerをAddComponentしておく。
- observableEventTriggersにトリガーとなるオブジェクトを、Inspectorから、ドラッグする
1つのObservableに複数イベントを統合して処理する場合
単純にトリガーをforeachで処理する場合
using UnityEngine; using UniRx; using UniRx.Triggers; public class MultiAddComponentScene : MonoBehaviour { void Start() { ObservableEventTriggers(); } // 単純にトリガーをforeachで処理する場合 public List<ObservableEventTrigger> observableEventTriggers; void ObservableEventTriggers() { foreach (var eventTrigger in observableEventTriggers) { eventTrigger .OnPointerEnterAsObservable() .Subscribe(pointerEventData => Debug.Log("PointerEnter " + pointerEventData.position)) // 触れた時 PointerExit (666.0, 1013.0) .AddTo(gameObject); } } }
- オブジェクトを複数(とりあえず2つ)用意して、ObservableEventTriggerをAddComponentしておく。
- observableEventTriggersにトリガーとなるオブジェクトを、Inspectorから、ドラッグする
1つのObservableに複数イベントを統合して処理する場合
Unitを使い、引数を使わない場合
using UnityEngine; using UniRx; using UniRx.Triggers; using System; using UnityEngine.Eventsystems; public class MultiAddComponentScene : MonoBehaviour { void Start() { OneObservableTriggers(); } // 1つのObservableに複数イベントを統合して処理する場合 public List<EventTrigger> eventTriggers; void OneObservableTriggers() { IObservable<Unit> triggerOnPointerEnterAsObservable; triggerOnPointerEnterAsObservable = Observable.Create<Unit>(observer => { Debug.Log("eventTriggers.Count=" + eventTriggers.Count); foreach (var trigger in eventTriggers) { if (trigger == null) { Debug.Log("trigger is null"); } var pointerEnter = new EventTrigger.Entry { eventID = EventTriggerType.PointerEnter }; pointerEnter.callback.AddListener(data => { observer.OnNext(Unit.Default); }); trigger.triggers.Add(pointerEnter); } return Disposable.Empty; }); triggerOnPointerEnterAsObservable .Subscribe(pointerEventData => Debug.Log("PointerEnter")) .AddTo(gameObject); } }
- オブジェクトを複数(とりあえず2つ)用意して、EventTriggerをAddComponentしておく。
- eventTriggersにトリガーとなるオブジェクトを、Inspectorから、ドラッグする
参考:https://www.hanachiru-blog.com/entry/2018/09/21/151426
PointerEventDataを引数として渡す場合
using UnityEngine; using UniRx; using UniRx.Triggers; using System; using UnityEngine.Eventsystems; public class MultiAddComponentScene : MonoBehaviour { void Start() { OnePointerEventDataObservableTriggers(); } // 1つのObservableに複数イベントを統合して処理し、PointerEventDataを引数として渡す場合 public List<EventTrigger> eventTriggers; void OnePointerEventDataObservableTriggers() { IObservable<PointerEventData> triggerOnPointerEnterAsObservable; triggerOnPointerEnterAsObservable = Observable.Create<PointerEventData>(observer => { foreach (var trigger in eventTriggers) { var pointerEnter = new EventTrigger.Entry { eventID = EventTriggerType.PointerEnter }; pointerEnter.callback.AddListener(data => { observer.OnNext((PointerEventData)data); }); trigger.triggers.Add(pointerEnter); } return Disposable.Empty; }); triggerOnPointerEnterAsObservable .Subscribe(pointerEventData => Debug.Log("PointerEnter " + pointerEventData.position)) .AddTo(gameObject); } }
参考:https://docs.unity3d.com/ja/2018.4/ScriptReference/Eventsystems.EventTrigger.html
GameObjectを引数として渡す場合
public class MultiAddComponentScene : MonoBehaviour { void Start() { OneGameObjectObservableTriggers(); } public List<EventTrigger> eventTriggers; // 1つのObservableに複数イベントを統合して処理し、GameObjectを引数として渡す場合 void OneGameObjectObservableTriggers() { IObservable<GameObject> triggerOnPointerEnterAsObservable; triggerOnPointerEnterAsObservable = Observable.Create<GameObject>(observer => { foreach (var trigger in eventTriggers) { var pointerEnter = new EventTrigger.Entry { eventID = EventTriggerType.PointerEnter }; pointerEnter.callback.AddListener(data => { observer.OnNext(trigger.gameObject); }); trigger.triggers.Add(pointerEnter); } return Disposable.Empty; }); triggerOnPointerEnterAsObservable .Subscribe(obj => Debug.Log("PointerEnter " + obj.name)) .AddTo(gameObject); } }
PointerEventDataを引数として渡す場合
using UnityEngine; using UniRx; using UniRx.Triggers; using System; using UnityEngine.Multiystems; public class MultiAddComponentScene : MonoBehaviour { void Start() { OnePointerEventDataObservableTriggers(); } // 1つのObservableに複数イベントを統合して処理し、PointerEventDataを引数として渡す場合 public List<EventTrigger> eventTriggers; void OnePointerEventDataObservableTriggers() { IObservable<PointerEventData> triggerOnPointerEnterAsObservable; triggerOnPointerEnterAsObservable = Observable.Create<PointerEventData>(observer => { foreach (var trigger in eventTriggers) { var pointerEnter = new EventTrigger.Entry { eventID = EventTriggerType.PointerEnter }; pointerEnter.callback.AddListener(data => { observer.OnNext((PointerEventData)data); }); trigger.triggers.Add(pointerEnter); } return Disposable.Empty; }); triggerOnPointerEnterAsObservable .Subscribe(pointerEventData => Debug.Log("PointerEnter " + pointerEventData.position)) .AddTo(gameObject); } }
参考:https://docs.unity3d.com/ja/2018.4/ScriptReference/Multiystems.EventTrigger.html
GameObjectを引数として渡す場合
public class MultiAddComponentScene : MonoBehaviour { void Start() { OneGameObjectObservableTriggers(); } public List<EventTrigger> eventTriggers; // 1つのObservableに複数イベントを統合して処理し、GameObjectを引数として渡す場合 void OneGameObjectObservableTriggers() { IObservable<GameObject> triggerOnPointerEnterAsObservable; triggerOnPointerEnterAsObservable = Observable.Create<GameObject>(observer => { foreach (var trigger in eventTriggers) { var pointerEnter = new EventTrigger.Entry { eventID = EventTriggerType.PointerEnter }; pointerEnter.callback.AddListener(data => { observer.OnNext(trigger.gameObject); }); trigger.triggers.Add(pointerEnter); } return Disposable.Empty; }); triggerOnPointerEnterAsObservable .Subscribe(obj => Debug.Log("PointerEnter " + obj.name)) .AddTo(gameObject); } }