Search Unity

Question Make ObjectField readonly?

Discussion in 'UI Toolkit' started by CDF, Oct 23, 2022.

  1. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,313
    Wondering if it's possible to make an ObjectField readonly?
    I still want to allow pinging the object and setting the value through code. But I don't want to allow changing the reference by drag and drop.

    calling "SetEnabled(false)" doesn't work, and actually still allows drag and drop but prevents ping.
     
  2. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    You could have it not be bound, and track the actual property with TrackPropertyValue instead. You'd update the field's value with SetValueWithoutNotify. You'd then prevent any direct changes to the field by listening to valueChangeEvent and setting the value to the previous value with SetValueWithoutNotify. You'd probably should disable the picker button too.

    It might be easier to just implement your own field, though.
     
  3. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,313
    Yeah that works. Thanks