facebook twitter hatena line email

「Unity/UniRx/EventTrigger」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(サンプル)
行6: 行6:
 
using UniRx.Triggers;
 
using UniRx.Triggers;
  
GameObject eventTextObj = GameObject.Find("EventText");
+
public class EventScene : MonoBehaviour
var eventTrigger = eventTextObj.AddComponent<ObservableEventTrigger>();
+
{
       
+
    void Start()
eventTrigger.OnPointerEnterAsObservable()
+
    {
            .Subscribe(pointerEventData => Debug.Log("PointerEnter " + pointerEventData.position)); // 触れた時 PointerExit (666.0, 1013.0)
+
        GameObject eventTextObj = GameObject.Find("EventText");
eventTrigger.OnPointerExitAsObservable()
+
        var eventTrigger = eventTextObj.AddComponent<ObservableEventTrigger>();
            .Subscribe(pointerEventData => Debug.Log("PointerExit " + pointerEventData.position)); // 外れた時
+
 
eventTrigger.OnDragAsObservable()
+
        eventTrigger.OnPointerEnterAsObservable()
            .Subscribe(pointerEventData => Debug.Log("Drag " + pointerEventData.position));
+
                    .Subscribe(pointerEventData => Debug.Log("PointerEnter " + pointerEventData.position)); // 触れた時 PointerExit (666.0, 1013.0)
eventTrigger.OnPointerDownAsObservable()
+
        eventTrigger.OnPointerExitAsObservable()
            .Subscribe(pointerEventData => Debug.Log("PointerDown " + pointerEventData.position)); // downした時
+
                    .Subscribe(pointerEventData => Debug.Log("PointerExit " + pointerEventData.position)); // 外れた時
eventTrigger.OnPointerClickAsObservable()
+
        eventTrigger.OnDragAsObservable()
            .Subscribe(pointerEventData => Debug.Log("PointerClick " + pointerEventData.position)); // upした時だが、PointerUpの後に実行
+
                    .Subscribe(pointerEventData => Debug.Log("Drag " + pointerEventData.position));
eventTrigger.OnPointerUpAsObservable()
+
        eventTrigger.OnPointerDownAsObservable()
            .Subscribe(pointerEventData => Debug.Log("PointerUp " + pointerEventData.position)); // upした時
+
                    .Subscribe(pointerEventData => Debug.Log("PointerDown " + pointerEventData.position)); // downした時
eventTrigger.OnBeginDragAsObservable()
+
        eventTrigger.OnPointerClickAsObservable()
            .Subscribe(pointerEventData => Debug.Log("BeginDrag " + pointerEventData.position));
+
                    .Subscribe(pointerEventData => Debug.Log("PointerClick " + pointerEventData.position)); // upした時だが、PointerUpの後に実行
eventTrigger.OnEndDragAsObservable()
+
        eventTrigger.OnPointerUpAsObservable()
            .Subscribe(pointerEventData => Debug.Log("EndDrag " + pointerEventData.position));
+
                    .Subscribe(pointerEventData => Debug.Log("PointerUp " + pointerEventData.position)); // upした時
eventTrigger.OnDropAsObservable()
+
        eventTrigger.OnBeginDragAsObservable()
            .Subscribe(pointerEventData => Debug.Log("Drop " + pointerEventData.position)); // EndDragとほぼ同じで、EndDragより早い。
+
                    .Subscribe(pointerEventData => Debug.Log("BeginDrag " + pointerEventData.position));
eventTrigger.OnInitializePotentialDragAsObservable()
+
        eventTrigger.OnEndDragAsObservable()
            .Subscribe(pointerEventData => Debug.Log("Initialize " + pointerEventData.position)); // downした時とほぼ同じで、少し後
+
                    .Subscribe(pointerEventData => Debug.Log("EndDrag " + pointerEventData.position));
eventTrigger.OnScrollAsObservable()
+
        eventTrigger.OnDropAsObservable()
            .Subscribe(pointerEventData => Debug.Log("Scroll " + pointerEventData.position)); // オブジェクト上でscrollした時
+
                    .Subscribe(pointerEventData => Debug.Log("Drop " + pointerEventData.position)); // EndDragとほぼ同じで、EndDragより早い。
// 以下は、反応しない
+
        eventTrigger.OnInitializePotentialDragAsObservable()
eventTrigger.OnDeselectAsObservable()
+
                    .Subscribe(pointerEventData => Debug.Log("Initialize " + pointerEventData.position)); // downした時とほぼ同じで、少し後
            .Subscribe(baseEventData => Debug.Log("Deselect " + baseEventData.selectedObject));
+
        eventTrigger.OnScrollAsObservable()
eventTrigger.OnSelectAsObservable()
+
                    .Subscribe(pointerEventData => Debug.Log("Scroll " + pointerEventData.position)); // オブジェクト上でscrollした時
            .Subscribe(baseEventData => Debug.Log("Select " + baseEventData.selectedObject));
+
        // 以下は、反応しない
eventTrigger.OnSubmitAsObservable()
+
        eventTrigger.OnDeselectAsObservable()
            .Subscribe(baseEventData => Debug.Log("Submit " + baseEventData.selectedObject));
+
                    .Subscribe(baseEventData => Debug.Log("Deselect " + baseEventData.selectedObject));
eventTrigger.OnCancelAsObservable()
+
        eventTrigger.OnSelectAsObservable()
            .Subscribe(baseEventData => Debug.Log("Cancel " + baseEventData.selectedObject));
+
                    .Subscribe(baseEventData => Debug.Log("Select " + baseEventData.selectedObject));
eventTrigger.OnUpdateSelectedAsObservable()
+
        eventTrigger.OnSubmitAsObservable()
            .Subscribe(baseEventData => Debug.Log("UpdateSelected " + baseEventData.selectedObject));
+
                    .Subscribe(baseEventData => Debug.Log("Submit " + baseEventData.selectedObject));
eventTrigger.OnMoveAsObservable()
+
        eventTrigger.OnCancelAsObservable()
            .Subscribe(axisEventData => Debug.Log("Move " + axisEventData.moveVector));
+
                    .Subscribe(baseEventData => Debug.Log("Cancel " + baseEventData.selectedObject));
 +
        eventTrigger.OnUpdateSelectedAsObservable()
 +
                    .Subscribe(baseEventData => Debug.Log("UpdateSelected " + baseEventData.selectedObject));
 +
        eventTrigger.OnMoveAsObservable()
 +
                    .Subscribe(axisEventData => Debug.Log("Move " + axisEventData.moveVector));
 +
 
 +
    }
 +
}
 +
 
 
