Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

PropertyDrawer for a property is not executed when property is an array?

Discussion in 'Scripting' started by Freaking-Pingo, Aug 4, 2014.

  1. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    -----------------------
    Edit: SOLUTION
    -----------------------

    I have found the solution, and it was actually very simple. EditorGUI.PropertyField(instanceOfNextQuest) have an overload which takes a bool as input. This bool defines whether the PropertyField is suppose to draw any child properties. In order to draw an array using EditorGUI.PropertyField() this bool must be set to TRUE

    EditorGUI.PropertyField(someProperty, true, null)


    -----------------------
    PROBLEM
    -----------------------



    Hi there uniteers.
    In my current editor script, I have a class called NextQuest which is drawn through the use of EditorGUI.PropertyField(instanceOfNextQuest).

    Code (CSharp):
    1. [System.Serializable]
    2. public class NextQuest : System.Object
    3. {
    4.    // Some logic
    5. }
    NextQuest have its own proprety drawer:

    Code (CSharp):
    1. [CustomPropertyDrawer(typeof(NextQuest))]
    2. public class NextQuestDrawer : PropertyDrawer
    3. {
    4.  
    5.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    6.     {
    7.         // Some logic
    8.     }
    9. }

    The OnGUI is being executed when NextQuest is being declared as an ordinary variable. The OnGUI is NOT executed when NextQuest is declared as an array of NextQuests.

    Code (CSharp):
    1.  
    2. public class QuestClass : ScriptableObject
    3. {
    4.    NextQuest nextQuest; // OnGUI is called
    5.    NextQuest[] nextQuestArr; // OnGUI is NOT called
    6. }
    What am I missing here? I am pretty sure this worked earlier. We just upgraded to 4.5.3.f1, but I can't find any notes or comments about bugged PropertyDrawers for arrays.
     
    Last edited: Aug 5, 2014
    rakkarage likes this.
  2. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    Oh well, I am beginning to think its a bug now. I just noticed one of my previous PropertyDrawers which made use of an array doesn't work anymor. I know this worked earlier, because I had used it on multiple occuations.

    I would appreciate if someone out there could either confirm or disprove this.
     
  3. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    Okay, either I am doing something wrong here, or there is a somewhat weird bug going on. It appears that EditorGUI.PropertyField() can't draw any arrays properly anymore. It doesn't work with int, enums, strings or anything. I have attached an example. List of ints is a List<int>, while Ordinary int is just an int. They are both drawn using the EditorGUI.propertyfield().

     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Property drawers are limited to very simple cases. As soon as it gets a little more complex like with arrays or lists, they aren't well suited anymore. At least that was my experience which seems to be close to yours, unfortunately.
     
  5. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    You are properly right, but I am no longer talking about custom property drawers. Unity is having problems drawing simple array properties such as arrays of ints, string and enums, without the use of custom drawers.
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I always had to use custom drawers for arrays, not just for the elements, because it simply didn't work otherwise.
     
  7. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    Even for standard types? For a long time I haven't had to use any property drawers, even custom classes. This is the first time I encounter this. I am currently downloading 4.5.1 to see whether its a version bug.
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  9. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    I just downgraded to 4.5.1. Problem stil occuring...
     
  10. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    I am still wresting with this issue. Anyone have any clues or ideas of this behavior? I am trying now to go even further back to 4.3.2, but I am not sure whether it will work. I can't figure out whether it is an unity bug or simply just something stupido from my site.
     
  11. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    Okay, this was a bit akward. I have found the solution, and it was actually very simple. EditorGUI.PropertyField(instanceOfNextQuest) have an overload which takes a bool as input. This bool defines whether the PropertyField is suppose to draw any child properties. In order to draw an array using EditorGUI.PropertyField() this bool must be set to TRUE

    EditorGUI.PropertyField(someProperty, true, null)