Search Unity

[Solved] A way for OnClick to ignore children?

Discussion in 'UGUI & TextMesh Pro' started by Melang, Aug 29, 2014.

  1. Melang

    Melang

    Joined:
    Mar 30, 2014
    Posts:
    166
    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?
     
  2. davebuchhofer

    davebuchhofer

    Joined:
    Nov 9, 2007
    Posts:
    126
    Replace GetComponents with appropriate variables for fun and profit, but this should do it.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class UnregisterGraphicFromRaycasting : MonoBehaviour {
    7.  
    8.     void OnEnable(){
    9.         GraphicRegistry.UnregisterGraphicForCanvas(GetComponent<Canvas>(),GetComponent<Graphic>());
    10.     }
    11. }
    12.  
     
    Melang likes this.
  3. Melang

    Melang

    Joined:
    Mar 30, 2014
    Posts:
    166
    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:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. [RequireComponent(typeof(Graphic))]
    8. public class UnregisterGraphicFromRaycasting : MonoBehaviour {
    9.  
    10.     void Start(){
    11.  
    12.         Canvas canvas = FindInParents<Canvas>(gameObject); //searching in parents because there can be multiple canvases
    13.  
    14.         GraphicRegistry.UnregisterGraphicForCanvas(canvas, GetComponent<Graphic>());
    15.     }
    16.  
    17.     static public T FindInParents<T>(GameObject go) where T : Component
    18.     {
    19.         if (go == null) return null;
    20.         T comp = go.GetComponent<T>();
    21.      
    22.         if (comp != null)
    23.             return comp;
    24.      
    25.         Transform t = go.transform.parent;
    26.         while (t != null && comp == null)
    27.         {
    28.             comp = t.gameObject.GetComponent<T>();
    29.             t = t.parent;
    30.         }
    31.         return comp;
    32.     }
    33. }
    34.  
     
  4. davebuchhofer

    davebuchhofer

    Joined:
    Nov 9, 2007
    Posts:
    126
    The other way, as I was looking for something else and loaded up the example project! :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class IgnoreRaycast : MonoBehaviour, ICanvasRaycastFilter
    7. {
    8.     public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
    9.     {
    10.         return false;
    11.     }
    12. }
    13.  
     
  5. stuck_cow

    stuck_cow

    Joined:
    Dec 5, 2018
    Posts:
    1
    this might be old, but in case anyone wondering, un-check 'Raycast Target' component of child in inspector will solve this problem.
     
    nixiaotang2004, CharlieCook and mbrmj like this.