public void HandleDrawnType(Type drawnType, Type propertyType, FieldInfo field, PropertyAttribute attribute)
        {
            Type drawerTypeForType = ScriptAttributeUtility.GetDrawerTypeForType(drawnType);

            if (drawerTypeForType != null)
            {
                if (typeof(PropertyDrawer).IsAssignableFrom(drawerTypeForType))
                {
                    if (propertyType != null && propertyType.IsArrayOrList())
                    {
                        return;
                    }
                    this.m_PropertyDrawer             = (PropertyDrawer)Activator.CreateInstance(drawerTypeForType);
                    this.m_PropertyDrawer.m_FieldInfo = field;
                    this.m_PropertyDrawer.m_Attribute = attribute;
                }
                else if (typeof(DecoratorDrawer).IsAssignableFrom(drawerTypeForType))
                {
                    if (field != null && field.FieldType.IsArrayOrList() && !propertyType.IsArrayOrList())
                    {
                        return;
                    }
                    DecoratorDrawer decoratorDrawer = (DecoratorDrawer)Activator.CreateInstance(drawerTypeForType);
                    decoratorDrawer.m_Attribute = attribute;
                    if (this.m_DecoratorDrawers == null)
                    {
                        this.m_DecoratorDrawers = new List <DecoratorDrawer>();
                    }
                    this.m_DecoratorDrawers.Add(decoratorDrawer);
                }
            }
        }
Example #2
0
        public bool CanCacheInspectorGUI(SerializedProperty property)
        {
            bool result;

            if (this.m_DecoratorDrawers != null && !this.isCurrentlyNested)
            {
                if (this.m_DecoratorDrawers.Any((DecoratorDrawer decorator) => !decorator.CanCacheInspectorGUI()))
                {
                    result = false;
                    return(result);
                }
            }
            if (this.propertyDrawer != null)
            {
                result = this.propertyDrawer.CanCacheInspectorGUISafe(property.Copy());
            }
            else
            {
                property = property.Copy();
                SerializedProperty endProperty = property.GetEndProperty();
                bool enterChildren             = property.isExpanded && EditorGUI.HasVisibleChildFields(property);
                while (property.NextVisible(enterChildren) && !SerializedProperty.EqualContents(property, endProperty))
                {
                    if (!ScriptAttributeUtility.GetHandler(property).CanCacheInspectorGUI(property))
                    {
                        result = false;
                        return(result);
                    }
                    enterChildren = false;
                }
                result = true;
            }
            return(result);
        }
        internal void Show(Type requiredType, SerializedProperty property, bool allowSceneObjects, List <int> allowedInstanceIDs = null, Action <UnityObject> onObjectSelectorClosed = null, Action <UnityObject> onObjectSelectedUpdated = null)
        {
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }

            if (requiredType == null)
            {
                ScriptAttributeUtility.GetFieldInfoFromProperty(property, out requiredType);
                // case 951876: built-in types do not actually have reflectable fields, so their object types must be extracted from the type string
                // this works because built-in types will only ever have serialized references to other built-in types, which this window's filter expects as unqualified names
                if (requiredType == null)
                {
                    m_RequiredTypes = new string[] { s_MatchPPtrTypeName.Match(property.type).Groups[1].Value }
                }
                ;
            }

            // Don't select anything on multi selection
            UnityObject obj = property.hasMultipleDifferentValues ? null : property.objectReferenceValue;

            UnityObject objectBeingEdited = property.serializedObject.targetObject;

            m_EditedProperty = property;

            Show(obj, new Type[] { requiredType }, objectBeingEdited, allowSceneObjects, allowedInstanceIDs, onObjectSelectorClosed, onObjectSelectedUpdated);
        }
Example #4
0
        public bool CanCacheInspectorGUI(SerializedProperty property)
        {
            if (m_DecoratorDrawers != null &&
                !isCurrentlyNested &&
                m_DecoratorDrawers.Any(decorator => !decorator.CanCacheInspectorGUI()))
            {
                return(false);
            }

            if (propertyDrawer != null)
            {
                return(propertyDrawer.CanCacheInspectorGUISafe(property.Copy()));
            }

            property = property.Copy();
            SerializedProperty endProperty = property.GetEndProperty();
            bool childrenAreExpanded       = property.isExpanded && EditorGUI.HasVisibleChildFields(property);

            // Loop through all child properties
            while (property.NextVisible(childrenAreExpanded) && !SerializedProperty.EqualContents(property, endProperty))
            {
                if (!ScriptAttributeUtility.GetHandler(property).CanCacheInspectorGUI(property))
                {
                    return(false);
                }
                childrenAreExpanded = false;
            }

            return(true);
        }
        public float GetHeight(SerializedProperty property, GUIContent label, bool includeChildren)
        {
            float num = 0f;

            if (this.m_DecoratorDrawers != null && !this.isCurrentlyNested)
            {
                foreach (DecoratorDrawer current in this.m_DecoratorDrawers)
                {
                    num += current.GetHeight();
                }
            }
            if (this.propertyDrawer != null)
            {
                num += this.propertyDrawer.GetPropertyHeightSafe(property.Copy(), label ?? EditorGUIUtility.TempContent(property.displayName));
            }
            else if (!includeChildren)
            {
                num += EditorGUI.GetSinglePropertyHeight(property, label);
            }
            else
            {
                property = property.Copy();
                SerializedProperty endProperty = property.GetEndProperty();
                num += EditorGUI.GetSinglePropertyHeight(property, label);
                bool enterChildren = property.isExpanded && EditorGUI.HasVisibleChildFields(property);
                while (property.NextVisible(enterChildren) && !SerializedProperty.EqualContents(property, endProperty))
                {
                    num          += ScriptAttributeUtility.GetHandler(property).GetHeight(property, EditorGUIUtility.TempContent(property.displayName), true);
                    enterChildren = false;
                    num          += 2f;
                }
            }
            return(num);
        }
