Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by vexe · Nov 03, 2014 at 01:44 AM · editorinspectorreflection

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Nov 03, 2014 at 02:47 AM 0
Share

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 ;)

avatar image Bunny83 · Nov 03, 2014 at 03:06 AM 0
Share

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.

avatar image vexe · Nov 03, 2014 at 10:38 AM 0
Share

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...)

avatar image Bunny83 · Nov 03, 2014 at 01:54 PM 0
Share

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 ;)

Anyways mixing fields and properties isn't good style ;)

avatar image vexe · Nov 03, 2014 at 02:29 PM 0
Share

@Bunny83

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges