Search Unity

Resolved Making a property have 2 entry points for variables

Discussion in 'Scripting' started by Hexolenn, Mar 7, 2023.

  1. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    In the unity editors inspector for some preset properties you can enter 2 or more variables from 2 or more entry points like so
    upload_2023-3-7_12-14-41.png
    In my script i need to get a start point and an end point for my property but i cant make it have 2 diffirent entry points
    upload_2023-3-7_12-12-0.png
    How can I make this like

    Talk Time
    Element0 ------ Start point [ 10 ] End Point [ 21 ]
    Element1 ------ Start point [ 25 ] End Point [ 48 ]
    Element2 ------ Start point [ 60 ] End Point [ 66 ]

    When I searched this in the internet I found nothing is there a set way to do this in unity itself ?
     
    Last edited: Mar 7, 2023
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Do you mean the x, y, z? This is just a vector3 value. Which is just a single variable. Vector3's are struts. If you want a start and end point, you can create your own strut and serialize it so it will display in the inspector with the 2 values. Then you just make a list of that strut.
     
  3. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    I have learned how to use vector2[] to get my variables sometime after posting this but I still cant change the naming of it. Right now it looks like this
    upload_2023-3-8_10-29-13.png
    Which is what I wanted but is there a way to rename these attributes as I said in my first post

    Instead of
    Element 0 --------- X [ 5 ] Y [ 10 ]

    Is there a way to make it say

    Element 0 --------- Start Time [ 5 ] End Tİme [ 10 ]

    For coding I know this is not necesery but I want it to be more understandble by others with out an explanation.

    Or if you hover your mouse over the variables over base unity components it tells you what it does by a pop-up window is there a way to make that for my variables ?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,860
    As mentioned, you can just make your own struct:
    Code (CSharp):
    1. [System.Serializable]
    2. public struct Timing
    3. {
    4.     public float StartTime;
    5.    
    6.     public float EndTime;
    7. }
    Then use a collection of that struct instead.
     
  5. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    I haven't used structs in unity yet so I didn't know it can work like this. Thank you it works like I wanted it to
    upload_2023-3-8_13-22-18.png

    Here is the code for it

    Code (CSharp):
    1. [System.Serializable]
    2. public struct Timing
    3. {
    4.     public float startTime;
    5.     public float endTime;
    6. }
    7. public Timing[] talkTime;
     
    Last edited: Mar 8, 2023
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    Ultimately if you want to present this in a custom way in the inspector, you'd have to write a so-called property drawer, which is how Unity inspectors are supposed to be extended in the editor. It's a pretty complex topic on its own, and what you did is a cheap and perfectly fine workaround if you just want to input some data manually and move on.