Example #6
0
        internal override bool OnOptimizedInspectorGUI(Rect contentRect)
        {
            m_SerializedObject.UpdateIfRequiredOrScript();

            bool childrenAreExpanded = true;
            bool wasEnabled          = GUI.enabled;
            var  visibleRect         = GUIClip.visibleRect;
            var  contentOffset       = contentRect.y;

            if (Event.current.type != EventType.Repaint)
            {
                visibleRect = m_LastVisibleRect;
            }

            // Release keyboard focus before scrolling so that the virtual scrolling focus wrong control.
            if (Event.current.type == EventType.ScrollWheel)
            {
                GUIUtility.keyboardControl = 0;
            }

            var behaviour             = target as MonoBehaviour;
            var property              = m_SerializedObject.GetIterator();
            var isInspectorModeNormal = inspectorMode == InspectorMode.Normal;

            using (new UnityEditor.Localization.Editor.LocalizationGroup(behaviour))
            {
                while (property.NextVisible(childrenAreExpanded))
                {
                    var handler           = ScriptAttributeUtility.GetHandler(property);
                    var hasPropertyDrawer = handler.propertyDrawer != null;
                    childrenAreExpanded = !hasPropertyDrawer && property.isExpanded && EditorGUI.HasVisibleChildFields(property);
                    contentRect.height  = handler.GetHeight(property, null, hasPropertyDrawer);

                    if (contentRect.Overlaps(visibleRect))
                    {
                        EditorGUI.indentLevel = property.depth;
                        using (new EditorGUI.DisabledScope(isInspectorModeNormal && "m_Script" == property.propertyPath))
                            childrenAreExpanded &= handler.OnGUI(contentRect, property, null, false, visibleRect);
                    }

                    contentRect.y += contentRect.height + EditorGUI.kControlVerticalSpacing;
                }
            }

            // Fix new height
            if (Event.current.type == EventType.Repaint)
            {
                m_LastVisibleRect = visibleRect;
                var newHeight = contentRect.y - contentOffset;
                if (newHeight != m_LastHeight)
                {
                    m_LastHeight = contentRect.y - contentOffset;
                    Repaint();
                }
            }

            GUI.enabled = wasEnabled;
            return(m_SerializedObject.ApplyModifiedProperties());
        }
        internal static PropertyHandler GetHandler(SerializedProperty property)
        {
            if (property == null || property.serializedObject.inspectorMode != InspectorMode.Normal)
            {
                return(ScriptAttributeUtility.s_SharedNullHandler);
            }
            PropertyHandler handler1 = ScriptAttributeUtility.propertyHandlerCache.GetHandler(property);

            if (handler1 != null)
            {
                return(handler1);
            }
            System.Type type = (System.Type)null;
            List <PropertyAttribute> propertyAttributeList = (List <PropertyAttribute>)null;

            System.Reflection.FieldInfo field        = (System.Reflection.FieldInfo)null;
            UnityEngine.Object          targetObject = property.serializedObject.targetObject;
            if (targetObject is MonoBehaviour || targetObject is ScriptableObject)
            {
                field = ScriptAttributeUtility.GetFieldInfoFromProperty(property, out type);
                propertyAttributeList = ScriptAttributeUtility.GetFieldAttributes(field);
            }
            else
            {
                if (ScriptAttributeUtility.s_BuiltinAttributes == null)
                {
                    ScriptAttributeUtility.PopulateBuiltinAttributes();
                }
                if (propertyAttributeList == null)
                {
                    propertyAttributeList = ScriptAttributeUtility.GetBuiltinAttributes(property);
                }
            }
            PropertyHandler handler2 = ScriptAttributeUtility.s_NextHandler;

            if (propertyAttributeList != null)
            {
                for (int index = propertyAttributeList.Count - 1; index >= 0; --index)
                {
                    handler2.HandleAttribute(propertyAttributeList[index], field, type);
                }
            }
            if (!handler2.hasPropertyDrawer && type != null)
            {
                handler2.HandleDrawnType(type, type, field, (PropertyAttribute)null);
            }
            if (handler2.empty)
            {
                ScriptAttributeUtility.propertyHandlerCache.SetHandler(property, ScriptAttributeUtility.s_SharedNullHandler);
                handler2 = ScriptAttributeUtility.s_SharedNullHandler;
            }
            else
            {
                ScriptAttributeUtility.propertyHandlerCache.SetHandler(property, handler2);
                ScriptAttributeUtility.s_NextHandler = new PropertyHandler();
            }
            return(handler2);
        }
        internal override bool OnOptimizedInspectorGUI(Rect contentRect)
        {
            bool childrenAreExpanded = true;
            bool wasEnabled          = GUI.enabled;
            var  visibleRect         = GUIClip.visibleRect;
            var  contentOffset       = contentRect.y;

            if (Event.current.type != EventType.Repaint)
            {
                visibleRect = m_LastVisibleRect;
            }

            // Release keyboard focus before scrolling so that the virtual scrolling focus wrong control.
            if (Event.current.type == EventType.ScrollWheel)
            {
                GUIUtility.keyboardControl = 0;
            }

            contentRect.xMin += InspectorWindow.kInspectorPaddingLeft;
            contentRect.xMax -= InspectorWindow.kInspectorPaddingRight;

            var property = m_SerializedObject.GetIterator();
            var isInspectorModeNormal = inspectorMode == InspectorMode.Normal;

            while (property.NextVisible(childrenAreExpanded))
            {
                var handler = ScriptAttributeUtility.GetHandler(property);
                contentRect.height = handler.GetHeight(property, null, false);

                if (contentRect.Overlaps(visibleRect))
                {
                    EditorGUI.indentLevel = property.depth;
                    using (new EditorGUI.DisabledScope(isInspectorModeNormal && "m_Script" == property.propertyPath))
                        childrenAreExpanded = handler.OnGUI(contentRect, property, null, false, visibleRect) && property.isExpanded;
                }
                else
                {
                    childrenAreExpanded = property.isExpanded;
                }

                contentRect.y += contentRect.height + EditorGUI.kControlVerticalSpacing;
            }

            // Fix new height
            if (Event.current.type == EventType.Repaint)
            {
                m_LastVisibleRect = visibleRect;
                var newHeight = contentRect.y - contentOffset;
                if (newHeight != m_LastHeight)
                {
                    m_LastHeight = contentRect.y - contentOffset;
                    Repaint();
                }
            }

            GUI.enabled = wasEnabled;
            return(m_SerializedObject.ApplyModifiedProperties());
        }
 private static System.Reflection.FieldInfo GetFieldInfoFromProperty(SerializedProperty property, out System.Type type)
 {
     System.Type typeFromProperty = ScriptAttributeUtility.GetScriptTypeFromProperty(property);
     if (typeFromProperty != null)
     {
         return(ScriptAttributeUtility.GetFieldInfoFromPropertyPath(typeFromProperty, property.propertyPath, out type));
     }
     type = (System.Type)null;
     return((System.Reflection.FieldInfo)null);
 }
Example #10
0
        private static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, out Type type)
        {
            Type scriptTypeFromProperty = ScriptAttributeUtility.GetScriptTypeFromProperty(property);

            if (scriptTypeFromProperty == null)
            {
                type = null;
                return(null);
            }
            return(ScriptAttributeUtility.GetFieldInfoFromPropertyPath(scriptTypeFromProperty, property.propertyPath, out type));
        }
Example #11
0
        SelectorHandlerType GetSelectorHandlerFromProperty(SerializedProperty property)
        {
            var fieldInfo = ScriptAttributeUtility.GetFieldInfoFromProperty(property, out _);

            if (fieldInfo == null)
            {
                return(null);
            }

            return(GetSelectorHandlerFromFieldInfo(fieldInfo));
        }
Example #12
0
        public override VisualElement CreatePropertyGUI(SerializedProperty property)
        {
            ScriptAttributeUtility.GetFieldInfoFromProperty(property, out var fieldType);

            var objectField = EditorUIService.instance.CreateObjectField();
            var genericType = fieldType.GetGenericArguments()[0];

            EditorUIService.instance.SetObjectField(objectField, property.objectReferenceValue, genericType, property.displayName);
            objectField.bindingPath = property.propertyPath;

            return(objectField);
        }
Example #13
0
        public override VisualElement CreatePropertyGUI(SerializedProperty property)
        {
            ScriptAttributeUtility.GetFieldInfoFromProperty(property, out var fieldType);

            var objectField = new UnityEditor.UIElements.ObjectField(property.displayName);
            var genericType = fieldType.GetGenericArguments()[0];

            objectField.objectType  = genericType;
            objectField.value       = property.objectReferenceValue;
            objectField.bindingPath = property.propertyPath;

            return(objectField);
        }
 internal static System.Type GetDrawerTypeForType(System.Type type)
 {
     if (ScriptAttributeUtility.s_DrawerTypeForType == null)
     {
         ScriptAttributeUtility.BuildDrawerTypeForTypeDictionary();
     }
     ScriptAttributeUtility.DrawerKeySet drawerKeySet;
     ScriptAttributeUtility.s_DrawerTypeForType.TryGetValue(type, out drawerKeySet);
     if (drawerKeySet.drawer != null || !type.IsGenericType)
     {
         return(drawerKeySet.drawer);
     }
     ScriptAttributeUtility.s_DrawerTypeForType.TryGetValue(type.GetGenericTypeDefinition(), out drawerKeySet);
     return(drawerKeySet.drawer);
 }
Example #15
0
        public void HandleDrawnType(SerializedProperty property, Type drawnType, Type propertyType, FieldInfo field, PropertyAttribute attribute)
        {
            Type drawerType = ScriptAttributeUtility.GetDrawerTypeForPropertyAndType(property, drawnType);

            // If we found a drawer type, instantiate the drawer, cache it, and return it.
            if (drawerType != null)
            {
                if (typeof(PropertyDrawer).IsAssignableFrom(drawerType))
                {
                    // Use PropertyDrawer on array elements, not on array itself.
                    // If there's a PropertyAttribute on an array, we want to apply it to the individual array elements instead.
                    // This is the only convenient way we can let the user apply PropertyDrawer attributes to elements inside an array.
                    if (propertyType != null && propertyType.IsArrayOrList())
                    {
                        return;
                    }

                    var propertyDrawerForType = (PropertyDrawer)System.Activator.CreateInstance(drawerType);
                    propertyDrawerForType.m_FieldInfo = field;

                    // Will be null by design if default type drawer!
                    propertyDrawerForType.m_Attribute = attribute;

                    if (m_PropertyDrawers == null)
                    {
                        m_PropertyDrawers = new List <PropertyDrawer>();
                    }
                    m_PropertyDrawers.Add(propertyDrawerForType);
                }
                else if (typeof(DecoratorDrawer).IsAssignableFrom(drawerType))
                {
                    // Draw decorators on array itself, not on each array elements
                    if (field != null && field.FieldType.IsArrayOrList() && !propertyType.IsArrayOrList())
                    {
                        return;
                    }

                    DecoratorDrawer decoratorDrawerForType = (DecoratorDrawer)System.Activator.CreateInstance(drawerType);
                    decoratorDrawerForType.m_Attribute = attribute;

                    if (m_DecoratorDrawers == null)
                    {
                        m_DecoratorDrawers = new List <DecoratorDrawer>();
                    }
                    m_DecoratorDrawers.Add(decoratorDrawerForType);
                }
            }
        }
