动态ReorderableList元素的高度,但拖动时为SingleLineHeight

时间:2018-08-06 10:32:15

标签: c# unity3d

我已经找到了如何在RedditUnity Feedback中为Unity中的ReordableList动态使用不同的元素高度。

所以目前我的ReorderableList看起来像这样:

#if UNITY_EDITOR
using UnityEditor;
using UnityEditorInternal;
#endif
using UnityEngine;  
using System.Collections.Generic;
using System;
using System.Linq;

public class Example : MonoBehaviour
{
    [SerializeField]
    private List<string> _options = new List<string>();

    [CustomEditor(typeof(Example), true)]
    private class ExampleInspector: Editor
    {
        private SerializedProperty _options;
        private ReorderableList _optionsList;
        private List<float> _heights;

        privtae void Awake()
        {
            _options = serializedObject.FindProperty("_options");
            serializedObject.Update();
            _heights = new List<float>(_options.arraySize);

            _optionsList = new ReorderableList(serializedObject, _options, true, true, true, true)
            {
                drawHeaderCallback = delegate (Rect rect) { EditorGUI.LabelField(rect, "Options"); },

                drawElementCallback = delegate (Rect rect, int index, bool active, bool focused)
                {
                    var option = _optionsList.serializedProperty.GetArrayElementAtIndex(index);
                    var height = EditorGUIUtility.SingleLineHeight * 1.25f;

                    option.stringValue = EditorGUI.TextField(rect, "some Label", option.stringValue);

                    if(string.IsNullOrEmpty(options.stringValue))
                    {
                        height += EditorGUIUtility.SingleLineHeight;
                        EditorGUI.HelpBox(new Rect(rect.x, rect.y + EditorGUIUtility.SingleLineHeight, rect.width, EditorGUIUtility.SingleLineHeight), "Text is empty!", MessageType.Warning);
                    }
                },

                elementHeightCallback = (index) =>
                {
                    Repaint();
                    float height = 0;

                    try
                    {
                        height = _heights[index];
                    }
                    catch (ArgumentOutOfRangeException e)
                    {
                        Debug.LogWarning(e.Message);
                    }
                    finally
                    {
                        float[] floats = _heights.ToArray();
                        Array.Resize(ref floats, _options.arraySize);
                        _heights = floats.ToList();
                    }

                    return height;
                }
            }
        }
    }
}

到目前为止,效果很好。
但是,正如链接中已经提到的那样,关于drag&drp功能存在一些问题/错误:由于元素在draggin期间可能具有不同的高度,因此有时无法将其拖动到列表的顶部/底部。

所以我的问题是:
(如何)在拖动元素周围时可以强制将elementSize始终设置为SingleLineHeight?

0 个答案:

没有答案