- Home /
How does Unity sort fields in order of their declaration?
If you have
public class Test : MonoBehaviour
{
public int x, y;
public Transform t;
public GameObject go;
}
In the inspector, you'll see these fields in this exact order! My question is how?
doing a: typeof(Test).GetMembers()
isn't guaranteed to return members the order they're declared in (src)
My stab is that they either use MonoCecil somehow to get them in that order, or convert the class code to string, parse the code, use regex to get the fields' names and then do type.GetField(name)
Edit: So in case of fields it seems that GetMembers
or GetFields
do indeed return the members/fields in the correct order. However; changing one of the fields to be a property will yield different results, i.e. a Transform t { get; set; } instead of Transform t;
I asked in MonoCecil's google group here if it could help or not, seems the only is via parsing the source code.
Answer by Bunny83 · Nov 03, 2014 at 02:40 AM
Well, to answer that question: we simply don't know as there is no way in pure C# / reflection to determine the order since it is irrelevant and the compiler might even change the order when compiling.
I have some counter questions:
Why is it important? :)
Have you tried GetMembers? The docs just state that you can't rely on a certain order since that's not specified in specification. However if the compiler outputs the members in the declaration order it's most likely that GetMembers will return the members in that order.
It is known that Unity uses the Cecil library a lot. But i don't think that they try to keep the declaration order "by any means necessary" since it doesn't really matter in most cases. Serialization happens based on the fields name.
A quick test in Visual Studio showed that Get$$anonymous$$ember returns all members in the declaration order, even when private members are in between. At least the fields. So i guess that Unity just shows them in that order. Since you can't make Get$$anonymous$$embers to return a different order you can't test if the order would change in Unity ;)
Just something to think about: You can compile your $$anonymous$$onoBehaviours scripts manually into an assembly and place it in your assets folder. There Unity doesn't have the source available and will most likely show the same order Get$$anonymous$$embers / GetFields returns.
Hey @Bunny83 thanks for your answer! - I did try Get$$anonymous$$embers, and it does return those fields in that order, however if you change for example the Transform t;
to be an auto property, the result will be different. Why is it important? I need it for my framework, I have all the drawing for members etc overridden, my custom serialization/drawing api treats fields and auto-props equally the same. I'm currently ordering members by their data type name, then by their name. It would be really nice if I can order them in the order of their declaration.
I asked in $$anonymous$$onoCecil's google group here, it seems I can't do it with $$anonymous$$C as well, I have to get the source and parse it, either via Roslyn or NRefactory (which Unity also uses btw...)
On the $$anonymous$$SDN pages they demand you to not rely on a particular order. Get$$anonymous$$embers returns the declaration order, but grouped by the member type:
declared methods including getter and setter methods.
static declared methods (including getter / settor of static properties)
inherited methods
constructor(s) / static constructor
properties (both static and instance)
instance fields
static / const fields
Since Unity only serializes fields there's no problem here ;)
I guess I'll just stick to my current categorizations. Using NRefactory/Roslyn, parsing etc is just not worth it...
mixing fields and properties isn't good style
I'm not sure why you said that, nor what you mean by it. Are you referring to the previous example replacing Transform t;
with Transform t { get; set; }
? that was just an example. or are you talking in general? - I use both fields and properties in my codes where it makes sense to use either.
Your answer
Follow this Question
Related Questions
How to have two inspectors of different types thats objects they are inspecting change 0 Answers
Make popup window in inspector if deleting GameObject 0 Answers
reflection propertyinfo.getvalue compiles fine but gives erros in editor 1 Answer
Drop down with sizes 1 Answer
ReImport in c# of GameObjects only for Scene Objects, not assets? 1 Answer