Example #16
0
        internal static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, out Type type)
        {
            Type      scriptTypeFromProperty = ScriptAttributeUtility.GetScriptTypeFromProperty(property);
            FieldInfo result;

            if (scriptTypeFromProperty == null)
            {
                type   = null;
                result = null;
            }
            else
            {
                result = ScriptAttributeUtility.GetFieldInfoFromPropertyPath(scriptTypeFromProperty, property.propertyPath, out type);
            }
            return(result);
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            System.Type fieldType;
            ScriptAttributeUtility.GetFieldInfoFromProperty(property, out fieldType);

            EditorGUI.BeginChangeCheck();

            var value = property.objectReferenceValue;

            position = EditorGUI.PrefixLabel(position, label);
            value    = EditorGUI.ObjectField(position, value, fieldType.GetGenericArguments()[0], false);

            if (EditorGUI.EndChangeCheck())
            {
                property.objectReferenceValue = value;
            }
        }
        public float GetHeight(SerializedProperty property, GUIContent label, bool includeChildren)
        {
            float height = 0;

            if (m_DecoratorDrawers != null && !isCurrentlyNested)
            {
                foreach (DecoratorDrawer drawer in m_DecoratorDrawers)
                {
                    height += drawer.GetHeight();
                }
            }

            if (propertyDrawer != null)
            {
                height += propertyDrawer.GetPropertyHeightSafe(property.Copy(), label ?? EditorGUIUtility.TempContent(property.displayName));
            }
            else if (!includeChildren)
            {
                height += EditorGUI.GetSinglePropertyHeight(property, label);
            }
            else
            {
                property = property.Copy();

                // First property with custom label
                height += EditorGUI.GetSinglePropertyHeight(property, label);
                bool childrenAreExpanded = property.isExpanded && EditorGUI.HasVisibleChildFields(property);

                // Loop through all child properties
                var tc = EditorGUIUtility.TempContent(property.displayName);
                if (childrenAreExpanded)
                {
                    SerializedProperty endProperty = property.GetEndProperty();
                    while (property.NextVisible(childrenAreExpanded) && !SerializedProperty.EqualContents(property, endProperty))
                    {
                        height += ScriptAttributeUtility.GetHandler(property).GetHeight(property, tc, true);
                        childrenAreExpanded = false;
                        height += EditorGUI.kControlVerticalSpacing;
                    }
                }
            }

            return(height);
        }
Example #19
0
        public bool CanCacheInspectorGUI(SerializedProperty property)
        {
            if (m_DecoratorDrawers != null &&
                !isCurrentlyNested &&
                m_DecoratorDrawers.Any(decorator => !decorator.CanCacheInspectorGUI()))
            {
                return(false);
            }

            if (propertyDrawer != null)
            {
                // Retrieve drawer BEFORE increasing nesting.
                PropertyDrawer drawer = propertyDrawer;
                using (var nestingContext = IncrementNestingContext())
                {
                    return(drawer.CanCacheInspectorGUISafe(property.Copy()));
                }
            }

            property = property.Copy();

            bool childrenAreExpanded = property.isExpanded && EditorGUI.HasVisibleChildFields(property);

            // Loop through all child properties
            if (childrenAreExpanded)
            {
                PropertyHandler    handler     = null;
                SerializedProperty endProperty = property.GetEndProperty();
                while (property.NextVisible(childrenAreExpanded) && !SerializedProperty.EqualContents(property, endProperty))
                {
                    if (handler == null)
                    {
                        handler = ScriptAttributeUtility.GetHandler(property);
                    }
                    if (!handler.CanCacheInspectorGUI(property))
                    {
                        return(false);
                    }
                    childrenAreExpanded = false;
                }
            }

            return(true);
        }
        public float GetHeight(SerializedProperty property, GUIContent label, bool includeChildren)
        {
            float num1 = 0.0f;

            if (this.m_DecoratorDrawers != null && !this.isCurrentlyNested)
            {
                using (List <DecoratorDrawer> .Enumerator enumerator = this.m_DecoratorDrawers.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        DecoratorDrawer current = enumerator.Current;
                        num1 += current.GetHeight();
                    }
                }
            }
            float num2;

            if (this.propertyDrawer != null)
            {
                num2 = num1 + this.propertyDrawer.GetPropertyHeightSafe(property.Copy(), label ?? EditorGUIUtility.TempContent(property.displayName));
            }
            else if (!includeChildren)
            {
                num2 = num1 + EditorGUI.GetSinglePropertyHeight(property, label);
            }
            else
            {
                property = property.Copy();
                SerializedProperty endProperty = property.GetEndProperty();
                num2 = num1 + EditorGUI.GetSinglePropertyHeight(property, label);
                bool enterChildren = property.isExpanded && EditorGUI.HasVisibleChildFields(property);
                while (property.NextVisible(enterChildren) && !SerializedProperty.EqualContents(property, endProperty))
                {
                    float num3 = num2 + ScriptAttributeUtility.GetHandler(property).GetHeight(property, EditorGUIUtility.TempContent(property.displayName), true);
                    enterChildren = false;
                    num2          = num3 + 2f;
                }
            }
            return(num2);
        }
        internal override bool OnOptimizedInspectorGUI(Rect contentRect)
        {
            bool childrenAreExpanded = true;
            bool wasEnabled          = GUI.enabled;
            var  visibleRect         = GUIClip.visibleRect;
            var  contentOffset       = contentRect.y;

            contentRect.xMin += InspectorWindow.kInspectorPaddingLeft;
            contentRect.xMax -= InspectorWindow.kInspectorPaddingRight;
            contentRect.y    += EditorGUI.kControlVerticalSpacing;

            var property = m_SerializedObject.GetIterator();
            var isInspectorModeNormal = m_InspectorMode == InspectorMode.Normal;

            while (property.NextVisible(childrenAreExpanded))
            {
                var handler = ScriptAttributeUtility.GetHandler(property);
                contentRect.height = handler.GetHeight(property, null, false);

                if (contentRect.Overlaps(visibleRect))
                {
                    EditorGUI.indentLevel = property.depth;
                    using (new EditorGUI.DisabledScope(isInspectorModeNormal && "m_Script" == property.propertyPath))
                        childrenAreExpanded = handler.OnGUI(contentRect, property, null, false, visibleRect);
                }
                else
                {
                    childrenAreExpanded = property.isExpanded;
                }

                contentRect.y += contentRect.height + EditorGUI.kControlVerticalSpacing;
            }

            m_LastHeight = contentRect.y - contentOffset;
            GUI.enabled  = wasEnabled;
            return(m_SerializedObject.ApplyModifiedProperties());
        }
