Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Servail · Jul 10, 2019 at 03:20 PM · serializationeventjsonreflectionutility

Is it possible to serialize UnityEvent through reflection?

When I do:

 typeof(UnityEvent).GetMembers(); 

it returns nothing but methods visible from code, which are useless in serialization. Same for UnityEventBase. But when I do:

 JsonUtility.ToJson(myUnityEvent);

It returns JSON-formatted string with all those "hidden" members (like m_Calls), which I needed. Yes, now I can replace instanceIDs with something more persistent on serialization, then replace back on deserialization. But how does JsonUtility got these members? Is it some Unity internal black magic? Then why so serious? Are UnityEvents outdated?

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 · Jul 10, 2019 at 09:19 PM

In this case it's not Unity related. You just have to learn how to use reflection if you really need it.


First of all you have to look at the actual implementation of the class(es). You will notice that most used classes are actually private classes. So you can't use those classes directly and everything you want to do with them has to be done through reflection.


Next you should learn to read documentation. The .NET framework has a great documentation. As you can see the GetMembers method has two overloads, one without any parameters and one with BindingFlags. The BindingFlags enum is a bit mask enum. So you can combine several values with the bitwise or operator. By default you will only find public and instance members. However NonPublic members (private, internal, protected) as well as static members will not be found. You can combine all 4 values (Instance | Static | Public | NonPublic) to get literally all members of a type.


If it's about serialization you usually only have to care about fields. Members could mean anything which includes fields, properties, (.NET)events, methods, nested types and some others. If you're just interested in Fields, you should use GetFields instead.


Note that the UnityEvent class is purely implemented in .NET managed code. Therefore you can access all information through reflection. However the way it's designed (and because most of the other required classes are private) it's a mess to deal with UnityEvents manually through reflection. Though nothing is impossible.


It's a different story for built-in types which are not implemented in managed code but actually in native C++ code in the engine core. Unity's serialization system actually serializes the data on the native side. Therefore Unity has access to all fields that are actually implemented on the native side. For those classes Unity provides methods (actual methods or getter / setter) which are implemented in native code to read / write values of that class. The prime example is the Transform component. It doesn't contain a single field in managed code. Everything is a property or method that is implemented on the native side. So in those cases you can only use the methods the class provides. Reflection allows you to call private or internal methods. Though if Unity doesn't provide a method to access some internal data there's no way to get it. In some cases Unity's serialitation system might have access to data you don't get otherwise.

Comment
Add comment · Show 1 · 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 Servail · Jul 11, 2019 at 11:43 AM 0
Share

Oh, THAT'S why! "Get$$anonymous$$embers() returns all the public members of the current Type." I (mis?)red documentation, but for some reason thought that Get$$anonymous$$embers return literally all members. So I just can:

 typeof(UnityEventBase).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

...then use these to get values directly from UnityEvent.

Thanks, Sir, your information was very helpful!

P.S.: Another method to get all members is:

 typeof(myClass).GetFields((BindingFlags)(-1));

Just in case.

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

117 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Binding JSON contents to ui sliders 0 Answers

JSON-Serializer (Renewed open) 0 Answers

How do I go about deserializing a json array? 3 Answers

Json class will serialize but wont deserialize 1 Answer

UnityEngine.Object field pointing to Project file gets serialized by JSON as instance ID. After deserialization field points to different Object. Why? 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