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

Resolved Disable IntegerField changing value on drag

Discussion in 'UI Toolkit' started by achimmihca, Jun 13, 2023.

  1. achimmihca

    achimmihca

    Joined:
    Feb 13, 2016
    Posts:
    266
    I noticed that my IntegerFields change the value when I drag the label left / right.
    This is nice in general. However, for runtime UI I want to disable it.

    I think this behaviour may confuse players. Furthermore, it conflicts with manual drag-to-scroll that I implemented for desktop platforms.

    How can I disable the "drag to change value" feature of an IntegerField? I did not find a corresponding property.

    Unity 2023.3.0f1
     
    oscarAbraham likes this.
  2. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    I don't think there's a built-in way to do it in UXML or the builder, but you can do it from code with StopDragging. You could make child classes of these fields that call StopDragging in their constructor for a custom way of doing it in UXML.

    EDIT
    Sorry. This doesn't work. I know it can be done. Let me try to remember and I'll get back to you later if there isn't an answer yet.
     
    Last edited: Jun 13, 2023
  3. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    Hi. I had a busy day today, so I couldn't fix my answer until now. I hope it still helps.

    I can't remember how I used to do it, but I took a quick look at it. This is the easiest way (it's a bit dirty, though):

    Code (CSharp):
    1.  
    2.             var intField = new IntegerField("My Int");
    3.             intField.labelElement.style.cursor = StyleKeyword.Initial;
    4.  
    5.             intField.labelElement.RegisterCallback<MouseMoveEvent>(
    6.                 e => e.StopImmediatePropagation(), TrickleDown.TrickleDown);
    7.  
    8.             intField.labelElement.RegisterCallback<PointerMoveEvent>(
    9.                 e => e.StopImmediatePropagation(), TrickleDown.TrickleDown);
    10.  
    It basically intercepts mouse/pointer move events in the label before they can affect the value when dragging. It also sets the cursor image of the mouse when hovering the label to normal.

    If that feels too ugly, I took a look at Unity's code and it seems quite easy to roll your own custom, non-draggable TextValueFields. You can do something like IntegerField, but without calling AddLabelDragger and implementing ApplyInputDeviceDelta.

    That said, dear UI team at Unity, please add my vote in favor of the idea of adding a property+attribute that controls label dragging in numeric fields. It would be easy to implement and it would be great for non-editor stuff.
     
  4. achimmihca

    achimmihca

    Joined:
    Feb 13, 2016
    Posts:
    266
    This works perfectly fine for me :) .
    Thank you for the help and the fast response!
     
    oscarAbraham likes this.