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. Dismiss Notice

Extending Selectable

Discussion in 'UGUI & TextMesh Pro' started by Democide, Mar 16, 2016.

  1. Democide

    Democide

    Joined:
    Jan 29, 2013
    Posts:
    315
    So, I have some issues with the default Selectable. One of them being the fact that you can only Transition one graphic (imagine a button with text, you want to transition both, maybe even differently. Now imagine a button with text and an icon on it...). The second issue is that you can not select buttons that are disabled (terrible for controller/keyboard use).

    My idea was to simply extend button and/or selectable, but looking at it most of the important things are private or otherwise inaccessible, so you cannot inherit from Selectable. Instead you would have to build the entire thing again. Besides being redundant, this also has the issue that the nice inspector ui is then gone.

    Is there any way around this?
     
    GibTreaty likes this.
  2. Vallcrist

    Vallcrist

    Joined:
    Aug 24, 2012
    Posts:
    8
    Well, The transition thing can be resolved by using the animation transition, with animation you can change whatever you like in the object/its children.
     
  3. Democide

    Democide

    Joined:
    Jan 29, 2013
    Posts:
    315
    Thanks, but that means I need an animation controller for some simple recoloring..
     
  4. Democide

    Democide

    Joined:
    Jan 29, 2013
    Posts:
    315
    Question: Is it possible to edit the source, build the DLL and then replace the existing DLL with a new one?
     
  5. Vallcrist

    Vallcrist

    Joined:
    Aug 24, 2012
    Posts:
    8
    What's the problem in using animators, however? For the UI they are fairly simple, don't cost a lot and are very flexible.

    I've been using animators a lot for UI things lately, and even with upwards of 100 animators "active" at the same time it takes less than 0.2ms per frame
     
  6. Vallcrist

    Vallcrist

    Joined:
    Aug 24, 2012
    Posts:
    8
    Plus, you can reuse the same animator on similar widgets with the same structure ( so, 1 animator for all your buttons ).

    I understand that animators are a bit bad to use, specially because they are messy in the project view, my company has a free asset that deals with that, however, letting you organize your animators better. Here's an example :

    And here's the link if you want to know more about it : http://u3d.as/mF1
     
    eses likes this.
  7. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Yes, you can replace the UI source with your own compiled versions, there are instructions on the BitBucket site for that. Although I did see some posts with others having trouble doing so. but you should be able to either:
    * replace the dll's in your install folder with compiled ones (just be sure to delete your projects library folder!)
    * Add the Dll's to your project which should override the default one's on build.

    However, it is just as simple to copy the class in question and rename it as your own, a lot less hasstle unless your editing a base class.

    Animatable is probably the best route, but if you don't want to do that, simply write your own script controller that manipulates the properties of child or specific objects manually or on a curve.
     
  8. Democide

    Democide

    Joined:
    Jan 29, 2013
    Posts:
    315
    That works, if you make sure all other interactables are also then child of your new selectable. Since the new class is no child of Selectable, navigation doesn't work across the original and the new selectable class. It IS the cleaner route though...
     
    SimonDarksideJ likes this.
  9. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
  10. Democide

    Democide

    Joined:
    Jan 29, 2013
    Posts:
    315
    Well, I have at this moment extended Selectable, though a lot of the code in there is pretty hacky. Too much so for my taste. In either case I then built a new button based on that extended selectable.

    I'm not really happy about that whole setup though. I may just build a completely new selectable base class and then duplicate all the interactive elements I need for that one. That makes the solution a lot cleaner, but also more fragile, if Unity changes something in the Selectable behavior.

    IMHO it would have been nicer of them to create the baseSelectable class and make this really minimal (maybe abstract, maybe only navigation, no visuals). Then have a child off of that with the visual stuff. That way I could build my own selectable off that base class in a much cleaner way...

    @phil-Unity, I'm guessing it's a bit too late for that, no? ;)
     
    noio likes this.
  11. IsaiahKelly

    IsaiahKelly

    Joined:
    Nov 11, 2012
    Posts:
    418
    That's what animation transitions are for. The built-in transitions are only there for very basic stuff. An animation is perfect for anything more complicated, and can be easily reused on different UI objects with the same type elements and layout.

    Lets say you add an extra transition to the Selectable class, but then decided you need another, and another, etc. This will get very messy. Ideally, you could create a new Selectable that contains a list to store all the basic transitions you want. But then you'd need to set the color/sprite for every element for every state! An animation transition is super clean and simple by comparison.

    I don't understand the issue here. That is the whole point of being disabled. You can still navigate around disabled buttons. Automatic navigation is best for this because it will automatically skip disabled elements, but explicit navigation works too; You just can't see when a disabled element is selected.

    I'm not sure why you're so set against using an animation transition here. Your workaround seems to be way more complicated and troublesome, but to each his own I guess.

    If you're really determined to go down this path, then I'd think a custom button that inherits from Selectable with it's own custom transitions would maybe work better.
     
    Last edited: Mar 26, 2016
  12. Rugbug_Redfern

    Rugbug_Redfern

    Joined:
    Jun 2, 2017
    Posts:
    20
    Well, no one has posted here since 4 years ago, but one reason for not wanting to use the animator component is the preference of tweening
     
  13. Lipefe

    Lipefe

    Joined:
    Jul 14, 2017
    Posts:
    10
    Plus in the case that UI is driven by backend it is a lot easier to store binary data for colours and textures but not for crap animations, also Unity Animation Controller redraws the entire UI component at every transition, garbage!!!
     
    noproblemwiththat likes this.
  14. arielcorrea

    arielcorrea

    Joined:
    Feb 21, 2019
    Posts:
    9
    No one has posted here in a long time, but I thought to leave my contribution to the issue. In my project I ended up creating my own "button" class and adding an EventTrigger component to it on the fly.

    upload_2023-9-28_4-36-22.png

    The events are triggered by the standard Unity event pipeline which was enough for me. In my case this was a VR project and I needed to grab a reference to the correct hand controller whenever Hover or Activate was triggered, so I could send back haptic feedback.