- Home /
Is there a way to get all editable fields of a component in a script?
Hello, I am creating a custom window in which I want the user to select which properties of a certain prefab will be affected by a certain value (which isn't relevant for the discussion). Currently I am manually selecting and showing the fields I am interested in and showing them to the user - such as "Position X", "Rotation Z", etc... Is there I way via code that I can get all the editable fields for a certain component? This way I would be able to loop through all the components of a certain prefab and thus basically show the user all the fields that are visible for this prefab in the regular inspector. Thank you!
what you are talking about is highly possible but there is no included system for it with it in unity or C#. you would have to think outside the box and manually set up a system in your code for it. i would recommend keeping all editable variables in arrays so you have easy reference index numbers. possibly setting up corresponding string arrays that users can understand there inputs that line up to the index numbers of your arrays of "real" variables as a work around. but to answer your question, no... there is no easy or direct way to get all variables or let users change code without some sort of intuitive custom code set up for it
I would look at tagging those fields with custom Attributes, and using reflection to fetch the fields that have that the appropriate attribute.
Answer by FlaSh-G · Aug 06, 2017 at 09:27 AM
Have a look at SerializedObject for this. Alternatively, reflection would also work.