Example #22
0
        internal static Type GetDrawerTypeForType(Type type)
        {
            if (ScriptAttributeUtility.s_DrawerTypeForType == null)
            {
                ScriptAttributeUtility.BuildDrawerTypeForTypeDictionary();
            }
            ScriptAttributeUtility.DrawerKeySet drawerKeySet;
            ScriptAttributeUtility.s_DrawerTypeForType.TryGetValue(type, out drawerKeySet);
            Type drawer;

            if (drawerKeySet.drawer != null)
            {
                drawer = drawerKeySet.drawer;
            }
            else
            {
                if (type.IsGenericType)
                {
                    ScriptAttributeUtility.s_DrawerTypeForType.TryGetValue(type.GetGenericTypeDefinition(), out drawerKeySet);
                }
                drawer = drawerKeySet.drawer;
            }
            return(drawer);
        }
Example #23
0
        internal static PropertyHandler GetHandler(SerializedProperty property)
        {
            PropertyHandler result;

            if (property == null)
            {
                result = ScriptAttributeUtility.s_SharedNullHandler;
            }
            else if (property.serializedObject.inspectorMode != InspectorMode.Normal)
            {
                result = ScriptAttributeUtility.s_SharedNullHandler;
            }
            else
            {
                PropertyHandler handler = ScriptAttributeUtility.propertyHandlerCache.GetHandler(property);
                if (handler != null)
                {
                    result = handler;
                }
                else
                {
                    Type type = null;
                    List <PropertyAttribute> list   = null;
                    FieldInfo          field        = null;
                    UnityEngine.Object targetObject = property.serializedObject.targetObject;
                    if (targetObject is MonoBehaviour || targetObject is ScriptableObject)
                    {
                        field = ScriptAttributeUtility.GetFieldInfoFromProperty(property, out type);
                        list  = ScriptAttributeUtility.GetFieldAttributes(field);
                    }
                    else
                    {
                        if (ScriptAttributeUtility.s_BuiltinAttributes == null)
                        {
                            ScriptAttributeUtility.PopulateBuiltinAttributes();
                        }
                        if (list == null)
                        {
                            list = ScriptAttributeUtility.GetBuiltinAttributes(property);
                        }
                    }
                    handler = ScriptAttributeUtility.s_NextHandler;
                    if (list != null)
                    {
                        for (int i = list.Count - 1; i >= 0; i--)
                        {
                            handler.HandleAttribute(list[i], field, type);
                        }
                    }
                    if (!handler.hasPropertyDrawer && type != null)
                    {
                        handler.HandleDrawnType(type, type, field, null);
                    }
                    if (handler.empty)
                    {
                        ScriptAttributeUtility.propertyHandlerCache.SetHandler(property, ScriptAttributeUtility.s_SharedNullHandler);
                        handler = ScriptAttributeUtility.s_SharedNullHandler;
                    }
                    else
                    {
                        ScriptAttributeUtility.propertyHandlerCache.SetHandler(property, handler);
                        ScriptAttributeUtility.s_NextHandler = new PropertyHandler();
                    }
                    result = handler;
                }
            }
            return(result);
        }
        internal bool OnGUI(Rect position, SerializedProperty property, GUIContent label, bool includeChildren, Rect visibleArea)
        {
            float oldLabelWidth, oldFieldWidth;

            float propHeight = position.height;

            position.height = 0;
            if (m_DecoratorDrawers != null && !isCurrentlyNested)
            {
                foreach (DecoratorDrawer decorator in m_DecoratorDrawers)
                {
                    position.height = decorator.GetHeight();

                    oldLabelWidth = EditorGUIUtility.labelWidth;
                    oldFieldWidth = EditorGUIUtility.fieldWidth;
                    decorator.OnGUI(position);
                    EditorGUIUtility.labelWidth = oldLabelWidth;
                    EditorGUIUtility.fieldWidth = oldFieldWidth;

                    position.y += position.height;
                    propHeight -= position.height;
                }
            }

            position.height = propHeight;
            if (propertyDrawer != null)
            {
                // Remember widths
                oldLabelWidth = EditorGUIUtility.labelWidth;
                oldFieldWidth = EditorGUIUtility.fieldWidth;
                // Draw with custom drawer
                propertyDrawer.OnGUISafe(position, property.Copy(), label ?? EditorGUIUtility.TempContent(property.localizedDisplayName));
                // Restore widths
                EditorGUIUtility.labelWidth = oldLabelWidth;
                EditorGUIUtility.fieldWidth = oldFieldWidth;

                return(false);
            }
            else
            {
                if (!includeChildren)
                {
                    return(EditorGUI.DefaultPropertyField(position, property, label));
                }

                // Remember state
                Vector2 oldIconSize = EditorGUIUtility.GetIconSize();
                bool    wasEnabled  = GUI.enabled;
                int     origIndent  = EditorGUI.indentLevel;

                int relIndent = origIndent - property.depth;

                SerializedProperty prop = property.Copy();

                position.height = EditorGUI.GetSinglePropertyHeight(prop, label);

                // First property with custom label
                EditorGUI.indentLevel = prop.depth + relIndent;
                bool childrenAreExpanded = EditorGUI.DefaultPropertyField(position, prop, label) && EditorGUI.HasVisibleChildFields(prop);
                position.y += position.height + EditorGUI.kControlVerticalSpacing;

                // Loop through all child properties
                if (childrenAreExpanded)
                {
                    SerializedProperty endProperty = prop.GetEndProperty();
                    while (prop.NextVisible(childrenAreExpanded) && !SerializedProperty.EqualContents(prop, endProperty))
                    {
                        var handler = ScriptAttributeUtility.GetHandler(prop);
                        EditorGUI.indentLevel = prop.depth + relIndent;
                        position.height       = handler.GetHeight(prop, null, false);

                        if (position.Overlaps(visibleArea))
                        {
                            EditorGUI.BeginChangeCheck();
                            childrenAreExpanded = handler.OnGUI(position, prop, null, false) && EditorGUI.HasVisibleChildFields(prop);
                            // Changing child properties (like array size) may invalidate the iterator,
                            // so stop now, or we may get errors.
                            if (EditorGUI.EndChangeCheck())
                            {
                                break;
                            }
                        }

                        position.y += position.height + EditorGUI.kControlVerticalSpacing;
                    }
                }

                // Restore state
                GUI.enabled = wasEnabled;
                EditorGUIUtility.SetIconSize(oldIconSize);
                EditorGUI.indentLevel = origIndent;

                return(false);
            }
        }
