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

Question Hexadecimal integers in the Inspector?

Discussion in 'Scripting' started by HookedWit, Jul 6, 2023.

  1. HookedWit

    HookedWit

    Joined:
    Dec 31, 2022
    Posts:
    31
    I have created a script component which contains a number of integers which would more conveniently be entered & displayed in the Inspector as hexadecimal, rather than decimal - but I can't find a way to do this (seems like it should be an attribute?).

    I can do the conversion in my head, but this is not quite so convenient!

    Am I missing something & there is a way to do this (I have done a lot of searching & nothing has shown up)?
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    You need to write a custom inspector for things like that. Here is an example.
     
    Bunny83 likes this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Or just use a string field if you don't mind that the conversion may fail for invalid user entries and requires extra handling code for invalid entries. Convert to and from hex strings is simple.
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Great occasion for using the
    OnValidate
    function. :)
     
  5. HookedWit

    HookedWit

    Joined:
    Dec 31, 2022
    Posts:
    31
    I think I'll just use a string field. Thanks for the responses. Must say I'm surprised a hex formatting attribute isn't standard.
     
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,317
    It is hard to read and rarely used. Hex comes into play when you use bitmasks, and for bitmasks there's already flags
     
    Bunny83 likes this.
  7. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    Really I'm not sure if there'd be much call for it. I used hex a lot back in the old days when I was memory mapping my dad's Apple IIe, but now I'm curious what you'd need hex formatting for in this day in age. Surely you'd want to abstract that away if at all possible.
     
  8. emotivci

    emotivci

    Joined:
    Aug 13, 2023
    Posts:
    1
    Added a hexadecimal convertor for integers when the display as hexadecimal option is enabled.
    When the update is live, I'll post again.
     
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,084
    Colors are the only things I can think of, but colors would be handled by a color picker.

    What advantage does it have over the one posted by @Lurking-Ninja?
     
    Last edited: Aug 13, 2023
    Bunny83 likes this.