</pre>
 
</pre>
  
 
==反応しない場合==
 
==反応しない場合==
 
Scene内に、EventSystemのゲームオブジェクトがあるか確認。
 
Scene内に、EventSystemのゲームオブジェクトがあるか確認。

2021年9月29日 (水) 19:03時点における版

サンプル

オブジェクトに、触れたときとか、外れたときとかのイベントを取得できる。 ObservableEventTriggerを、オブジェクトに、事前にAddComponentしておいても良い。

using UniRx;
using UniRx.Triggers;

public class EventScene : MonoBehaviour
{
    void Start()
    {
        GameObject eventTextObj = GameObject.Find("EventText");
        var eventTrigger = eventTextObj.AddComponent<ObservableEventTrigger>();

        eventTrigger.OnPointerEnterAsObservable()
                    .Subscribe(pointerEventData => Debug.Log("PointerEnter " + pointerEventData.position)); // 触れた時 PointerExit (666.0, 1013.0)
        eventTrigger.OnPointerExitAsObservable()
                    .Subscribe(pointerEventData => Debug.Log("PointerExit " + pointerEventData.position)); // 外れた時
        eventTrigger.OnDragAsObservable()
                    .Subscribe(pointerEventData => Debug.Log("Drag " + pointerEventData.position));
        eventTrigger.OnPointerDownAsObservable()
                    .Subscribe(pointerEventData => Debug.Log("PointerDown " + pointerEventData.position)); // downした時
        eventTrigger.OnPointerClickAsObservable()
                    .Subscribe(pointerEventData => Debug.Log("PointerClick " + pointerEventData.position)); // upした時だが、PointerUpの後に実行
        eventTrigger.OnPointerUpAsObservable()
                    .Subscribe(pointerEventData => Debug.Log("PointerUp " + pointerEventData.position)); // upした時
        eventTrigger.OnBeginDragAsObservable()
                    .Subscribe(pointerEventData => Debug.Log("BeginDrag " + pointerEventData.position));
        eventTrigger.OnEndDragAsObservable()
                    .Subscribe(pointerEventData => Debug.Log("EndDrag " + pointerEventData.position));
        eventTrigger.OnDropAsObservable()
                    .Subscribe(pointerEventData => Debug.Log("Drop " + pointerEventData.position)); // EndDragとほぼ同じで、EndDragより早い。
        eventTrigger.OnInitializePotentialDragAsObservable()
                    .Subscribe(pointerEventData => Debug.Log("Initialize " + pointerEventData.position)); // downした時とほぼ同じで、少し後
        eventTrigger.OnScrollAsObservable()
                    .Subscribe(pointerEventData => Debug.Log("Scroll " + pointerEventData.position)); // オブジェクト上でscrollした時
        // 以下は、反応しない
        eventTrigger.OnDeselectAsObservable()
                    .Subscribe(baseEventData => Debug.Log("Deselect " + baseEventData.selectedObject));
        eventTrigger.OnSelectAsObservable()
                    .Subscribe(baseEventData => Debug.Log("Select " + baseEventData.selectedObject));
        eventTrigger.OnSubmitAsObservable()
                    .Subscribe(baseEventData => Debug.Log("Submit " + baseEventData.selectedObject));
        eventTrigger.OnCancelAsObservable()
                    .Subscribe(baseEventData => Debug.Log("Cancel " + baseEventData.selectedObject));
        eventTrigger.OnUpdateSelectedAsObservable()
                    .Subscribe(baseEventData => Debug.Log("UpdateSelected " + baseEventData.selectedObject));
        eventTrigger.OnMoveAsObservable()
                    .Subscribe(axisEventData => Debug.Log("Move " + axisEventData.moveVector));

    }
}

反応しない場合

Scene内に、EventSystemのゲームオブジェクトがあるか確認。