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 WillNode · Oct 08, 2016 at 07:33 AM · c#editorpropertydrawerserializedproperty

Call a function from CustomPropertyDrawer of Arbitrary class

Hi, So I have this class

 public class SomeClass {
    public int number;
    public void SomeFunction() {}
 }

This class is used in many part of my GameObject components, so If i want to create a custom editor for this class, i have to use CustomPropertyDrawer.

 [CustomPropertyDrawer(typeof(SomeClass))]
 public class ComExpressionEditor : PropertyDrawer
 {
 
     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
     {
                      // Do something magic here.
     }
 }

But how i can call my SomeFunction() from there? I've tried property.serializedObject.targetObject but it makes a compiler error since my SomeClass is an arbitrary (Not Unity.Object) class. Is there any workaround for this?

Comment
Add comment · Show 2
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 Sergio7888 · Oct 08, 2016 at 07:49 PM 0
Share

PropertyDrawer are made change how a field is show in the editor and is not intended to use to edit, but you can get the field value using SerializedProperty.objectReferenceValue.

avatar image Bunny83 Sergio7888 · Oct 09, 2016 at 02:24 AM 1
Share

PropertyDrawers are ment to provide GUI controls to edit the property but through the SerializedProperty and not by direct manipulating objects.

Also "objectReferenceValue" does only work for classes that are serialized on their own (classes that are derived from UnityEngine.Object). So this only applies either for built-in components and classes or for your own classes that are derived from $$anonymous$$onoBehabiour / ScriptableObject.

1 Reply

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

Answer by Bunny83 · Oct 09, 2016 at 02:19 AM

That's actually not possible with massive useage of reflection as a SerializedProperty only represents the seraializable data and not the actual class instances. Classes derived from UnityEngine.Object are a different story. For those (usually MonoBehaviour or ScriptableObjects) you can use SerializedProperty.objectReferenceValue to get the serialized reference to that object. However if it's just a serializable custom class the serialization system doesn't even know that instance exists. It's content is simply serialized along with the containing MonoBehaviour object (or ScriptableObject).

As i said there are ways to access the "current" instance by using reflection as shown by HiddenMonk and Johannski over here.

Despite it's possible, it's not a good approach. Unity's serialization system abstracted the data from the actual objects. A SerializedObject / SerializedProperty just represents the actual stored data and not the concrete object. Any manipulation is ment to be done through the SerializedProperty class.

I know this system has some limitations when it comes to more complex structures. If you really need to directly access a class instance you would need to use a seperate CustomEditor. PropertyDrawers aren't designed for what you want to do. They are ment to work on the abstracted representation. Keep in mind that a SerializedProperty can actually represent multiple objects at once in case you have more than one object selected.

A custom editor can access the any members of the MonoBehaviour / ScriptableObject through the "target" / "targets" variable(s). In that case you have to handle everything yourself including registering undo operations.

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 WillNode · Oct 09, 2016 at 06:39 AM 0
Share

Thank you very much for the link. the Hidden$$anonymous$$onk and Johannski's answer is works great. I was think it doesn't possible (took me hours to understand the code). But anyway, yes, as you warned. If I use a reflection for changing the values, then somehow the SerializedProperty doesn't update it's value. I need to make a distict usage between using SerializedProperty vs. Reflection.

Also +1 (and thanks) for the great explanation you give to me.

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

229 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 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

Initialising List array for use in a custom Editor 1 Answer

Insert new custom class element with _default_ values to a SerializedProperty array? 1 Answer

Reset a SerializedProperty to it's default value. 2 Answers

Get "this" SerializedProperty ? 1 Answer

Multiple Cars not working 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