Ящик пользовательских свойств Unity, запрещающий присвоение значений - PullRequest
0 голосов
/ 21 апреля 2020

Я столкнулся с проблемой (на самом деле две) при создании ящика настраиваемого свойства для объекта ScriptableObject в единстве.

1) Я не могу объяснить, почему только некоторые поля свойств не позволяют присваивать значения, когда У меня есть несколько других похожих ящиков и полей, которые работают просто отлично.

2) Почему только некоторые из моих (gameobject & Transform) serializedProperties не просто работают с использованием стандартного редактора GUI .PropertyField () Метод.

public class ExportedData : ScriptableObject {
    public string intro = "";
    public AudioClip introClip;

    public Transform startTransform;

    //(Remainder of class commented out to aid in debugging)
}

[CustomPropertyDrawer(typeof(ExportedData))]
public class ExportedDataDrawer : PropertyDrawer {
    private bool isStyleInitialized = false;
    private static GUIStyle sectionTitleStyle;

    private void InitializeStyles() {
            //Workaround for default EditorStyles not initializing before custom editor scripts... (Unity Bug?)
            if (EditorStyles.label == null) {
                return;
            }

            sectionTitleStyle = new GUIStyle(EditorStyles.label);
            sectionTitleStyle.alignment = TextAnchor.UpperCenter;
            sectionTitleStyle.fontStyle = FontStyle.Bold;
            sectionTitleStyle.richText = true;

            isStyleInitialized = true;
        }

    public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label) {
        if (!isStyleInitialized) {
            InitializeStyles();
            return;
        }

        EditorGUI.BeginProperty(rect, label, property);
        rect.height = EditorGUIUtility.singleLineHeight;

        EditorGUI.LabelField(rect, "<b>Data Asset</b>", sectionTitleStyle);

        rect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
        EditorGUI.PropertyField(rect, property, GUIContent.none); //Receives assignment of .asset file via drag and drop just fine.

        if (property.objectReferenceValue != null) {
            SerializedObject obj = new SerializedObject(property.objectReferenceValue);

            rect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing + vertPadding;
            ShowDataFields(rect, obj);
        }

        EditorGUI.EndProperty();
    }

    private void ShowDataFields(Rect rect, SerializedObject obj) {

        DrawContentInputFields(ref rect, obj.FindProperty("intro"), obj.FindProperty("introClip"));

        obj.ApplyModifiedProperties();//data field above won't store values unless called

        rect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing + vertPadding;
        EditorGUI.LabelField(rect, "<b>Supplemental Data</b>", sectionTitleStyle);

        rect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing + vertPadding;

        /* Fails despite other fields not failing. */

        //Refuses to accept value assignment via editor
        //EditorGUI.PropertyField(rect, obj.FindProperty("startTransform"), true);

        //Fails to store value, field is instantly set to null. (With and without ApplyModifiedProperties call)
        //SerializedProperty startTfm = obj.FindProperty("startTransform");
        //startTfm.objectReferenceValue = EditorGUI.ObjectField(rect, startTfm.objectReferenceValue, typeof(Transform), true)

        //Fails to store value, field is instantly set to null.
        //SerializedProperty startTfm = obj.FindProperty("startTransform");
        //Transform tfm = EditorGUI.ObjectField(rect, (Transform)startTfm.objectReferenceValue, typeof(Transform), true) as Transform;
        //if (tfm != null && tfm != (Transform)startTfm.objectReferenceValue) {
        //  startTfm.objectReferenceValue = tfm;
        //  startTfm.serializedObject.ApplyModifiedProperties();
        //}


        /* Works, but shouldn't need to access the underlying object like this */
        SerializedProperty startTfm = obj.FindProperty("startTransform");
        Transform tfm = EditorGUI.ObjectField(rect, (Transform)startTfm.objectReferenceValue, typeof(Transform), true) as Transform;
        if (tfm != null && tfm != (Transform)startTfm.objectReferenceValue) {
            LessonData owningObject = (LessonData)obj.targetObject;
            owningObject.startTransform = tfm;
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...