I have a button with a child element that shouldn’t be clickable, but its RectTransform seems to trigger OnClick and PointerEnter on the parent button. Is there a way to disable this behaviour?
Replace GetComponents with appropriate variables for fun and profit, but this should do it.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UnregisterGraphicFromRaycasting : MonoBehaviour {
void OnEnable(){
GraphicRegistry.UnregisterGraphicForCanvas(GetComponent<Canvas>(),GetComponent<Graphic>());
}
}
1 Like
Thanks, it works nicely! I added it to Start() instead because OnEnable doesn’t seem to work for instantiated objects, and added a search for the canvas from the demo DragMe script.
In case anyone else needs this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[RequireComponent(typeof(Graphic))]
public class UnregisterGraphicFromRaycasting : MonoBehaviour {
void Start(){
Canvas canvas = FindInParents<Canvas>(gameObject); //searching in parents because there can be multiple canvases
GraphicRegistry.UnregisterGraphicForCanvas(canvas, GetComponent<Graphic>());
}
static public T FindInParents<T>(GameObject go) where T : Component
{
if (go == null) return null;
T comp = go.GetComponent<T>();
if (comp != null)
return comp;
Transform t = go.transform.parent;
while (t != null && comp == null)
{
comp = t.gameObject.GetComponent<T>();
t = t.parent;
}
return comp;
}
}
The other way, as I was looking for something else and loaded up the example project! :
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class IgnoreRaycast : MonoBehaviour, ICanvasRaycastFilter
{
public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
{
return false;
}
}
this might be old, but in case anyone wondering, un-check ‘Raycast Target’ component of child in inspector will solve this problem.
3 Likes