/// <summary>
        /// Draws property using provided <see cref="Rect"/>.
        /// </summary>
        /// <param name="position"></param>
        /// <param name="property"></param>
        /// <param name="label"></param>
        protected override void OnGUISafe(Rect position, SerializedProperty property, GUIContent label)
        {
            var refAttribute = Attribute;
            var refProperty  = property.FindPropertyRelative("classReference");

            //validate serialized data
            if (refAttribute == null || refAttribute.AssemblyType == null)
            {
                EditorGUI.PropertyField(position, refProperty, label);
                return;
            }

            var refType   = !string.IsNullOrEmpty(refProperty.stringValue) ? Type.GetType(refProperty.stringValue) : null;
            var refTypes  = new List <Type>();
            var refLabels = new List <string>()
            {
                "<None>"
            };
            var index = -1;

            //get stored types if possible or create new item
            if (!filteredTypes.TryGetValue(refAttribute.AssemblyType, out refTypes))
            {
                refTypes = filteredTypes[refAttribute.AssemblyType] = refAttribute.GetFilteredTypes();
            }
            else
            {
                refTypes = filteredTypes[refAttribute.AssemblyType];
            }

            //create labels from filtered types
            for (int i = 0; i < refTypes.Count; i++)
            {
                var menuType  = refTypes[i];
                var menuLabel = FormatGroupedTypeName(menuType, refAttribute.Grouping);

                if (menuType == refType)
                {
                    index = i;
                }

                refLabels.Add(menuLabel);
            }

            //draw reference property
            EditorGUI.BeginProperty(position, label, refProperty);
            label = property.name != "data" ? label : GUIContent.none;
            index = EditorGUI.Popup(position, label.text, index + 1, refLabels.ToArray());
            //get correct class reference, index = 0 is reserved to <None> type
            refProperty.stringValue = index >= 1 ? SerializedTypeReference.GetClassReference(refTypes[index - 1]) : "";
            EditorGUI.EndProperty();
        }
示例#2
0
        /// <summary>
        /// Draws property using provided <see cref="Rect"/>.
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="property"></param>
        /// <param name="label"></param>
        public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
        {
            var refAttribute = Attribute;
            var refProperty  = property.FindPropertyRelative("classReference");

            if (refProperty == null)
            {
                Debug.LogWarning(property.name + " property in " + property.serializedObject.targetObject +
                                 " - " + attribute.GetType() + " can be used only on string properties.");
                EditorGUI.PropertyField(rect, property, label);
                return;
            }

            if (refAttribute == null || refAttribute.AssemblyType == null)
            {
                EditorGUI.PropertyField(rect, refProperty, label);
                return;
            }

            //DrawTypeSelectionRect(position, refProperty, refAttribute, label);

            var refType   = ResolveType(refProperty.stringValue);
            var refTypes  = new List <Type>();
            var refLabels = new List <string>()
            {
                "<None>"
            };
            var index = -1;

            //get stored types if possible or create new item
            if (!filteredTypes.TryGetValue(refAttribute.AssemblyType, out refTypes))
            {
                refTypes = filteredTypes[refAttribute.AssemblyType] = refAttribute.GetFilteredTypes();
            }
            else
            {
                refTypes = filteredTypes[refAttribute.AssemblyType];
            }

            //create labels from filtered types
            for (int i = 0; i < refTypes.Count; i++)
            {
                var menuType  = refTypes[i];
                var menuLabel = FormatGroupedTypeName(menuType, refAttribute.Grouping);

                if (menuType == refType)
                {
                    index = i;
                }

                refLabels.Add(menuLabel);
            }

            //draw reference property
            EditorGUI.BeginProperty(rect, label, refProperty);
            label = property.name != "data" ? label : GUIContent.none;
            index = EditorGUI.Popup(rect, label.text, index + 1, refLabels.ToArray());
            //get correct class reference, index = 0 is reserved to <None> type
            refProperty.stringValue = index >= 1 ? SerializedTypeReference.GetClassReference(refTypes[index - 1]) : "";
            EditorGUI.EndProperty();
        }
示例#3
0
 private void OnTypeSelected(object userData)
 {
     selectedClassRef = SerializedTypeReference.GetClassReference(userData as Type);
     EditorWindow.focusedWindow.SendEvent(EditorGUIUtility.CommandEvent("TypeReferenceUpdated"));
 }