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 /
  • Help Room /
avatar image
0
Question by Pablomon · Apr 11, 2018 at 06:22 PM · propertydrawergenerics

CustomPropertyDrawer for a concrete class of a generic abstract

Hi!

So I in pursue of a reference system that allows me to not hardcode things in the inspector I created a "FloatReference" class that holds a scriptableObjet variable so I can drag values from my project to my scene.

 public class FloatReference : MonoBehaviour
     {
         public bool useConstant = true;
         public float constValue;
         public FloatVariable variable;
     
         public float value
         {
             get { return useConstant ? constValue : variable.value; }
         }
     }

I create a custom property for convenience:

     [CustomPropertyDrawer(typeof(FloatReference))]
     public class FloatReferenceDrawer : PropertyDrawer
     {
         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
         {
             SerializedProperty useConstant = property.FindPropertyRelative("useConstant");
             SerializedProperty constValue = property.FindPropertyRelative("constValue");
             SerializedProperty variable = property.FindPropertyRelative("variable");
     
             EditorGUI.BeginProperty(position, label, property);
     
             useConstant.boolValue = EditorGUI.ToggleLeft(r, "Use Constant", useConstant.boolValue);
             
             r.xMin += 95;
             r.xMax = position.xMax;
             r.height = EditorGUIUtility.singleLineHeight;


             // EXCEPTION HERE!
             if (useConstant.boolValue)
             {
     /*
     more code that worked before I made it derive from a generic class...
     */
         }
 }

So far so good. Then trying to refine my system I thought it would be awesome to get FloatReference class to derive from a generic abstract Reference class as follows:

public class Reference : MonoBehaviour { public bool useConstant = true; public T constValue; public Variable variable;

 public T value
 {
     get { return useConstant ? constValue : variable.value; }
 }

}

so my FloatReference, IntegerReference, ColorReference, etc get simplified to:

 [Serializable]
 public class FloatReference : Reference<float> 
 {
 }

And then my custom property throws a null reference exception and I don't understand why.

Any help? thanks

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

2 Replies

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

Answer by Adam-Mechtley · Apr 11, 2018 at 06:52 PM

It's a little unclear here, but assuming that FloatReference in fact inherits Reference<float>, the problem here is that a FloatReference field on some object is serializing a reference to a UnityEngine.Object (since Reference<T> inherits MonoBehaviour). Unless I'm missing something, it looks like Reference<T> should instead inherit System.Object.


Explanation:

In Unity, when you have a serialized field to of a type that inherits from UnityEngine.Object, you are serializing a reference to some object. Serializing a struct or a class that does not inherit from UnityEngine.Object will serialize a copy of the object. You access SerializedProperties of fields on a copy using e.g., SerializedProperty.FindRelative(). In the case of a reference, you access the reference itself via SerializedProperty.objectReferenceValue. In order to access any of the fields on this reference, you then need to wrap it in a wholly separate serialized data stream (e.g., var so = new SerializedObject(property.objectReferenceValue);), in which case you also need to properly manage updating and dirtying this object's serialized data stream and so on.


See the serialization documentation, specifically the section When might the serializer behave unexpectedly?

Comment
Add comment · 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
0

Answer by Pablomon · Apr 12, 2018 at 09:19 AM

Thank you Adam, that did it. But why? Doesn't Monobehaviour inherit from Object?

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 Adam-Mechtley · Apr 12, 2018 at 11:17 AM 0
Share

I've update my answer above with more info. If that clears it up you can mark it as the accepted answer :)

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

132 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

Related Questions

How to create Generics Property Type Property Drawer 0 Answers

Removing / modifying in Inspector a generic variable derived from a non-generic variable 1 Answer

Need quick editor scripting help 1 Answer

Inheritance, List and PropertyDrawer,CustomPropertyDrawer and Inheritance 0 Answers

Dynamically create lists and their names? 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