Home > Software design >  Unity Inspector array with enum Index for class don't expand
Unity Inspector array with enum Index for class don't expand

Time:07-03

I have an array with a custom propertydrawer which have enums as index. But when i try to expand it it only shows the propertys of the array element right behind the stuff below the array. Because it wont take the space needed to show the full content of the array element.

Here is how the expanded array overlap with the script below:

Here is how the expanded array overlap with the script below

If i remove the enums as index the array expand correctly. But i want the index to be enums and i cant find what exactly in this drawer class causes this issue. I had the same problem some weeks ago with a older version of unity and a array containing other arrays. Updating the version fixed it but I´m on the newest version and i don't think it would help. There must be an issue in my drawer that causes this problem.

The code for the Drawer:

using UnityEngine;
public class EnumNamedArrayAttribute : PropertyAttribute
{
    public string[] names;
    public EnumNamedArrayAttribute(System.Type names_enum_type)
    {
        this.names = System.Enum.GetNames(names_enum_type);
    }
}

and

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
[CustomPropertyDrawer(typeof(EnumNamedArrayAttribute))]
public class DrawerEnumNamedArray : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EnumNamedArrayAttribute enumNames = attribute as EnumNamedArrayAttribute;
        //propertyPath returns something like component_hp_max.Array.data[4]
        //so get the index from there
        int index = System.Convert.ToInt32(property.propertyPath.Substring(property.propertyPath.IndexOf("[")).Replace("[", "").Replace("]", ""));
        //change the label
        label.text = enumNames.names[index];
        //draw field
        EditorGUI.PropertyField(position, property, label, true);
    }
}

the array is declared as

[EnumNamedArray(typeof(Spells))]
public SpellBlueprints[] spellBlueprints;

public enum Spells
{
    fireball,lightningbolt,frostbolt
}

and the class of the array:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class SpellBlueprint
{
    public float speed = 20f;
    public float damage = 15f;

    public bool AOE_enabled = false;

    public GameObject SpellPrefab;
}

edit: I should say that the enumerator array drawer works perfectly fine for normal arrays with int, strings float etc. but somehow its wont work with classes or structs with multiple attributes

CodePudding user response:

When you are creating PropertyDrawer there are 2 approaches:

  1. Use EditorGUILayout so Unity can calculated property height automatically:

    EditorGUILayout.PropertyField(property, label, true);
    
  2. Implement GetPropertyHeight and calculate height on your own, in your case:

    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        return EditorGUI.GetPropertyHeight(property, label, true);
    }
    
  • Related