Ejemplo n.º 1
0
        /// <summary>
        /// Displays the popup.
        /// </summary>
        /// <param name="position">Position.</param>
        /// <param name="property">Property.</param>
        /// <param name="label">Label.</param>
        protected virtual void DisplayPopup(Rect position, SerializedProperty property, GUIContent label)
        {
            FieldInfo field;
            object    provider = property.GetProvider(out field);
            string    key      = property.propertyPath;

            if (!m_PopupData.ContainsKey(key))
            {
                m_PopupData[key] = new PopupData();
            }
            PopupData popupData = m_PopupData[key];

            // initialize the contents getter method
            if (popupData.GetContentsCallback == null)
            {
                MethodInfo method =
                    provider.GetType().GetInstanceMethod(this.Attribute.PopupContentsGetter, s_ContentsGetterParams);
                if (method != null)
                {
                    popupData.GetContentsCallback = System.Delegate.CreateDelegate(
                        typeof(PopupAttribute.GetPopupContentsCallback), provider, method
                        ) as PopupAttribute.GetPopupContentsCallback;
                }
            }
            // if method cannot be found, display error icon
            if (popupData.GetContentsCallback == null)
            {
                EditorGUIX.DisplayPropertyFieldWithStatus(
                    position,
                    property,
                    ValidationStatus.Error,
                    label,
                    true,
                    string.Format(
                        "Unabled to find method: int {0}.{1} (List<GUIContent> labels, List<object> values)",
                        provider.GetType(), this.Attribute.PopupContentsGetter
                        )
                    );
                return;
            }
            else if (
                property.propertyType == SerializedPropertyType.Generic ||
                property.propertyType == SerializedPropertyType.Gradient
                )
            {
                EditorGUIX.DisplayPropertyFieldWithStatus(
                    position,
                    property,
                    ValidationStatus.Error,
                    label,
                    true,
                    string.Format(
                        "SerializedPropertyType.{0} not supported for popup drawer.", property.propertyType
                        )
                    );
                return;
            }
            EditorGUI.BeginProperty(position, label, property);
            {
                int index = popupData.GetContentsCallback(popupData.Labels, popupData.Values);
                EditorGUI.BeginChangeCheck();
                {
                    index = EditorGUI.Popup(position, label, index, popupData.Labels.ToArray());
                }
                if (EditorGUI.EndChangeCheck() && index > -1 && index < popupData.Values.Count)
                {
                    property.SetValue(popupData.Values[index]);
                    popupData.GetContentsCallback = null;
                }
            }
            EditorGUI.EndProperty();
        }