mikmueller
Topic Author
Posts: 2
Joined: 04 Jun 2019, 12:50

Trouble relaying MouseEvents

05 Aug 2019, 11:11

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):
_view.MouseMove(_localPosX, _localPosY);
it basically totally ignores the following:
 _view.MouseButtonDown(_localPosX, _localPosY,MouseButton.Left);
 
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!

Tags:
 
User avatar
jsantos
Site Admin
Posts: 2563
Joined: 20 Jan 2012, 17:18
Contact:

Re: Trouble relaying MouseEvents

08 Aug 2019, 12:25

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).
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:

"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
Topic Author
Posts: 2
Joined: 04 Jun 2019, 12:50

Re: Trouble relaying MouseEvents

08 Aug 2019, 12:58

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)

Image

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 ;)
 
User avatar
jsantos
Site Admin
Posts: 2563
Joined: 20 Jan 2012, 17:18
Contact:

Re: Trouble relaying MouseEvents

08 Aug 2019, 13:03

Thanks! I am sure it will be helpful.

Who is online

Users browsing this forum: Google [Bot] and 4 guests