Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting non serialized data in custom property drawer

Discussion in 'Scripting' started by cranky, Jan 22, 2017.

  1. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Hey guys,

    I've been out of the loop for a while and I'm just getting back into Unity. I'm trying to create a custom property drawer for a class I created. During runtime, I want to display some nonserialized data in the property drawer. How can I get a reference to the object I'm editing to do so?

    Example:

    Code (CSharp):
    1. [Serializable]
    2. public class Test
    3. {
    4.     public int serialized;
    5.  
    6.     [NonSerialized]
    7.     public int nonserialized;
    8. }
    How can I display the int nonserialized in the custom property drawer?

    Thanks!
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    I'm pretty sure that one of the aspects of the [NonSerialized] tag is that it makes public variables not sure up in the inspector. I don't think there is any way around this. I believe the inspector is dealing with the serialized objects down in the c++ layer.
     
  3. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    Looking at the docs there's a fieldInfo member of the PropertyDrawer which should allow you do do what you want via reflection.
     
  4. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180

    I don't believe FieldInfo can do what I need it to do, as GetValue requires a reference to the instance. Unless I am rusty on my reflection....
     
  5. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    I didn't look into it, so you're most probably correct.
     
  6. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Thanks anyways. I appreciate you taking the time to help.

    Anyone have any more ideas? Really stuck in a rough place, as l need this for future debugging.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    Grahammmm, coeing and cranky like this.
  8. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    Are you looking for something like:
    Code (csharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEditor;
    4. public class TestContainer : MonoBehaviour
    5. {
    6.     [SerializeField] private Test Test;
    7. }
    8.  
    9. [Serializable]
    10. public class Test
    11. {
    12.     [SerializeField] private int Serialized;
    13.     [NonSerialized] public int NonSerialize;
    14. }
    15.  
    16. [CustomPropertyDrawer(typeof(Test))]
    17. public class TestDrawer : PropertyDrawer
    18. {
    19.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    20.     {
    21.         Test obj = fieldInfo.GetValue(property.serializedObject.targetObject) as Test;
    22.         Debug.Log(obj.NonSerialize);
    23.         // draw property stuff
    24.     }
    25. }
     
    cranky likes this.
  9. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Thank you for this, however, this didn't work as targetObject is a Unity object and not a .Net object. This means it does not represent Test, but instead the Unity object Test is a part of (TestContainer in your code example.)

    This is probably the solution I will use. Thank you very much for it. I will report back how it goes.

    EDIT: Works perfectly, thank you very much!

    Thanks everyone for the help.
     
    Last edited: Jan 24, 2017
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    Kru's code would have worked if the object was directly on the targetObject.

    But yes, because targetObject is the unity object, you sometimes have to dig deeper through multiple properties. Which is what my code does, it figures out if it's a field of a field of a field... also if it's in a list/array.
     
    CheekySparrow78 and forestrf like this.