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);
        }
        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);
        }
Esempio n. 3
0
        SelectorHandlerType GetSelectorHandlerFromProperty(SerializedProperty property)
        {
            var fieldInfo = ScriptAttributeUtility.GetFieldInfoFromProperty(property, out _);

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

            return(GetSelectorHandlerFromFieldInfo(fieldInfo));
        }
Esempio n. 4
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);
        }
Esempio n. 5
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);
        }
        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;
            }
        }
Esempio n. 7
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 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);
                }
            }
        }
Esempio n. 9
0
        internal void Show(UnityEngine.Object obj, Type requiredType, SerializedProperty property, bool allowSceneObjects, List <int> allowedInstanceIDs, Action <UnityEngine.Object> onObjectSelectorClosed, Action <UnityEngine.Object> onObjectSelectedUpdated)
        {
            this.m_ObjectSelectorReceiver  = null;
            this.m_AllowSceneObjects       = allowSceneObjects;
            this.m_IsShowingAssets         = true;
            this.m_AllowedIDs              = allowedInstanceIDs;
            this.m_OnObjectSelectorClosed  = onObjectSelectorClosed;
            this.m_OnObjectSelectorUpdated = onObjectSelectedUpdated;
            if (property != null)
            {
                if (requiredType == null)
                {
                    ScriptAttributeUtility.GetFieldInfoFromProperty(property, out requiredType);
                    if (requiredType == null)
                    {
                        this.m_RequiredType = this.s_MatchPPtrTypeName.Match(property.type).Groups[1].Value;
                    }
                }
                obj = property.objectReferenceValue;
                this.m_ObjectBeingEdited = property.serializedObject.targetObject;
                if (this.m_ObjectBeingEdited != null && EditorUtility.IsPersistent(this.m_ObjectBeingEdited))
                {
                    this.m_AllowSceneObjects = false;
                }
            }
            if (this.m_AllowSceneObjects)
            {
                if (obj != null)
                {
                    if (typeof(Component).IsAssignableFrom(obj.GetType()))
                    {
                        obj = ((Component)obj).gameObject;
                    }
                    this.m_IsShowingAssets = EditorUtility.IsPersistent(obj);
                }
                else
                {
                    this.m_IsShowingAssets = (requiredType != typeof(GameObject) && !typeof(Component).IsAssignableFrom(requiredType));
                }
            }
            else
            {
                this.m_IsShowingAssets = true;
            }
            this.m_DelegateView = GUIView.current;
            if (requiredType != null)
            {
                this.m_RequiredType = ((!typeof(ScriptableObject).IsAssignableFrom(requiredType) && !typeof(MonoBehaviour).IsAssignableFrom(requiredType)) ? requiredType.Name : requiredType.FullName);
            }
            this.m_SearchFilter      = "";
            this.m_OriginalSelection = obj;
            this.m_ModalUndoGroup    = Undo.GetCurrentGroup();
            ContainerWindow.SetFreezeDisplay(true);
            base.ShowWithMode(ShowMode.AuxWindow);
            base.titleContent = EditorGUIUtility.TrTextContent("Select " + ((requiredType != null) ? requiredType.Name : this.m_RequiredType), null, null);
            Rect position = this.m_Parent.window.position;

            position.width  = EditorPrefs.GetFloat("ObjectSelectorWidth", 200f);
            position.height = EditorPrefs.GetFloat("ObjectSelectorHeight", 390f);
            base.position   = position;
            base.minSize    = new Vector2(200f, 335f);
            base.maxSize    = new Vector2(10000f, 10000f);
            this.SetupPreview();
            base.Focus();
            ContainerWindow.SetFreezeDisplay(false);
            this.m_FocusSearchFilter = true;
            this.m_Parent.AddToAuxWindowList();
            int num = (!(obj != null)) ? 0 : obj.GetInstanceID();

            if (property != null && property.hasMultipleDifferentValues)
            {
                num = 0;
            }
            if (ObjectSelector.ShouldTreeViewBeUsed(requiredType))
            {
                this.m_ObjectTreeWithSearch.Init(base.position, this, new UnityAction <ObjectTreeForSelector.TreeSelectorData>(this.CreateAndSetTreeView), new UnityAction <TreeViewItem>(this.TreeViewSelection), new UnityAction(this.ItemWasDoubleClicked), num, 0);
            }
            else
            {
                this.InitIfNeeded();
                this.m_ListArea.InitSelection(new int[]
                {
                    num
                });
                if (num != 0)
                {
                    this.m_ListArea.Frame(num, true, false);
                }
            }
        }