Example #25
0
        internal override bool GetOptimizedGUIBlock(bool isDirty, bool isVisible, out float height)
        {
            height = -1;

            // Don't use optimizedGUI for audio filters
            var behaviour = target as MonoBehaviour;

            if (behaviour != null && AudioUtil.HasAudioCallback(behaviour) && AudioUtil.GetCustomFilterChannelCount(behaviour) > 0)
            {
                return(false);
            }

            if (ObjectIsMonoBehaviourOrScriptableObject(target))
            {
                return(false);
            }

            if (isDirty)
            {
                ResetOptimizedBlock();
            }

            if (!isVisible)
            {
                height = 0;
                return(true);
            }

            // Return cached result if any.
            if (m_OptimizedBlockState != OptimizedBlockState.CheckOptimizedBlock)
            {
                if (m_OptimizedBlockState == OptimizedBlockState.NoOptimizedBlock)
                {
                    return(false);
                }
                height = m_LastHeight;
                return(true);
            }

            // Update serialized object representation
            if (m_SerializedObject == null)
            {
                m_SerializedObject = new SerializedObject(targets, m_Context)
                {
                    inspectorMode = inspectorMode
                }
            }
            ;
            else
            {
                m_SerializedObject.Update();
                m_SerializedObject.inspectorMode = inspectorMode;
            }

            height = 0;
            SerializedProperty property = m_SerializedObject.GetIterator();
            bool childrenAreExpanded    = true;

            while (property.NextVisible(childrenAreExpanded))
            {
                var handler = ScriptAttributeUtility.GetHandler(property);
                height += handler.GetHeight(property, null, false) + EditorGUI.kControlVerticalSpacing;

                childrenAreExpanded = property.isExpanded && EditorGUI.HasVisibleChildFields(property);
            }

            m_LastHeight          = height;
            m_OptimizedBlockState = OptimizedBlockState.HasOptimizedBlock;

            return(true);
        }
Example #26
0
        public float GetHeight(SerializedProperty property, GUIContent label, bool includeChildren)
        {
            float height = 0;

            if (m_DecoratorDrawers != null && !isCurrentlyNested)
            {
                foreach (DecoratorDrawer drawer in m_DecoratorDrawers)
                {
                    height += drawer.GetHeight();
                }
            }


            if (propertyDrawer != null)
            {
                // Retrieve drawer BEFORE increasing nesting.
                PropertyDrawer drawer = propertyDrawer;
                using (var nestingContext = IncrementNestingContext())
                {
                    height += drawer.GetPropertyHeightSafe(property.Copy(), label ?? EditorGUIUtility.TempContent(property.localizedDisplayName, tooltip));
                }
            }
            else if (!includeChildren)
            {
                height += EditorGUI.GetSinglePropertyHeight(property, label);
            }
            else if (UseReorderabelListControl(property))
            {
                ReorderableListWrapper reorderableList;
                string key = ReorderableListWrapper.GetPropertyIdentifier(property);

                // If collection doesn't have a ReorderableList assigned to it, create one and assign it
                if (!s_reorderableLists.TryGetValue(key, out reorderableList))
                {
                    reorderableList         = new ReorderableListWrapper(property, label, true);
                    s_reorderableLists[key] = reorderableList;
                }

                reorderableList.Property = property;
                height += s_reorderableLists[key].GetHeight();
                return(height);
            }
            else
            {
                property = property.Copy();

                // First property with custom label
                height += EditorGUI.GetSinglePropertyHeight(property, label);
                bool childrenAreExpanded = property.isExpanded && EditorGUI.HasVisibleChildFields(property);

                // Loop through all child properties
                var tc = EditorGUIUtility.TempContent(property.localizedDisplayName, tooltip);
                if (childrenAreExpanded)
                {
                    SerializedProperty endProperty = property.GetEndProperty();
                    while (property.NextVisible(childrenAreExpanded) && !SerializedProperty.EqualContents(property, endProperty))
                    {
                        height += ScriptAttributeUtility.GetHandler(property).GetHeight(property, tc, true);
                        childrenAreExpanded = false;
                        height += EditorGUI.kControlVerticalSpacing;
                    }
                }
            }

            return(height);
        }
Example #27
0
        internal bool OnGUI(Rect position, SerializedProperty property, GUIContent label, bool includeChildren, Rect visibleArea)
        {
            TestInvalidateCache();

            float oldLabelWidth, oldFieldWidth;

            float propHeight = position.height;

            position.height = 0;
            if (m_DecoratorDrawers != null && !isCurrentlyNested)
            {
                foreach (DecoratorDrawer decorator in m_DecoratorDrawers)
                {
                    position.height = decorator.GetHeight();

                    oldLabelWidth = EditorGUIUtility.labelWidth;
                    oldFieldWidth = EditorGUIUtility.fieldWidth;
                    decorator.OnGUI(position);
                    EditorGUIUtility.labelWidth = oldLabelWidth;
                    EditorGUIUtility.fieldWidth = oldFieldWidth;

                    position.y += position.height;
                    propHeight -= position.height;
                }
            }

            position.height = propHeight;
            if (propertyDrawer != null)
            {
                // Remember widths
                oldLabelWidth = EditorGUIUtility.labelWidth;
                oldFieldWidth = EditorGUIUtility.fieldWidth;
                // Draw with custom drawer - retrieve it BEFORE increasing nesting.
                PropertyDrawer drawer = propertyDrawer;

                using (var nestingContext = IncrementNestingContext())
                {
                    drawer.OnGUISafe(position, property.Copy(), label ?? EditorGUIUtility.TempContent(property.localizedDisplayName, tooltip));
                }

                // Restore widths
                EditorGUIUtility.labelWidth = oldLabelWidth;
                EditorGUIUtility.fieldWidth = oldFieldWidth;

                return(false);
            }
            else
            {
                if (!includeChildren)
                {
                    return(EditorGUI.DefaultPropertyField(position, property, label));
                }

                if (UseReorderabelListControl(property))
                {
                    ReorderableListWrapper reorderableList;
                    string key = ReorderableListWrapper.GetPropertyIdentifier(property);

                    if (!s_reorderableLists.TryGetValue(key, out reorderableList))
                    {
                        // Manual layout controls don't call GetHeight() method so we need to have a way to initialized list as we prepare to render it here
                        reorderableList         = new ReorderableListWrapper(property, label, true);
                        s_reorderableLists[key] = reorderableList;
                    }

                    // Calculate visibility rect specifically for reorderable list as when applied for the whole serialized object,
                    // it causes collapsed out of sight array elements appear thus messing up scroll-bar experience
                    var screenPos = GUIUtility.GUIToScreenPoint(position.position);

                    screenPos.y = Mathf.Clamp(screenPos.y,
                                              GUIView.current?.screenPosition.yMin ?? 0,
                                              GUIView.current?.screenPosition.yMax ?? Screen.height);

                    Rect listVisibility = new Rect(screenPos.x, screenPos.y,
                                                   GUIView.current?.screenPosition.width ?? Screen.width,
                                                   GUIView.current?.screenPosition.height ?? Screen.height);

                    listVisibility = GUIUtility.ScreenToGUIRect(listVisibility);

                    reorderableList.Property = property;
                    reorderableList.Draw(label, position, listVisibility, tooltip, includeChildren);
                    return(!includeChildren && property.isExpanded);
                }

                // Remember state
                Vector2 oldIconSize = EditorGUIUtility.GetIconSize();
                bool    wasEnabled  = GUI.enabled;
                int     origIndent  = EditorGUI.indentLevel;

                int relIndent = origIndent - property.depth;

                SerializedProperty prop = property.Copy();

                position.height = EditorGUI.GetSinglePropertyHeight(prop, label);

                // First property with custom label
                EditorGUI.indentLevel = prop.depth + relIndent;
                bool childrenAreExpanded = EditorGUI.DefaultPropertyField(position, prop, label) && EditorGUI.HasVisibleChildFields(prop);
                position.y += position.height + EditorGUI.kControlVerticalSpacing;

                // Loop through all child properties
                if (childrenAreExpanded)
                {
                    SerializedProperty endProperty = prop.GetEndProperty();
                    while (prop.NextVisible(childrenAreExpanded) && !SerializedProperty.EqualContents(prop, endProperty))
                    {
                        var handler = ScriptAttributeUtility.GetHandler(prop);
                        EditorGUI.indentLevel = prop.depth + relIndent;
                        position.height       = handler.GetHeight(prop, null, UseReorderabelListControl(prop) && includeChildren);

                        if (position.Overlaps(visibleArea))
                        {
                            EditorGUI.BeginChangeCheck();
                            childrenAreExpanded = handler.OnGUI(position, prop, null, UseReorderabelListControl(prop)) && EditorGUI.HasVisibleChildFields(prop);
                            // Changing child properties (like array size) may invalidate the iterator,
                            // so stop now, or we may get errors.
                            if (EditorGUI.EndChangeCheck())
                            {
                                break;
                            }
                        }

                        position.y += position.height + EditorGUI.kControlVerticalSpacing;
                    }
                }

                // Restore state
                GUI.enabled = wasEnabled;
                EditorGUIUtility.SetIconSize(oldIconSize);
                EditorGUI.indentLevel = origIndent;

                return(false);
            }
        }
        internal void Show(UnityObject obj, Type requiredType, SerializedProperty property, bool allowSceneObjects, List <int> allowedInstanceIDs, Action <UnityObject> onObjectSelectorClosed, Action <UnityObject> onObjectSelectedUpdated)
        {
            m_ObjectSelectorReceiver = null;
            m_AllowSceneObjects      = allowSceneObjects;
            m_IsShowingAssets        = true;
            m_SkipHiddenPackages     = true;
            m_AllowedIDs             = allowedInstanceIDs;

            m_OnObjectSelectorClosed  = onObjectSelectorClosed;
            m_OnObjectSelectorUpdated = onObjectSelectedUpdated;

            if (property != null)
            {
                if (requiredType == null)
                {
                    ScriptAttributeUtility.GetFieldInfoFromProperty(property, out requiredType);
                    // case 951876: built-in types do not actually have reflectable fields, so their object types must be extracted from the type string
                    // this works because built-in types will only ever have serialized references to other built-in types, which this window's filter expects as unqualified names
                    if (requiredType == null)
                    {
                        m_RequiredType = s_MatchPPtrTypeName.Match(property.type).Groups[1].Value;
                    }
                }

                obj = property.objectReferenceValue;
                m_ObjectBeingEdited = property.serializedObject.targetObject;

                // Do not allow to show scene objects if the object being edited is persistent
                if (m_ObjectBeingEdited != null && EditorUtility.IsPersistent(m_ObjectBeingEdited))
                {
                    m_AllowSceneObjects = false;
                }
            }

            // Set which tab should be visible at startup
            if (m_AllowSceneObjects)
            {
                if (obj != null)
                {
                    if (typeof(Component).IsAssignableFrom(obj.GetType()))
                    {
                        obj = ((Component)obj).gameObject;
                    }
                    // Set the right tab visible (so we can see our selection)
                    m_IsShowingAssets = EditorUtility.IsPersistent(obj);
                }
                else
                {
                    m_IsShowingAssets = (requiredType != typeof(GameObject) && !typeof(Component).IsAssignableFrom(requiredType));
                }
            }
            else
            {
                m_IsShowingAssets = true;
            }

            // Set member variables
            m_DelegateView = GUIView.current;
            // type filter requires unqualified names for built-in types, but will prioritize them over user types, so ensure user types are namespace-qualified
            if (requiredType != null)
            {
                m_RequiredType = typeof(ScriptableObject).IsAssignableFrom(requiredType) || typeof(MonoBehaviour).IsAssignableFrom(requiredType) ? requiredType.FullName : requiredType.Name;
            }
            m_SearchFilter      = "";
            m_OriginalSelection = obj;
            m_ModalUndoGroup    = Undo.GetCurrentGroup();

            // Freeze to prevent flicker on OSX.
            // Screen will be updated again when calling
            // SetFreezeDisplay(false) further down.
            ContainerWindow.SetFreezeDisplay(true);

            ShowWithMode(ShowMode.AuxWindow);
            titleContent = EditorGUIUtility.TrTextContent("Select " + (requiredType == null ? m_RequiredType : requiredType.Name));

            // Deal with window size
            Rect p = m_Parent == null ? new Rect(0, 0, 1, 1) : m_Parent.window.position;

            p.width  = EditorPrefs.GetFloat("ObjectSelectorWidth", 200);
            p.height = EditorPrefs.GetFloat("ObjectSelectorHeight", 390);
            position = p;
            minSize  = new Vector2(kMinWidth, kMinTopSize + kPreviewExpandedAreaHeight + 2 * kPreviewMargin);
            maxSize  = new Vector2(10000, 10000);
            SetupPreview();

            // Focus
            Focus();
            ContainerWindow.SetFreezeDisplay(false);

            m_FocusSearchFilter = true;

            // Add after unfreezing display because AuxWindowManager.cpp assumes that aux windows are added after we get 'got/lost'- focus calls.
            if (m_Parent != null)
            {
                m_Parent.AddToAuxWindowList();
            }

            // Initial selection
            int initialSelection = obj != null?obj.GetInstanceID() : 0;

            if (property != null && property.hasMultipleDifferentValues)
            {
                initialSelection = 0; // don't select anything on multi selection
            }
            if (initialSelection != 0)
            {
                var assetPath = AssetDatabase.GetAssetPath(initialSelection);
                if (m_SkipHiddenPackages && !PackageManagerUtilityInternal.IsPathInVisiblePackage(assetPath))
                {
                    m_SkipHiddenPackages = false;
                }
            }

            if (ShouldTreeViewBeUsed(m_RequiredType))
            {
                m_ObjectTreeWithSearch.Init(position, this, CreateAndSetTreeView, TreeViewSelection, ItemWasDoubleClicked, initialSelection, 0);
            }
            else
            {
                // To frame the selected item we need to wait to initialize the search until our window has been setup
                InitIfNeeded();
                m_ListArea.InitSelection(new[] { initialSelection });
                if (initialSelection != 0)
                {
                    m_ListArea.Frame(initialSelection, true, false);
                }
            }
        }
