2022年9月13日 星期二

Unity New Input System 判斷滑鼠拖曳 Drag

 2022.09.12 Unity New Input System 在使用滑鼠拖曳 Drag 事件時,以往的 OnMouseDrag() 不再支援,網路上查半天方法都是非常麻煩,最終發現簡單可行方法:

1.安裝 New Input System,並設定啟用。

2.新增物件並在上新增 Player Input component,然後 Create Actions。

3.在 Actions 下方選完成如下圖。

4.新增script 並放在剛剛的新增物件上。內容如下:

using UnityEngine;
using UnityEngine.InputSystem;
public class TestDrag : MonoBehaviour
{   
public void Drag(InputAction.CallbackContext ctx)
   {
        Debug.Log(ctx.ReadValue<Vector2>());
  }
}

5.在 Player Input component 中的 Events 點選剛新增的 Action,拖曳 TestDrag 並選擇 TestDrag.Drag


6.執行後只要有滑鼠拖曳事件就會顯示拖曳的向量。


沒有留言:

張貼留言