-
- mikmueller
- Posts: 3
- Joined:
Trouble relaying MouseEvents
Hi!
I'm having a little issue with relaying MouseEvents, especially Clicks.
While I can easily relay the Mouse-Position like this (and verify it through MouseOver-Effects):
it basically totally ignores the following:
Any idea why this is not relayed as expected?
The thing is, we have Noesis running on a RenderTexture in Unity, which is placed in Camera-Space (therefore, Inputs need to be relayed manually, as I couldn't find any pre-integrated solution like in Screen-Space).
Many thanks in advance!
I'm having a little issue with relaying MouseEvents, especially Clicks.
While I can easily relay the Mouse-Position like this (and verify it through MouseOver-Effects):
Code: Select all
_view.MouseMove(_localPosX, _localPosY);
Code: Select all
_view.MouseButtonDown(_localPosX, _localPosY,MouseButton.Left);
The thing is, we have Noesis running on a RenderTexture in Unity, which is placed in Camera-Space (therefore, Inputs need to be relayed manually, as I couldn't find any pre-integrated solution like in Screen-Space).
Many thanks in advance!
Re: Trouble relaying MouseEvents
There is already a pre-integrated solution if your GameObject contains a MeshCollider. In that case, we automatically relay from 2D to 3D. It is commented in the Unity tutorial:The thing is, we have Noesis running on a RenderTexture in Unity, which is placed in Camera-Space (therefore, Inputs need to be relayed manually, as I couldn't find any pre-integrated solution like in Screen-Space).
"If you want to interact with the GUI rendered in the texture, your GameObject must have a MeshCollider component so texture coordinates can be obtained when doing the hit testing projection."
Please, let us know if this works for you.
-
- mikmueller
- Posts: 3
- Joined:
Re: Trouble relaying MouseEvents
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):
Maybe it can be of use for anybody ;)
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):
Code: Select all
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);
}
}
}
}
Re: Trouble relaying MouseEvents
Thanks! I am sure it will be helpful.
Who is online
Users browsing this forum: No registered users and 2 guests