示例#1
0
 /// <summary>
 /// Raises the GUI event.
 /// </summary>
 /// <param name="position">Position.</param>
 /// <param name="property">Property.</param>
 /// <param name="label">Label.</param>
 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
 {
     // initialize the getter method
     if (m_PopupContentsGetter == null)
     {
         m_PopupContentsGetter = fieldInfo.ReflectedType.GetMethod(
             Attribute.PopupContentsGetter,
             ReflectionX.instanceBindingFlags,
             null,
             new System.Type[] { typeof(List <GUIContent>), typeof(List <object>) },
             null
             );
     }
     // if method cannot be found, display error icon
     if (m_PopupContentsGetter == null)
     {
         EditorGUIX.DisplayPropertyFieldWithStatus(
             position,
             property,
             ValidationStatus.Error,
             label,
             true,
             string.Format(
                 "Unabled to find method: int {0}.{1} (List<GUIContent> labels, List<object> values)",
                 fieldInfo.ReflectedType.FullName, Attribute.PopupContentsGetter
                 )
             );
     }
     else if (
         property.propertyType == SerializedPropertyType.Generic ||
         property.propertyType == SerializedPropertyType.Gradient
         )
     {
         EditorGUIX.DisplayPropertyFieldWithStatus(
             position,
             property,
             ValidationStatus.Error,
             label,
             true,
             string.Format(
                 "SerializedPropertyType.{0} not currently supported for popup drawer.", property.propertyType
                 )
             );
     }
     else
     {
         EditorGUI.BeginProperty(position, label, property);
         {
             s_PopupContentsArgs[0] = m_PopupLabels;
             s_PopupContentsArgs[1] = m_PopupValues;
             int index =
                 (int)m_PopupContentsGetter.Invoke(property.serializedObject.targetObject, s_PopupContentsArgs);
             index = EditorGUI.Popup(position, label, index, m_PopupLabels.ToArray());
             property.SetValue(m_PopupValues[index]);
         }
         EditorGUI.EndProperty();
     }
 }
        /// <summary>
        /// Raises the GUI event.
        /// </summary>
        /// <param name="position">Position.</param>
        /// <param name="property">Property.</param>
        /// <param name="label">Label.</param>
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            ValidationStatus status  = ValidationStatus.None;
            bool             canDraw = true;
            object           value   = property.GetValue();

            if (value is IValidatable)
            {
                status = GetStatus(property.serializedObject.targetObject, value as IValidatable);
            }
            else
            {
                switch (property.propertyType)
                {
                case SerializedPropertyType.Generic:
                    canDraw = false;
                    break;

                case SerializedPropertyType.AnimationCurve:
                    canDraw = Attribute.ValidationMethod != null;
                    if (canDraw)
                    {
                        status = GetStatus(property.serializedObject.targetObject, property.animationCurveValue);
                    }
                    break;

                case SerializedPropertyType.Gradient:
                    canDraw = false;
                    break;

                case SerializedPropertyType.ArraySize:
                case SerializedPropertyType.Character:
                case SerializedPropertyType.Enum:
                case SerializedPropertyType.Float:
                case SerializedPropertyType.Integer:
                case SerializedPropertyType.String:
                    status = GetStatus(property.serializedObject.targetObject, (System.IComparable)property.GetValue());
                    break;

                case SerializedPropertyType.Boolean:
                case SerializedPropertyType.Bounds:
                case SerializedPropertyType.Color:
                case SerializedPropertyType.LayerMask:
                case SerializedPropertyType.ObjectReference:
                case SerializedPropertyType.Quaternion:
                case SerializedPropertyType.Rect:
                case SerializedPropertyType.Vector2:
                case SerializedPropertyType.Vector3:
                case SerializedPropertyType.Vector4:
                    status = GetStatus(property.serializedObject.targetObject, property.GetValue());
                    break;
                }
            }
            if (!canDraw)
            {
                Color oldColor = GUI.color;
                GUI.color = Color.red;
                EditorGUI.LabelField(
                    position,
                    label,
                    new GUIContent(
                        string.Format(
                            "Unable to perform comparison of SerializedPropertyType.{0} ({1}.{2}).",
                            property.propertyType, property.serializedObject.targetObject.name, property.propertyPath
                            )
                        )
                    );
                GUI.color = oldColor;
            }
            else
            {
                EditorGUIX.DisplayPropertyFieldWithStatus(
                    position, property, status, label, true, m_CurrentStatusTooltip
                    );
            }
        }
示例#3
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();
        }