Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Question About UI Buttons Best Practices

Discussion in 'UGUI & TextMesh Pro' started by DockAnkh, Sep 4, 2023.

  1. DockAnkh

    DockAnkh

    Joined:
    Nov 25, 2021
    Posts:
    10
    I've been working on a strategy game which ends up with a lot of buttons in the UI. However, I've been indecisive about how to create buttons. I ended up creating buttons with three different methods and I think that I should really just unify them to one thing. Some use the "Button" component, some use the "Event Trigger" component, and for most I'm using raycasting to just see where the mouse is when clicked.

    Is one of these ways of making buttons considered better than the others? My instinct is to do everything with raycasting but I'm not sure. Even then I'm not sure if I should just have one place where all the raycasting is checked or separate ones for separate menus.

    What do you all think?
     
  2. msfredb7

    msfredb7

    Joined:
    Nov 1, 2012
    Posts:
    156
    Use the Button component.

    Issues with other methods:
    • EventTrigger component:
      • This component capture all UI events even if you don't use them (e.g. mouse enter, mouse exit, mouse wheel) because it implements all the event system interfaces. This causes issues when buttons are inside containers that require those events to function. For example, I believe adding an EventsTrigger component on a button inside a ScrollView will prevent the ScrollRect from scrolling, because the button captures the mouse wheel events.
      • Personal taste: Adding logic in UnityEvents in the inspector makes debugging and code-flow hard to understand.
      • Does not support gamepad controllers
    • Raycast:
      • You bypass the "Disabled" state of the button and end up clicking on things that are disabled.
      • Does not support gamepad controllers
    I worked at Ubisoft for a few years on a Unity project and we used exclusively Button component (actually we used our custom button class, which inherited from Button).

    The only place you should not use buttons, is when the target graphic is constantly moving. For example objects that are part of the game world like units in an RTS game or characters in a point and click game. For these types of "buttons", use world raycasting.
     
    Moran_Comunix and DockAnkh like this.