Example #29
0
 private static void PopulateBuiltinAttributes()
 {
     ScriptAttributeUtility.s_BuiltinAttributes = new Dictionary <string, List <PropertyAttribute> >();
     ScriptAttributeUtility.AddBuiltinAttribute("GUIText", "m_Text", new MultilineAttribute());
     ScriptAttributeUtility.AddBuiltinAttribute("TextMesh", "m_Text", new MultilineAttribute());
 }
Example #30
0
        public bool OnGUI(Rect position, SerializedProperty property, GUIContent label, bool includeChildren)
        {
            float num = position.height;

            position.height = 0f;
            if (this.m_DecoratorDrawers != null && !this.isCurrentlyNested)
            {
                foreach (DecoratorDrawer current in this.m_DecoratorDrawers)
                {
                    position.height = current.GetHeight();
                    float labelWidth = EditorGUIUtility.labelWidth;
                    float fieldWidth = EditorGUIUtility.fieldWidth;
                    current.OnGUI(position);
                    EditorGUIUtility.labelWidth = labelWidth;
                    EditorGUIUtility.fieldWidth = fieldWidth;
                    position.y += position.height;
                    num        -= position.height;
                }
            }
            position.height = num;
            if (this.propertyDrawer != null)
            {
                float labelWidth = EditorGUIUtility.labelWidth;
                float fieldWidth = EditorGUIUtility.fieldWidth;
                this.propertyDrawer.OnGUISafe(position, property.Copy(), label ?? EditorGUIUtility.TempContent(property.displayName));
                EditorGUIUtility.labelWidth = labelWidth;
                EditorGUIUtility.fieldWidth = fieldWidth;
                return(false);
            }
            if (!includeChildren)
            {
                return(EditorGUI.DefaultPropertyField(position, property, label));
            }
            Vector2            iconSize           = EditorGUIUtility.GetIconSize();
            bool               enabled            = GUI.enabled;
            int                indentLevel        = EditorGUI.indentLevel;
            int                num2               = indentLevel - property.depth;
            SerializedProperty serializedProperty = property.Copy();
            SerializedProperty endProperty        = serializedProperty.GetEndProperty();

            position.height       = EditorGUI.GetSinglePropertyHeight(serializedProperty, label);
            EditorGUI.indentLevel = serializedProperty.depth + num2;
            bool enterChildren = EditorGUI.DefaultPropertyField(position, serializedProperty, label) && EditorGUI.HasVisibleChildFields(serializedProperty);

            position.y += position.height + 2f;
            while (serializedProperty.NextVisible(enterChildren) && !SerializedProperty.EqualContents(serializedProperty, endProperty))
            {
                EditorGUI.indentLevel = serializedProperty.depth + num2;
                position.height       = EditorGUI.GetPropertyHeight(serializedProperty, null, false);
                EditorGUI.BeginChangeCheck();
                enterChildren = (ScriptAttributeUtility.GetHandler(serializedProperty).OnGUI(position, serializedProperty, null, false) && EditorGUI.HasVisibleChildFields(serializedProperty));
                if (EditorGUI.EndChangeCheck())
                {
                    break;
                }
                position.y += position.height + 2f;
            }
            GUI.enabled = enabled;
            EditorGUIUtility.SetIconSize(iconSize);
            EditorGUI.indentLevel = indentLevel;
            return(false);
        }