我正在使用这两个库:


Unity-SerializableDictionary:
https://github.com/starikcetin/Unity-SerializableDictionary
Unity场景参考:
https://github.com/starikcetin/unity-scene-reference


基本上,可序列化的字典查看propertyType以确定属性是否可以扩展,并进行以下检查:

static bool CanPropertyBeExpanded(SerializedProperty property)
{
    switch(property.propertyType)
    {
    case SerializedPropertyType.Generic:
    case SerializedPropertyType.Vector4:
    case SerializedPropertyType.Quaternion:
        return true;
    default:
        return false;
    }
}


但是,似乎场景参考已注册为可扩展属性,即使未注册。这是因为-apparently- Unity将其注册为Generic类型。

我可以通过将SerializedProperty.propertyType设置为更有意义的类型来解决此问题,但这是只读的。

那么,如何设置自定义属性抽屉的SerializedProperty.propertyType

最佳答案

它没有记录,但是类型Generic被自动分配给任何自定义类(例如SceneReference)。您不能更改propertyType,因为它是只读的...并且您不能告诉编译器将自定义类作为其他类来处理...

即使您可以...什么是“更有意义的类型”? SerializedPropertyType的可用类型是有限的,并且它们对于自定义类都没有更有意义的意义。



这里的主要“问题”是:

SerializableDictionary抽屉只是假定通常,如果您有一个没有自定义Generic的自定义(PropertyDrawer)类-因此使用默认抽屉-它的行为与QuaternionVector4的默认抽屉完全相同:

c# - 如何在CustomPropertyDrawer中设置SerializedProperty.propertyType-LMLPHP


第一行有标签和折页
字段/内容绘制在下方,且仅在属性折叠后


由于SceneReference的抽屉未实现此行为,因此将其绘制在字典的键字段顶部。



因此,作为最简单的修复程序,您只需删除

case SerializedPropertyType.Generic:


因此SceneAsset(以及所有其他自定义类)被视为普通的展开字段-出于品味

c# - 如何在CustomPropertyDrawer中设置SerializedProperty.propertyType-LMLPHP



另外,您可以做的是更改PropertyDrawerSceneReference以反映例如Quaternion


添加带有标签的EditorGUI.Foldout来更改property.isExpaned
将任何内容移到下方一行(并且可以选择移动)
在属性高度和if(!property.isExpanded)条件添加一行


可能看起来例如喜欢:

// Made these two const btw
private const float PAD_SIZE = 2f;
private const float FOOTER_HEIGHT = 10f;

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
    // Move this up
    EditorGUI.BeginProperty(position, GUIContent.none, property);
    {
        // Here we add the foldout using a single line height, the label and change
        // the value of property.isExpanded
        property.isExpanded = EditorGUI.Foldout(new Rect(position.x, position.y, position.width, lineHeight), property.isExpanded, label);

        // Now you want to draw the content only if you unfold this property
        if (property.isExpanded)
        {
            // Optional: Indent the content
            //EditorGUI.indentLevel++;
            //{

            // reduce the height by one line and move the content one line below
            position.height -= lineHeight;
            position.y += lineHeight;

            var sceneAssetProperty = GetSceneAssetProperty(property);

            // Draw the Box Background
            position.height -= FOOTER_HEIGHT;
            GUI.Box(EditorGUI.IndentedRect(position), GUIContent.none, EditorStyles.helpBox);
            position = boxPadding.Remove(position);
            position.height = lineHeight;

            // Draw the main Object field
            label.tooltip = "The actual Scene Asset reference.\nOn serialize this is also stored as the asset's path.";


            var sceneControlID = GUIUtility.GetControlID(FocusType.Passive);
            EditorGUI.BeginChangeCheck();
            {
                // removed the label here since we already have it in the foldout before
                sceneAssetProperty.objectReferenceValue = EditorGUI.ObjectField(position, sceneAssetProperty.objectReferenceValue, typeof(SceneAsset), false);
            }
            var buildScene = BuildUtils.GetBuildScene(sceneAssetProperty.objectReferenceValue);
            if (EditorGUI.EndChangeCheck())
            {
                // If no valid scene asset was selected, reset the stored path accordingly
                if (buildScene.scene == null) GetScenePathProperty(property).stringValue = string.Empty;
            }

            position.y += paddedLine;

            if (!buildScene.assetGUID.Empty())
            {
                // Draw the Build Settings Info of the selected Scene
                DrawSceneInfoGUI(position, buildScene, sceneControlID + 1);
            }

            // Optional: If enabled before reset the indentlevel
            //}
            //EditorGUI.indentLevel--;
        }
    }
    EditorGUI.EndProperty();
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
    var sceneAssetProperty = GetSceneAssetProperty(property);
    // Add an additional line and check if property.isExpanded
    var lines = property.isExpanded ? sceneAssetProperty.objectReferenceValue != null ? 3 : 2 : 1;
    // If this oneliner is confusing you - it does the same as
    //var line = 3; // Fully expanded and with info
    //if(sceneAssetProperty.objectReferenceValue == null) line = 2;
    //if(!property.isExpanded) line = 1;

    return boxPadding.vertical + lineHeight * lines + PAD_SIZE * (lines - 1) + FOOTER_HEIGHT;
}




现在看起来像

[Serializable]
public class TestDict : SerializableDictionary<string, SceneReference> { }

public class Example : MonoBehaviour
{
    public SceneReference NormalReference;

    public TestDict DictExample = new TestDict();
}


c# - 如何在CustomPropertyDrawer中设置SerializedProperty.propertyType-LMLPHP

10-04 18:49