Example #1
0
        public List <SearchTreeEntry> CreateSearchTree(SearchWindowContext context)
        {
            var entries = new List <SearchTreeEntry>();

            var groups     = new HashSet <string>();
            var count      = types.Count;
            var namespaces = new Dictionary <Type, string>(count);

            for (int i = types.Count - 1; i >= 0; i--)
            {
                var type = types[i];

                if (!type.IsVisible)
                {
                    types.RemoveAt(i);
                    continue;
                }

                var fullName = type.FullName;

                if (string.IsNullOrEmpty(type.Namespace))
                {
                    fullName = $"Player.{type.Name}";
                }

                namespaces.Add(type, fullName);
            }

            var sortedTypes = types.OrderBy(x => namespaces[x]).ToList();

            foreach (var type in sortedTypes)
            {
                var names  = namespaces[type].Split('.');
                var length = names.Length;

                var depth   = 1;
                var current = names[0];

                foreach (var name in names)
                {
                    if (depth > 1)
                    {
                        current += $".{name}";
                    }

                    if (length <= depth)
                    {
                        var content = new GUIContent(name, ScriptIcons.GetIcon(type.FullName));
                        entries.Add(new SearchTreeEntry(content)
                        {
                            level = length, userData = type
                        });
                    }
                    else
                    {
                        if (!groups.Contains(current))
                        {
                            groups.Add(current);
                            entries.Add(new SearchTreeGroupEntry(new GUIContent(name))
                            {
                                level = depth
                            });
                        }
                    }

                    depth++;
                }
            }

            entries.Insert(0, new SearchTreeGroupEntry(new GUIContent("Select type...")));

            return(entries);
        }
Example #2
0
        public TypesPriorityGUI(string header, SerializedProperty property)
        {
            ScriptIcons.RetrieveFromScriptTypes(componentTypes);

            var typesProperty = property.FindPropertyRelative("types");

            List = new ReorderableList(property.serializedObject, typesProperty);
            List.elementHeight += 1;

            if (header == null)
            {
                List.headerHeight = 1;
            }

            List.drawHeaderCallback = rect =>
            {
                GUI.Label(rect, header);
            };

            List.drawElementBackgroundCallback += (rect, index, active, focused) =>
            {
                if (active || focused)
                {
                    EditorGUI.DrawRect(rect, isProSkin ? new Color32(77, 77, 77, 255) : new Color32(174, 174, 174, 255));
                }
            };

            List.drawElementCallback = (rect, index, active, focused) =>
            {
                if (miniPullDown == null)
                {
                    miniPullDown = new GUIStyle("MiniPullDown")
                    {
                        fontSize = 11
                    };
                    var textColor = miniPullDown.normal.textColor;
                    textColor.a = 0.9f;
                    miniPullDown.normal.textColor = textColor;
                }

                var color    = GUI.color;
                var mousePos = Event.current.mousePosition;

                var item = typesProperty.GetArrayElementAtIndex(index);

                var assemblyName = item.FindPropertyRelative("assemblyQualifiedName");
                var fullName     = item.FindPropertyRelative("fullName");
                var isIgnored    = item.FindPropertyRelative("isIgnored");

                var displayName = fullName.stringValue;
                var hasName     = !string.IsNullOrEmpty(displayName);

                if (hasName && !displayNames.TryGetValue(item, out displayName))
                {
                    displayName        = fullName.stringValue.Replace("UnityEngine.", " ");
                    displayNames[item] = displayName;
                }

                tempContent.text  = hasName ? displayName : "None";
                tempContent.image = ScriptIcons.GetIcon(fullName.stringValue);

                SetIconSize(new Vector2(16, 16));

                rect.y += 2;

                EditorGUI.BeginChangeCheck();

                var visibilityRect = new Rect(rect)
                {
                    width = 20
                };
                var visibilityIcon = isIgnored.boolValue ? visibilityOff : visibilityOn;

                if (!isIgnored.boolValue)
                {
                    GUI.color = new Color(1, 1, 1, 0.5f);
                }

                if (GUI.Button(visibilityRect, visibilityIcon, GUIStyle.none))
                {
                    isIgnored.boolValue = !isIgnored.boolValue;
                }

                GUI.color = color;

                rect.xMin += visibilityRect.width;

                if (GUI.Button(rect, tempContent, miniPullDown))
                {
                    var position = Event.current.mousePosition;
                    position.y = rect.position.y + 35;
                    position   = GUIUtility.GUIToScreenPoint(position);

                    var context = new SearchWindowContext(position);

                    var popup = ScriptableObject.CreateInstance <TypeSearchPopup>();
                    popup.Initialize(componentTypes, type =>
                    {
                        fullName.stringValue     = type.FullName;
                        assemblyName.stringValue = type.AssemblyQualifiedName;
                        SaveChanges();
                    });
                    SearchWindow.Open(context, popup);
                }

                // PropertyField serialization doesn't work directly, something is totally wrong..
                if (EditorGUI.EndChangeCheck())
                {
                    SaveChanges();
                }
            };

            List.onChangedCallback += _ => SaveChanges();

            void SaveChanges()
            {
                // I've spent hours trying to figure why reordering/value change didn't trigger saving.

                // ApplyModifiedProperties on this array doesn't work unless you insert new element into it.
                // And this stupid fix makes it work! God save me.

                typesProperty.InsertArrayElementAtIndex(typesProperty.arraySize - 1);
                typesProperty.serializedObject.ApplyModifiedProperties();
                typesProperty.DeleteArrayElementAtIndex(typesProperty.arraySize - 1);
                typesProperty.serializedObject.ApplyModifiedProperties();

                onChange?.Invoke();
            }
        }
        public TypesPriorityGUI(string header, SerializedProperty property)
        {
            ScriptIcons.RetrieveFromScriptTypes(componentTypes);

            var typesProperty = property.FindPropertyRelative("types");

            List = new ReorderableList(property.serializedObject, typesProperty);
            List.elementHeight += 1;

            if (header == null)
            {
                List.headerHeight = 1;
            }

            List.drawHeaderCallback = rect =>
            {
                GUI.Label(rect, header);
            };

            List.drawElementCallback = (rect, index, active, focused) =>
            {
                rect.y += 2;

                var item         = typesProperty.GetArrayElementAtIndex(index);
                var assemblyName = item.FindPropertyRelative("assemblyQualifiedName");
                var name         = item.FindPropertyRelative("fullName");
                var displayName  = name.stringValue;
                var hasName      = !string.IsNullOrEmpty(displayName);

                if (miniPullDown == null)
                {
                    miniPullDown = new GUIStyle("MiniPullDown")
                    {
                        fontSize = 11
                    };
                    var textColor = miniPullDown.normal.textColor;
                    textColor.a = 0.9f;
                    miniPullDown.normal.textColor = textColor;
                }

                if (hasName && !displayNames.TryGetValue(item, out displayName))
                {
                    displayName        = name.stringValue.Replace("UnityEngine.", " ");
                    displayNames[item] = displayName;
                }

                tempContent.text  = hasName ? displayName : "None";
                tempContent.image = ScriptIcons.GetIcon(name.stringValue);

                EditorGUIUtility.SetIconSize(new Vector2(16, 16));

                if (GUI.Button(rect, tempContent, miniPullDown))
                {
                    var position = Event.current.mousePosition;
                    position.y = rect.position.y + 35;
                    position   = GUIUtility.GUIToScreenPoint(position);

                    var context = new SearchWindowContext(position);

                    var popup = ScriptableObject.CreateInstance <TypeSearchPopup>();
                    popup.Initialize(componentTypes, type =>
                    {
                        name.stringValue         = type.FullName;
                        assemblyName.stringValue = type.AssemblyQualifiedName;
                        property.serializedObject.ApplyModifiedProperties();
                    });
                    SearchWindow.Open(context, popup);
                }
            };

            List.onChangedCallback += _ => SaveChanges();
            List.onReorderCallback += _ => SaveChanges();

            void SaveChanges()
            {
                property.serializedObject.ApplyModifiedProperties();
                onChange?.Invoke();
            }
        }