- Home /
Multiple canvases and graphic raycasters
Hi, I have a need for multiple canvases in my project (and thus multiple graphic raycasters). One is an overlay canvas, and the other is a camera space canvas. Currently only one canvas can detect input, the camera space canvas. If I disabled the camera space canvas's graphic raycaster, the overlay one can detect input, but on the contrary, the camera space canvas couldn't. The camera space canvas is needed to detect a 2D object that will behave like a button in the game. While the overlay is needed to detect the rest of the gui (menu, HUD, etc). So, how do I fix this? Thanks.
Answer by Mmmpies · Jan 24, 2015 at 12:28 PM
Don't know if this will help but it should report back all layers it passes through. I had it setup to act on a key press but no reason you couldn't use the top canvas OnClick to start the routine and report what it's gone through.
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
public class RaycastUI : MonoBehaviour {
public void RayCastingToUI ()
{
PointerEventData PED = new PointerEventData(EventSystem.current);
PED.position = Input.mousePosition;
List<RaycastResult> HITS = new List<RaycastResult>();
EventSystem.current.RaycastAll( PED, HITS );
foreach(RaycastResult hit in HITS)
{
GameObject go = hit.gameObject;
Debug.Log (go.name + " how many hit = " + HITS.Count);
}
}
}