簡體   English   中英

具有 class 枚舉索引的 Unity Inspector 數組不展開

[英]Unity Inspector array with enum Index for class don't expand

我有一個帶有自定義屬性抽屜的數組,它以枚舉為索引。 但是當我嘗試擴展它時,它只在數組下方的東西后面顯示數組元素的屬性。 因為它不會占用顯示數組元素的全部內容所需的空間。

以下是擴展數組與以下腳本重疊的方式:

這是擴展數組與下面的腳本重疊的方式

如果我刪除枚舉作為索引數組正確擴展。 但我希望索引是枚舉,我無法在這個抽屜 class 中找到究竟是什么導致了這個問題。 幾周前我遇到了同樣的問題,舊版本的統一和包含其他 arrays 的數組。 更新版本修復了它,但我使用的是最新版本,我認為它不會有幫助。 我的抽屜里一定有一個問題導致了這個問題。

抽屜的代碼:

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

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);
    }
}

數組被聲明為

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

public enum Spells
{
    fireball,lightningbolt,frostbolt
}

和陣列的 class :

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;
}

編輯:我應該說枚舉器數組抽屜對於具有 int、字符串浮點等的普通 arrays 工作得非常好,但不知何故它不適用於具有多個屬性的類或結構

當您創建 PropertyDrawer 時,有兩種方法:

  1. 使用 EditorGUILayout 以便 Unity 可以自動計算屬性高度:

     EditorGUILayout.PropertyField(property, label, true);
  2. 在您的情況下,實現 GetPropertyHeight 並自行計算高度:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM