Exemplo n.º 1
0
        protected override void OnEnable()
        {
            base.OnEnable();
            assetType = serializedObject.FindProperty("assetType");
            missingPropertyDisplay = serializedObject.FindProperty("missingPropertyDisplay");
            additionalColumns      = serializedObject.FindProperty("additionalColumns");
            columns                      = serializedObject.FindProperty("columns");
            typeString                   = serializedObject.FindProperty("typeString");
            nameDisplay                  = serializedObject.FindProperty("nameDisplay");
            iconPropertyPath             = serializedObject.FindProperty("iconPropertyPath");
            iconIsArray                  = serializedObject.FindProperty("iconIsArray");
            iconArrayPropertyInformation = serializedObject.FindProperty("iconArrayPropertyInformation");

            type = Type.GetType(typeString.stringValue);

            if (type == null)
            {
                return;
            }

            typeIsTextureOrSprite = typeof(Texture).IsAssignableFrom(type) || typeof(Sprite).IsAssignableFrom(type);
            typeIsAsset           = !type.IsSubclassOf(typeof(Component));

            backgroundOffset = new RectOffset(20, 6, 2, 0);
            reorderableList  = new ReorderableList(serializedObject, columns)
            {
                drawElementCallback = (rect, index, active, focused) =>
                {
                    float min = rect.y;
                    if (index % 2 == 0)
                    {
                        EditorGUI.DrawRect(backgroundOffset.Add(rect), new Color(0f, 0f, 0f, 0.075f));
                    }
                    SerializedProperty column       = columns.GetArrayElementAtIndex(index);
                    SerializedProperty propertyPath = column.FindPropertyRelative("PropertyPath");
                    SerializedProperty title        = column.FindPropertyRelative("Title");
                    rect.height = EditorGUIUtility.singleLineHeight;
                    EditorGUI.PropertyField(rect, title);
                    rect.NextGUIRect();
                    using (new EditorGUI.DisabledScope(true))
                        EditorGUI.PropertyField(rect, propertyPath);

                    var propertyType = (SerializedPropertyType)column.FindPropertyRelative("PropertyType").intValue;

                    string propertyName  = null;
                    bool   captureHeight = false;

                    if (propertyType == SerializedPropertyType.Generic)
                    {
                        /*
                         *      bool IsArray;
                         *      ArrayIndexing ArrayIndexing;
                         *      string ArrayPropertyKey;
                         *      string ArrayQuery;
                         *      int ArrayIndex;
                         *      string ArrayPropertyPath;
                         */
                        captureHeight = true;
                        bool isArray = column.FindPropertyRelative("IsArray").boolValue;
                        if (isArray)
                        {
                            var arrayPropertyInformation = column.FindPropertyRelative("ArrayPropertyInformation");

                            ArrayDataDrawer.OnGUI(ref rect, propertyPath, arrayPropertyInformation, referenceObject, null, rect.x - 20, -16);

                            var arrayPropertyPath = arrayPropertyInformation.FindPropertyRelative("ArrayPropertyPath");
                            if (!string.IsNullOrEmpty(arrayPropertyPath.stringValue))
                            {
                                propertyType = (SerializedPropertyType)arrayPropertyInformation.FindPropertyRelative("ArrayPropertyType").intValue;
                                propertyName = GetPropertyDisplayString(propertyType);
                            }
                        }
                    }
                    else
                    {
                        propertyName = GetPropertyDisplayString(propertyType);
                    }

                    if (propertyName != null)
                    {
                        rect.NextGUIRect();
                        EditorGUI.PropertyField(rect, column.FindPropertyRelative(propertyName));
                    }

                    if (captureHeight)
                    {
                        if (heightOverrideLookup.ContainsKey(index))
                        {
                            heightOverrideLookup[index] = rect.yMax - min;
                        }
                        else
                        {
                            heightOverrideLookup.Add(index, rect.yMax - min);
                        }
                    }
                },
                headerHeight          = 0,
                elementHeightCallback = index =>
                {
                    if (heightOverrideLookup.TryGetValue(index, out float height))
                    {
                        return(height);
                    }
                    return(EditorGUIUtils.HeightWithSpacing * 3);
                },
                onReorderCallback = list => heightOverrideLookup.Clear(),
                displayAdd        = false,
                onRemoveCallback  = list =>
                {
                    //Refresh property dropdown (this currently is only done to refresh the "enabled" state of the properties)
                    propertyDropdownState = null;
                    CreatePropertyDropdown();
                    heightOverrideLookup.Clear();
                    ReorderableList.defaultBehaviours.DoRemoveButton(list);
                }
            };

            referenceObject = LoadAssetByTypeName(type, type.IsSubclassOf(typeof(Component)), (AssetType)assetType.intValue);
            if (referenceObject != null)
            {
                CreatePropertyDropdown();
            }
        }
Exemplo n.º 2
0
        public override void OnInspectorGUI()
        {
            serializedObject.UpdateIfRequiredOrScript();

            if (type == null)
            {
                EditorGUILayout.HelpBox("The type associated with this Asset List could not be found.", MessageType.Error);
                EditorGUILayout.PropertyField(typeString);
                serializedObject.ApplyModifiedProperties();
                return;
            }

            using (new EditorGUI.DisabledScope(typeIsAsset))
            {
                //Force assets to only display as InAssets.
                if (typeIsAsset && assetType.intValue != 0)
                {
                    assetType.intValue = 0;
                }
                EditorGUILayout.PropertyField(assetType);
            }

            using (var cCS = new EditorGUI.ChangeCheckScope())
            {
                referenceObject = EditorGUILayout.ObjectField(referenceObjectLabel, referenceObject, type, true, singleHeight);
                if (cCS.changed)
                {
                    if (referenceObject != null)
                    {
                        CreatePropertyDropdown();
                    }
                }
            }

            if (EditorGUIUtils.DrawHeaderWithFoldout(preferencesLabel, missingPropertyDisplay.isExpanded, headerXOffset: -16f))
            {
                missingPropertyDisplay.isExpanded = !missingPropertyDisplay.isExpanded;
            }
            if (missingPropertyDisplay.isExpanded)
            {
                EditorGUILayout.PropertyField(missingPropertyDisplay);
                EditorGUILayout.PropertyField(additionalColumns);
            }
            EditorGUIUtils.DrawSplitter();

            GUILayout.Space(8);

            using (new EditorGUIUtils.ContainerScope(columnsLabel, -2))
                using (new EditorGUIUtils.OutlineScope(false, false))
                {
                    //Icon Header
                    GUILayout.Label(iconLabel, EditorGUIUtils.CenteredMiniLabel);

                    if (typeIsTextureOrSprite)
                    {
                        EditorGUILayout.HelpBox("Type inherits from Texture or Sprite. Icon is automated.", MessageType.Info);
                    }
                    else
                    {
                        //Icon property path
                        if (!string.IsNullOrEmpty(iconPropertyPath.stringValue))
                        {
                            EditorGUILayout.PropertyField(iconPropertyPath, GUIContent.none);
                        }
                        if (ValidateReferenceObjectWithHelpWarning(referenceObject))
                        {
                            //Icon property button.
                            Rect rect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
                            rect.height = EditorGUIUtility.singleLineHeight;                     //this is required to fix issues with the Layout stage
                            rect.xMin  += EditorGUI.indentLevel * 15;
                            if (GUI.Button(rect, findIconLabel))
                            {
                                CreateIconPropertyDropdown();
                                iconPropertyDropdown?.Show(rect);
                            }

                            bool isArray = iconIsArray.boolValue;
                            if (isArray)
                            {
                                float yBefore = rect.yMax;
                                ArrayDataDrawer.OnGUI(ref rect, iconPropertyPath, iconArrayPropertyInformation, referenceObject, typeStrings, rect.x - 2);
                                GUILayoutUtility.GetRect(0, rect.yMax - yBefore);
                            }
                        }
                    }

                    GUILayout.Space(6);
                    EditorGUIUtils.DrawSplitter(alignXMinToZero: false);
                    GUILayout.Space(4);
                    GUILayout.Label(propertyLabel, EditorGUIUtils.CenteredMiniLabel);
                    using (new EditorGUI.DisabledScope(true))
                        EditorGUILayout.TextField(titleLabel, "Name");

                    EditorGUILayout.PropertyField(nameDisplay);
                }

            reorderableList.DoLayoutList();

            using (new EditorGUIUtils.OutlineScope())
            {
                GUILayout.Label(searchlabel, EditorStyles.boldLabel);
                if (ValidateReferenceObjectWithHelpWarning(referenceObject))
                {
                    Rect rect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
                    if (GUI.Button(rect, addLabel))
                    {
                        if (propertyDropdown == null)
                        {
                            CreatePropertyDropdown();
                        }
                        propertyDropdown.Show(rect);
                    }
                }
            }

            serializedObject.ApplyModifiedProperties();
        }