private void UpdateConstraint(TypeConstraintAttribute attribute)
 {
     sharedConstraint.ApplyTarget(attribute.AssemblyType);
     if (sharedConstraint is TypeConstraintStandard constraint)
     {
         constraint.AllowAbstract = attribute.AllowAbstract;
         constraint.AllowObsolete = attribute.AllowObsolete;
         constraint.Settings      = attribute.TypeSettings;
     }
 }
Ejemplo n.º 2
0
 private static void FilterTypes(Assembly assembly, TypeConstraintAttribute filter,
                                 ICollection <Type> excludedTypes, List <Type> output)
 {
     output.AddRange(
         from type in assembly.GetTypes()
         where type.IsVisible
         where filter == null || filter.IsConstraintSatisfied(type)
         where excludedTypes == null || !excludedTypes.Contains(type)
         select type
         );
 }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            TypeConstraintAttribute target = attribute as TypeConstraintAttribute;

            CheckForCorrectInputetObject(property, target);
            if (position.Contains(Event.current.mousePosition))
            {
                CheckForCorrectDraggableObject(target);
            }

            property.objectReferenceValue = EditorGUI.ObjectField(position, label, property.objectReferenceValue, typeof(MonoBehaviour), true);
        }
        private void CheckForCorrectInputetObject(SerializedProperty property, TypeConstraintAttribute target)
        {
            if (property.objectReferenceValue != null)
            {
                if (!SystemUtils.IsSubclassOrHaveInterface(property.objectReferenceValue.GetType(), target.type))
                {
                    MonoBehaviour monoBehaviour = property.objectReferenceValue as MonoBehaviour;

                    if (monoBehaviour != null)
                    {
                        property.objectReferenceValue = SystemUtils.GetComponentWhichSubclassOrHaveInterface(monoBehaviour.gameObject, target.type);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        private List <Type> GetFilteredTypes(TypeConstraintAttribute filter)
        {
            var types = new List <Type>();

            var excludedTypes = ExcludedTypeCollectionGetter != null?ExcludedTypeCollectionGetter() : null;

            foreach (var referencedAssembly in AppDomain.CurrentDomain.GetAssemblies()
                     .Where(assembly => !assembly.FullName.Contains("Tests")))
            {
                FilterTypes(referencedAssembly, filter, excludedTypes, types);
            }

            types.Sort((a, b) => a.FullName.CompareTo(b.FullName));

            return(types);
        }
Ejemplo n.º 6
0
        private void DrawTypeSelectionControl(Rect position, SerializedProperty property, GUIContent label,
                                              TypeConstraintAttribute filter)
        {
            try
            {
                var restoreShowMixedValue = EditorGUI.showMixedValue;
                EditorGUI.showMixedValue = property.hasMultipleDifferentValues;

                property.stringValue = DrawTypeSelectionControl(position, label, property.stringValue, filter);

                EditorGUI.showMixedValue = restoreShowMixedValue;
            }
            finally
            {
                ExcludedTypeCollectionGetter = null;
            }
        }
        private void CheckForCorrectDraggableObject(TypeConstraintAttribute target)
        {
            if (DragAndDrop.objectReferences.Length > 0)
            {
                UnityEngine.Object draggedObject = DragAndDrop.objectReferences[0];

                if (draggedObject == null)
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
                }
                else if (!SystemUtils.IsSubclassOrHaveInterface(draggedObject.GetType(), target.type))
                {
                    if (SystemUtils.GetComponentWhichSubclassOrHaveInterface(draggedObject as GameObject, target.type) == null)
                    {
                        DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
                    }
                }
            }
        }
Ejemplo n.º 8
0
        private string DrawTypeSelectionControl(Rect position, GUIContent label, string classRef,
                                                TypeConstraintAttribute filter)
        {
            if (label != null && label != GUIContent.none)
            {
                position = EditorGUI.PrefixLabel(position, label);
            }

            var controlID = GUIUtility.GetControlID(s_ControlHint, FocusType.Keyboard, position);

            var triggerDropDown = false;

            switch (Event.current.GetTypeForControl(controlID))
            {
            case EventType.ExecuteCommand:
                if (Event.current.commandName == "TypeReferenceUpdated")
                {
                    if (s_SelectionControlID == controlID)
                    {
                        if (classRef != s_SelectedClassRef)
                        {
                            classRef    = s_SelectedClassRef;
                            GUI.changed = true;
                        }

                        s_SelectionControlID = 0;
                        s_SelectedClassRef   = null;
                    }
                }

                break;

            case EventType.MouseDown:
                if (GUI.enabled && position.Contains(Event.current.mousePosition))
                {
                    GUIUtility.keyboardControl = controlID;
                    triggerDropDown            = true;
                    Event.current.Use();
                }

                break;

            case EventType.KeyDown:
                if (GUI.enabled && GUIUtility.keyboardControl == controlID)
                {
                    if (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.Space)
                    {
                        triggerDropDown = true;
                        Event.current.Use();
                    }
                }

                break;

            case EventType.Repaint:
                // Remove assembly name from content of popup control.
                var classRefParts = classRef.Split(',');

                s_TempContent.text = classRefParts[0].Trim();
                if (s_TempContent.text == string.Empty)
                {
                    s_TempContent.text = "(None)";
                }
                else if (ResolveType(classRef) == null)
                {
                    s_TempContent.text += " {Missing}";
                }

                EditorStyles.popup.Draw(position, s_TempContent, controlID);
                break;
            }

            if (triggerDropDown)
            {
                s_SelectionControlID = controlID;
                s_SelectedClassRef   = classRef;

                var filteredTypes = GetFilteredTypes(filter);
                DisplayDropDown(position, filteredTypes, ResolveType(classRef), filter.Grouping);
            }

            return(classRef);
        }
Ejemplo n.º 9
0
 private static bool DoesMatchInstantiableRestriction(Type type, TypeConstraintAttribute constraint)
 {
     return(!constraint.ForceInstantiableType ||
            (!type.IsAbstract && !type.IsInterface && !type.IsGenericTypeDefinition));
 }
Ejemplo n.º 10
0
 private static bool IsMatchingType(Type type, TypeConstraintAttribute constraint)
 {
     return(constraint == null ||
            (DoesMatchInstantiableRestriction(type, constraint) &&
             IsAssignableFrom(constraint.BaseType, type)));
 }
 private void UpdateAppearance(TypeConstraintAttribute attribute)
 {
     sharedAppearance.TypeGrouping = attribute.TypeGrouping;
 }
 /// <summary>
 /// Creates default constraint attribute if the given one is invalid.
 /// </summary>
 private TypeConstraintAttribute GetVerifiedAttribute(TypeConstraintAttribute attribute)
 {
     return(IsDefaultField(attribute) ? GetDefaultConstraint() : attribute);
 }
 private bool IsDefaultField(TypeConstraintAttribute attribute)
 {
     return(attribute == null || attribute.AssemblyType == null);
 }