private void DrawElements(Element[] elements)
        {
            foreach (Element element in elements)
            {
                if (element.onGUI != null && !isSearching)
                {
                    element.onGUI();
                    continue;
                }


                if (!SearchMatch(element))
                {
                    continue;
                }

                Color backgroundColor = GUI.backgroundColor;
                Color textColor       = AddAssetWindow.m_Styles.elementButton.normal.textColor;
                int   padding         = AddAssetWindow.m_Styles.elementButton.padding.left;
                Rect  rect            = GUILayoutUtility.GetRect(element.label, AddAssetWindow.m_Styles.elementButton, GUILayout.Height(20f));
                GUI.backgroundColor = (rect.Contains(Event.current.mousePosition) ? GUI.backgroundColor : new Color(0, 0, 0, 0.0f));
                AddAssetWindow.m_Styles.elementButton.normal.textColor = (rect.Contains(Event.current.mousePosition) ? Color.white : textColor);
                Texture2D icon = null;//(Texture2D)EditorGUIUtility.ObjectContent(null, element.type).image;

                if (element.type != null)
                {
                    icon = EditorGUIUtility.FindTexture("cs Script Icon");
                    IconAttribute iconAttribute = element.type.GetAttribute <IconAttribute>();
                    if (iconAttribute != null)
                    {
                        if (iconAttribute.type != null)
                        {
                            icon = AssetPreview.GetMiniTypeThumbnail(iconAttribute.type);
                        }
                        else
                        {
                            icon = Resources.Load <Texture2D>(iconAttribute.path);
                        }
                    }
                }
                AddAssetWindow.m_Styles.elementButton.padding.left = (icon != null? 22 : padding);


                if (GUI.Button(rect, element.label, AddAssetWindow.m_Styles.elementButton))
                {
                    if (element.children.Count == 0)
                    {
                        if (onAddCallback != null)
                        {
                            onAddCallback(element.type);
                        }
                        Close();
                    }
                    else
                    {
                        this.m_SelectedElement = element;
                    }
                }
                GUI.backgroundColor = backgroundColor;
                AddAssetWindow.m_Styles.elementButton.normal.textColor = textColor;
                AddAssetWindow.m_Styles.elementButton.padding.left     = padding;

                if (icon != null)
                {
                    GUI.Label(new Rect(rect.x, rect.y, 20f, 20f), icon);
                }
                if (element.children.Count > 0)
                {
                    GUI.Label(new Rect(rect.x + rect.width - 16f, rect.y + 2f, 16f, 16f), "", AddAssetWindow.m_Styles.rightArrow);
                }
            }
        }
        public static bool Titlebar(UnityEngine.Object target, GenericMenu menu)
        {
            int        controlID      = EditorGUIUtility.GetControlID(FocusType.Passive);
            GUIContent content        = new GUIContent(ObjectNames.NicifyVariableName(target.GetType().Name));
            GUIStyle   inspectorTitle = new GUIStyle("IN Foldout")
            {
                overflow    = new RectOffset(0, 0, -3, 0),
                fixedWidth  = 0,
                fixedHeight = 20
            };
            GUIStyle inspectorTitleText = "IN TitleText";

            Rect position = GUILayoutUtility.GetRect(GUIContent.none, inspectorTitle);
            Rect rect     = new Rect(position.x + (float)inspectorTitle.padding.left, position.y + (float)inspectorTitle.padding.top, 16f, 16f);
            Rect rect1    = new Rect(position.xMax - (float)inspectorTitle.padding.right - 2f - 16f, rect.y, 16f, 16f);
            Rect rect4    = rect1;

            rect4.x = rect4.x - 18f;

            Rect rect2 = new Rect(position.x + 2f + 2f + 16f * 3, rect.y, 100f, rect.height)
            {
                xMax = rect4.xMin - 2f
            };

            Rect          rect3         = new Rect(position.x + 16f, rect.y, 20f, 20f);
            Texture2D     icon          = EditorGUIUtility.FindTexture("cs Script Icon");
            IconAttribute iconAttribute = target.GetType().GetAttribute <IconAttribute>();

            if (iconAttribute != null)
            {
                if (iconAttribute.type != null)
                {
                    icon = AssetPreview.GetMiniTypeThumbnail(iconAttribute.type);
                }
                else
                {
                    icon = Resources.Load <Texture2D>(iconAttribute.path);
                }
            }

            GUI.Label(new Rect(position.x + 13f, rect.y, 18f, 18f), icon);// EditorGUIUtility.FindTexture("cs Script Icon"));
            Rect rect5 = rect3;

            rect5.x = rect5.x + 16f;
            if (typeof(MonoBehaviour).IsAssignableFrom(target.GetType()))
            {
                MonoBehaviour behaviour = target as MonoBehaviour;
                behaviour.enabled = GUI.Toggle(rect5, behaviour.enabled, GUIContent.none);
            }
            else
            {
                FieldInfo enableField = target.GetType().GetField("enabled", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                if (enableField != null)
                {
                    bool isEnabled = GUI.Toggle(rect5, (bool)enableField.GetValue(target), GUIContent.none);
                    enableField.SetValue(target, isEnabled);
                }
            }



            if (menu != null && GUI.Button(rect1, EditorGUIUtility.FindTexture("_popup"), inspectorTitleText))
            {
                menu.ShowAsContext();
            }

            EventType eventType = Event.current.type;

            if (menu != null && eventType == EventType.MouseDown && Event.current.button == 1 && position.Contains(Event.current.mousePosition))
            {
                menu.ShowAsContext();
            }

            bool isFolded = EditorPrefs.GetBool("Instance ID" + target.GetInstanceID(), true);

            if (eventType != EventType.MouseDown)
            {
                if (eventType == EventType.Repaint)
                {
                    inspectorTitle.Draw(position, GUIContent.none, controlID, isFolded);

                    LineStyle.Draw(position, GUIContent.none, controlID);
                    inspectorTitleText.Draw(rect2, content, controlID, isFolded);
                }
            }

            bool flag = DoToggleForward(position, controlID, isFolded, GUIContent.none, GUIStyle.none);

            if (flag != isFolded)
            {
                EditorPrefs.SetBool("Instance ID" + target.GetInstanceID(), flag);
            }

            return(flag);
        }
Example #3
0
        private void DrawSelectableObjects()
        {
            List <UnityEngine.Object> selectableObjects = this.m_Root == null?this.m_SelectableObjects.Keys.ToList() : this.m_SelectableObjects[this.m_Root];

            this.m_ScrollPosition = EditorGUILayout.BeginScrollView(this.m_ScrollPosition);
            foreach (UnityEngine.Object obj in selectableObjects)
            {
                if (!SearchMatch(obj))
                {
                    continue;
                }

                Color      backgroundColor = GUI.backgroundColor;
                Color      textColor       = ObjectPickerWindow.m_Styles.elementButton.normal.textColor;
                int        padding         = ObjectPickerWindow.m_Styles.elementButton.padding.left;
                GUIContent label           = new GUIContent(obj.name);
                Rect       rect            = GUILayoutUtility.GetRect(label, ObjectPickerWindow.m_Styles.elementButton, GUILayout.Height(20f));
                GUI.backgroundColor = (rect.Contains(Event.current.mousePosition) ? GUI.backgroundColor : new Color(0, 0, 0, 0.0f));
                ObjectPickerWindow.m_Styles.elementButton.normal.textColor = (rect.Contains(Event.current.mousePosition) ? Color.white : textColor);

                Texture2D     icon          = EditorGUIUtility.LoadRequired("d_ScriptableObject Icon") as Texture2D;
                IconAttribute iconAttribute = obj.GetType().GetCustomAttribute <IconAttribute>();
                if (iconAttribute != null)
                {
                    if (iconAttribute.type != null)
                    {
                        icon = AssetPreview.GetMiniTypeThumbnail(iconAttribute.type);
                    }
                    else
                    {
                        icon = Resources.Load <Texture2D>(iconAttribute.path);
                    }
                }

                ObjectPickerWindow.m_Styles.elementButton.padding.left = (icon != null ? 22 : padding);


                if (GUI.Button(rect, label, ObjectPickerWindow.m_Styles.elementButton))
                {
                    if (this.m_Root != null && this.m_SelectableObjects[this.m_Root].Count > 0)
                    {
                        onSelectCallback?.Invoke(obj);
                        Close();
                    }
                    this.m_Root = obj;
                    if (!this.m_SelectChildren)
                    {
                        onSelectCallback?.Invoke(this.m_Root);
                        Close();
                    }
                }
                GUI.backgroundColor = backgroundColor;
                ObjectPickerWindow.m_Styles.elementButton.normal.textColor = textColor;
                ObjectPickerWindow.m_Styles.elementButton.padding.left     = padding;

                if (icon != null)
                {
                    GUI.Label(new Rect(rect.x, rect.y, 20f, 20f), icon);
                }
            }

            if (this.m_Root == null)
            {
                if (this.m_AcceptNull)
                {
                    GUIContent nullContent = new GUIContent("Null");
                    Rect       rect2       = GUILayoutUtility.GetRect(nullContent, ObjectPickerWindow.m_Styles.elementButton, GUILayout.Height(20f));
                    GUI.backgroundColor = (rect2.Contains(Event.current.mousePosition) ? GUI.backgroundColor : new Color(0, 0, 0, 0.0f));

                    if (GUI.Button(rect2, nullContent, ObjectPickerWindow.m_Styles.elementButton))
                    {
                        onSelectCallback?.Invoke(null);
                        Close();
                    }
                    GUI.Label(new Rect(rect2.x, rect2.y, 20f, 20f), EditorGUIUtility.LoadRequired("d_ScriptableObject On Icon") as Texture2D);
                }

                GUIContent createContent = new GUIContent("Create New " + this.m_Type.Name);
                Rect       rect1         = GUILayoutUtility.GetRect(createContent, ObjectPickerWindow.m_Styles.elementButton, GUILayout.Height(20f));
                GUI.backgroundColor = (rect1.Contains(Event.current.mousePosition) ? GUI.backgroundColor : new Color(0, 0, 0, 0.0f));

                if (GUI.Button(rect1, createContent, ObjectPickerWindow.m_Styles.elementButton))
                {
                    onCreateCallback?.Invoke();
                    Close();
                }
                GUI.Label(new Rect(rect1.x, rect1.y, 20f, 20f), EditorGUIUtility.LoadRequired("d_ScriptableObject On Icon") as Texture2D);
            }
            EditorGUILayout.EndScrollView();
        }