2019年12月2日 星期一

(續) render to RenderTexture 下的事件

上一篇筆記
為了嘗試在同時使用到多個 Cameras情境下
render to RenderTexture時讓 game object正確接收/處理 mouse events
因此爬了幾個文章

https://forum.unity.com/threads/interaction-with-objects-displayed-on-render-texture.517175/
(更完整個重新實現自己的 event input module)
https://forum.unity.com/threads/handling-event-input-offscreen.378470/

我試著參考這些資料試驗
但是因為自己尚未深入去爬 UnityEngine.UI source codes
所以對於部分 coding 還是無法理解

偶然才發現到其實 Unity 提供的 ***Raycaster 就是在處理這方面的事情
https://docs.unity3d.com/2019.2/Documentation/Manual/script-GraphicRaycaster.html
https://docs.unity3d.com/2019.2/Documentation/Manual/script-PhysicsRaycaster.html
只是之前從官方文件上看不夠仔細沒有想到而已

因此嘗試在 RenderTexture camera 中裝上 PhysicsRaycaster component
然後呢....
場景中 3D game object 就可以接收到 EventTrigger component 支援的所有事件了!
雖然我還是沒有正確接收到所有 mouse events
(OnMouseEnter, OnMouseExit, OnMouseDown, OnMouseOver ....)
但已可以實現很完整的一系列事件了
(OnPointerEnter, OnPointerExit, OnMove, OnDrag, .... 等)

可是事情總是沒有憨人想的這麼簡單 =  =
EventSystems 發出的 raycasted events 僅在 RenderTexture resolution = real screen resulotion的情況下正確運作
如果 RenderTexture大小與 screen大小不符合
接收到事件的 pointer (mouse) position 就會出錯
這.....不知道算是誰的鍋了

難怪 forum 裡面建議的還是自己延伸改寫 GraphicRaycaster / PhysicsRaycaster ...

沒有留言:

張貼留言