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 Min-Max structure

Discussion in 'Scripting' started by PasVol, Sep 2, 2023.

  1. PasVol

    PasVol

    Joined:
    Feb 5, 2016
    Posts:
    12
    Hi.
    Does Unity have a CSharp structure with Min and Max parameters?
    Min Max.png
    I often use Min and Max values.
    These prefixes constantly have to be specified in variable names:
    speedMin, speedMax; widthMin, widthMax; distanceMin, distanceMax....:confused:
    This data takes up too much space in the Inspector window and this window looks overloaded for reading: four variables occupy four lines and this makes it difficult to read the parameters.
     
    Bunny83 likes this.
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Well, yes and no ^^. Unity has the Vector2 struct which has two values and it was often used for that purpose. Unity even has a MinMaxSlider that works with Vector2 values. Unfortunately Unity doesn't provide a property drawer / attribute for this slider. Fortunately others have created just that. So a serialized Vector2 field with that "MinMaxSlider" attribute (given you have added the slider to your runtime code and the property drawer in an "editor" folder) would display the min max slider. As you can see from the code it has several additional options like rounding, if additional input fields should be displayed and so on.

    Though I you just want two input fields, you can just use a Vector2. You can also create your own property drawer that just shows two input fields if you don't like the min max slider or modify the slider drawer and add another boolean to the attribute to hide the slider? It's up to you. You also don't need to use Vector2 if you prefer to use your own struct. Though Vector2 has direct support by the SerializedProperty system. So when using custom serializable structs you have to reach out for the individual fields yourself.
     
    Kurt-Dekker likes this.
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    I also made a
    Range<T>
    type, feel free to try it in the package or cut it out and use it for your needs.
    Here is the docs (and the code is there too in github).
     
  4. PasVol

    PasVol

    Joined:
    Feb 5, 2016
    Posts:
    12
    Yes, I had the idea to use a Vector2, but it's also inconvenient to read the code.
    It looks like I'll have to use a custom structure.