Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 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 Jeff-Kesselman · Jul 28, 2015 at 06:11 PM · event

UnityEvent as a serialized property

I have a field of a GameObject that is a concretized subclass of UnityEvent.

I am trying to write a property drawer for that concretized subclass.

But UnityEventBase does not seem to be a child class of Object so using property.objectReferenceValue does not return anything that I can cast to my sub-class.

How does one get the deserailized property where such a property is a subclass of UnityEvent?

Comment
Add comment · Show 1
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 Crazydadz · Jul 29, 2015 at 06:05 AM 0
Share

Funny thing, I'm working on a tool, and I was stuck with that one today. UnityEvent's propertytype is Generic and not ObjectReference. So we can't get access to the value or change anything from any serializedProperty value field...hope someone can help us :).

2 Replies

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

Answer by Jeff-Kesselman · Jul 29, 2015 at 02:32 PM

The answer from Unity support is thus:

UnityEvent is a "normal" class, you should be able to use SerializedProperty.Next() to drill down into its data but not objectReferenceValue.

SO, I guess we have to use the SerializedProperty sub-property parsing function to get at the data.

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 Bunny83 · Jul 29, 2015 at 02:41 PM 0
Share

Yes, "normal" seriaizable classes are serialized along with the containing type. If you force text-serialization in Unity you can look at any prefab or scene asset with a text editor and see how the data is actually structured. Unity can only reference things derived from UnityEngine.Object as they are serialized on their own and get a seperate instance id which can be used to reference the instance.

avatar image
3

Answer by Acegikmo · Apr 13, 2016 at 11:58 AM

Here's a workaround that lets you access the proper UnityEvent object instead of just the serialized data

     public UnityEvent GetUnityEvent(object obj, string fieldName){
         if( obj != null ) {
             FieldInfo fi = obj.GetType().GetField(fieldName);
             if( fi != null ) {
                 return fi.GetValue(obj) as UnityEvent;
             }        
         }
         return null;
     }

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 Rimoldi · Apr 09 at 05:51 PM 0
Share

It's important to point out that if the field is private, you also have to pass NonPublic and Instance BindingFlags when using GetField, like this:

 public UnityEvent GetPrivateUnityEvent(object obj, string fieldName){
          if( obj != null ) {
              FieldInfo fi = obj.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
              if( fi != null ) {
                  return fi.GetValue(obj) as UnityEvent;
              }        
          }
          return null;
      }

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

25 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

Related Questions

Particle Collision / Trigger not being reported to On_xx Event 1 Answer

Animation Event Lag 0 Answers

how do you add an event to an imported animation 1 Answer

Event-based spawning 1 Answer

How to interpret Event.current.delta? 0 Answers


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