The thing is, the View is rendered on a RawImage (via RenderTexture) and lies within the Unity-GUI. Therefore, a MeshCollider does nothing.
I managed to write a Script that relays most of the inputs and works quite good for the set-up you see in the screenshot. (Important: Deactivate
Mouse Input (and Touch probably too) as it conflicts otherwise)
And here's the Code (ignore the filename, it's not using any raycast anymore):
using UnityEngine;
using UnityEngine.UI;
using Noesis;
using UnityEngine.EventSystems;
public class NoesisRayCaster : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
[SerializeField] private NoesisView _view;
private RawImage _raw;
private UnityEngine.Texture _texture;
private Camera _cam;
private bool _pointerInside;
private int _localPosX, _localPosY;
private bool[] _mouseDown, _mouseUp;
public void OnPointerEnter(PointerEventData eventData)
{
_pointerInside = true;
}
public void OnPointerExit(PointerEventData eventData)
{
_pointerInside = false;
}
void Awake()
{
_raw = GetComponent<RawImage>();
if (_raw != null)
{
_texture = _raw.mainTexture;
}
_mouseDown = new bool[5];
_mouseUp = new bool[5];
_cam = Camera.main;
}
void Update()
{
if (!_pointerInside) return;
UpdateMousePosition();
UpdateMouseButtons();
UIMousePositionUpdate();
UIMouseUpUpdate();
UIMouseDownUpdate();
}
void UpdateMouseButtons()
{
int totalMouse = _mouseDown.Length;
for (int i = 0; i < totalMouse; i++)
{
_mouseDown[i] = Input.GetMouseButtonDown(i);
}
for (int i = 0; i < totalMouse; i++)
{
_mouseUp[i] = Input.GetMouseButtonUp(i);
}
}
void UpdateMousePosition()
{
Vector2 size = new Vector2(_raw.rectTransform.rect.width, _raw.rectTransform.rect.height);
Vector3 normalizedViewportPosition = _cam.ScreenToViewportPoint(Input.mousePosition);
_localPosX = Mathf.RoundToInt(_texture.width * normalizedViewportPosition.x);
_localPosY = _texture.height - Mathf.RoundToInt(_texture.height * normalizedViewportPosition.y);
}
void UIMousePositionUpdate()
{
//Relay MousePosition
_view.MouseMove(_localPosX, _localPosY);
if (Input.mouseScrollDelta.y != 0)
{
_view.MouseWheel(_localPosX, _localPosY, Mathf.RoundToInt(Input.mouseScrollDelta.y));
}
}
void UIMouseUpUpdate()
{
//Relay MouseButtons
int totalMouse = _mouseUp.Length;
for (int i = 0; i < totalMouse; i++)
{
if (_mouseUp[i])
{
_view.MouseButtonUp(_localPosX, _localPosY, (MouseButton)i);
}
}
}
void UIMouseDownUpdate()
{
//Relay MouseButtons
int totalMouse = _mouseDown.Length;
for (int i = 0; i < totalMouse; i++)
{
if (_mouseDown[i])
{
_view.MouseButtonDown(_localPosX, _localPosY, (MouseButton)i);
}
}
}
}
Maybe it can be of use for anybody ;)