Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

[Solved] Allow overlapping buttons in Unity UI to all process the click event.

Discussion in 'Scripting' started by Deleted User, Apr 20, 2017.

  1. Deleted User

    Deleted User

    Guest

    I have 2 buttons that overlap a bit. I want that if the overlapping area is clicked, both buttons get triggered. I have no idea where to start.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    You could create a hidden third button that just covers the part where they overlap so that it could trigger both buttons when it's pushed.
     
  3. Deleted User

    Deleted User

    Guest

    That wouldn't work in my case. I did just find a very flexible way to do this:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine.EventSystems;
    3.  
    4. namespace MyUI
    5. {
    6.     public sealed class EventBubbler : MonoBehaviour, IPointerClickHandler, IPointerUpHandler, IPointerDownHandler
    7.     {
    8.         private void Bubble<T>(PointerEventData eventData, ExecuteEvents.EventFunction<T> eventFunction) where T : IEventSystemHandler
    9.         {
    10.             var allRaycasted = new List<RaycastResult>();
    11.             EventSystem.current.RaycastAll(eventData, allRaycasted);
    12.             foreach (var raycasted in allRaycasted)
    13.             {
    14.                 if (raycasted.gameObject == gameObject)
    15.                 {
    16.                     continue;
    17.                 }
    18.                 ExecuteEvents.Execute(raycasted.gameObject, eventData, eventFunction);
    19.             }
    20.         }
    21.  
    22.         void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
    23.         {
    24.             Bubble(eventData, ExecuteEvents.pointerClickHandler);
    25.         }
    26.  
    27.         void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
    28.         {
    29.             Bubble(eventData, ExecuteEvents.pointerDownHandler);
    30.         }
    31.  
    32.         void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
    33.         {
    34.             Bubble(eventData, ExecuteEvents.pointerUpHandler);
    35.         }
    36.     }
    37. }
    Just place it on the GO you want to allow to bubble events.
     
    Ronsu900 likes this.
  4. Ronsu900

    Ronsu900

    Joined:
    Mar 15, 2015
    Posts:
    8
    @supremegrandruler
    I was looking for ways to make this possible for several hours..
    that script very helpful for me, thanks!