Esempio n. 1
0
        private void DrawFolderIconsStrip(Rect rect, Texture largeIcon, Texture smallIcon)
        {
            //adjust rect for each icon
            var largeFolderIconRect = new Rect(rect.x,
                                               rect.yMax - Style.largeFolderHeight,
                                               Style.largeFolderWidth,
                                               Style.largeFolderHeight);
            var smallFolderIconRect = new Rect(rect.x + Style.largeFolderWidth,
                                               //adjust small rect to the large one
                                               largeFolderIconRect.y + Style.smallFolderHeight / 2 - Style.spacing,
                                               Style.smallFolderWidth,
                                               Style.smallFolderHeight);

            GUI.DrawTexture(largeFolderIconRect, Style.foldersIcon, ScaleMode.ScaleToFit);
            GUI.DrawTexture(smallFolderIconRect, Style.foldersIcon, ScaleMode.ScaleToFit);

            if (largeIcon)
            {
                GUI.DrawTexture(ToolboxEditorProject.GetLargeIconRect(largeFolderIconRect), largeIcon, ScaleMode.ScaleToFit);
            }

            if (smallIcon)
            {
                GUI.DrawTexture(ToolboxEditorProject.GetSmallIconRect(smallFolderIconRect), smallIcon, ScaleMode.ScaleToFit);
            }
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var typeProperty = property.FindPropertyRelative("type");
            var nameProperty = property.FindPropertyRelative("name");
            var pathProperty = property.FindPropertyRelative("path");
            var toolProperty = property.FindPropertyRelative("tooltip");

            var largeIconProperty = property.FindPropertyRelative("largeIcon");
            var smallIconProperty = property.FindPropertyRelative("smallIcon");

            var isPathBased  = typeProperty.intValue == 0;
            var propertyName = isPathBased
                ? pathProperty.stringValue
                : nameProperty.stringValue;

            //begin property
            label      = EditorGUI.BeginProperty(position, label, property);
            label.text = string.IsNullOrEmpty(propertyName) ? label.text : propertyName;

            var propertyPosition = new Rect(position.x, position.y, position.width, Style.height);

            if (!(property.isExpanded = EditorGUI.Foldout(propertyPosition, property.isExpanded, label, true, Style.folderLabelStyle)))
            {
                EditorGUI.EndProperty();

                //adjust position for small icon and draw it in label field
                var typeIconRect = new Rect(position.xMax - Style.smallFolderWidth,
#if UNITY_2019_3_OR_NEWER
                                            position.yMin, Style.smallFolderWidth, Style.smallFolderHeight);
#else
                                            position.yMin - Style.spacing, Style.smallFolderWidth, Style.smallFolderHeight);
#endif
                if (isPathBased)
                {
                    //NOTE: if path property is bigger than typical row height it means that associated value is invalid
                    var isValid = EditorGUI.GetPropertyHeight(pathProperty) <= Style.height;
                    if (isValid)
                    {
                        DrawFolderByPathIcon(typeIconRect);
                    }
                    else
                    {
                        DrawLabelWarningIcon(typeIconRect);
                    }
                }
                else
                {
                    DrawFolderByNameIcon(typeIconRect);
                }

                return;
            }

            EditorGUI.indentLevel++;

            var rawPropertyHeight  = propertyPosition.height + Style.spacing;
            var summaryFieldHeight = rawPropertyHeight;

            propertyPosition.y += rawPropertyHeight;
            summaryFieldHeight += rawPropertyHeight;
            //draw folder data type property
            EditorGUI.PropertyField(propertyPosition, typeProperty, false);
            propertyPosition.y += rawPropertyHeight;
            summaryFieldHeight += rawPropertyHeight;

            //decide what property should be drawn depending on folder data type
            if (isPathBased)
            {
                propertyPosition.height = EditorGUI.GetPropertyHeight(pathProperty);
                EditorGUI.PropertyField(propertyPosition, pathProperty, false);
                propertyPosition.y     += propertyPosition.height + Style.spacing;
                summaryFieldHeight     += propertyPosition.height + Style.spacing;
                propertyPosition.height = Style.height;
            }
            else
            {
                EditorGUI.PropertyField(propertyPosition, nameProperty, false);
                propertyPosition.y += rawPropertyHeight;
                summaryFieldHeight += rawPropertyHeight;
            }

            //draw tooltip property
            EditorGUI.PropertyField(propertyPosition, toolProperty, false);
            propertyPosition.y += rawPropertyHeight;
            summaryFieldHeight += rawPropertyHeight;

            //adjust rect for folder icons + button strip
            propertyPosition.y    += Style.spacing;
            propertyPosition.x    += Style.padding;
            propertyPosition.width = Style.largeFolderWidth;

            var selectorHash = GetSelectorHash(property);

            //draw normal icon property picker
            if (GUI.Button(propertyPosition, Style.largeIconPickerContent, Style.largeIconPickerStyle))
            {
                EditorGUIUtility.ShowObjectPicker <Texture>(largeIconProperty.objectReferenceValue, false, null, selectorHash + largeIconPickedId);
            }

            propertyPosition.xMin = propertyPosition.xMax;
            propertyPosition.xMax = propertyPosition.xMin + Style.smallFolderWidth;

            //draw small icon property picker
            if (GUI.Button(propertyPosition, Style.smallIconPickerContent, Style.smallIconPickerStyle))
            {
                EditorGUIUtility.ShowObjectPicker <Texture>(smallIconProperty.objectReferenceValue, false, null, selectorHash + smallIconPickedId);
            }

            //catch object selection event and assign it to proper property
            if (Event.current.commandName == selectorEventName)
            {
                //get proper action id by removing unique property hash code
                var rawPickId = EditorGUIUtility.GetObjectPickerControlID();
                var controlId = rawPickId - selectorHash;

                //determine the target property using predefined values
                SerializedProperty iconProperty = null;
                switch (controlId)
                {
                case largeIconPickedId:
                    iconProperty = largeIconProperty;
                    break;

                case smallIconPickedId:
                    iconProperty = smallIconProperty;
                    break;
                }

                //update the previously picked property
                if (iconProperty != null)
                {
                    iconProperty.serializedObject.Update();
                    iconProperty.objectReferenceValue = EditorGUIUtility.GetObjectPickerObject();
                    iconProperty.serializedObject.ApplyModifiedProperties();
                    //force GUI to changed state
                    GUI.changed = true;
                }
            }

            position.x += Style.padding;

            //adjust rect for each icon
            var largeFolderIconRect = new Rect(position.x,
                                               position.yMin + summaryFieldHeight,
                                               Style.largeFolderWidth,
                                               Style.largeFolderHeight);
            var smallFolderIconRect = new Rect(position.x + Style.largeFolderWidth,
                                               //adjust small rect to the large one
                                               largeFolderIconRect.y + Style.smallFolderHeight / 2 - Style.spacing,
                                               Style.smallFolderWidth,
                                               Style.smallFolderHeight);

            //draw default folder icons
            GUI.DrawTexture(largeFolderIconRect, Style.folderIcon, ScaleMode.ScaleToFit);
            GUI.DrawTexture(smallFolderIconRect, Style.folderIcon, ScaleMode.ScaleToFit);

            if (largeIconProperty.objectReferenceValue)
            {
                var previewTexture = largeIconProperty.objectReferenceValue as Texture;
                GUI.DrawTexture(ToolboxEditorProject.GetLargeIconRect(largeFolderIconRect), previewTexture, ScaleMode.ScaleToFit);
            }

            if (smallIconProperty.objectReferenceValue)
            {
                var previewTexture = smallIconProperty.objectReferenceValue as Texture;
                GUI.DrawTexture(ToolboxEditorProject.GetSmallIconRect(smallFolderIconRect), previewTexture, ScaleMode.ScaleToFit);
            }

            //end property and fix indent level
            EditorGUI.indentLevel--;
            EditorGUI.EndProperty();
        }
Esempio n. 3
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var typeProperty = property.FindPropertyRelative("type");
            var nameProperty = property.FindPropertyRelative("name");
            var pathProperty = property.FindPropertyRelative("path");
            var toolProperty = property.FindPropertyRelative("tooltip");

            var largeIconProperty = property.FindPropertyRelative("icon");
            var smallIconProperty = property.FindPropertyRelative("smallIcon");

            var isPathBased  = typeProperty.intValue == 0;
            var propertyName = isPathBased
                ? pathProperty.stringValue
                : nameProperty.stringValue;

            //begin property
            label      = EditorGUI.BeginProperty(position, label, property);
            label.text = string.IsNullOrEmpty(propertyName) ? label.text : propertyName;

            var propertyPosition = new Rect(position.x, position.y, position.width, Style.height);

            if (!(property.isExpanded = EditorGUI.Foldout(propertyPosition, property.isExpanded, label, true, Style.folderLabelStyle)))
            {
                EditorGUI.EndProperty();

                //adjust position for small icon and draw it in label field
                var typeIconRect = new Rect(position.xMax - Style.smallFolderWidth,
#if UNITY_2019_3_OR_NEWER
                                            position.yMin, Style.smallFolderWidth, Style.smallFolderHeight);
#else
                                            position.yMin - Style.spacing, Style.smallFolderWidth, Style.smallFolderHeight);
#endif
                if (isPathBased)
                {
                    DrawFolderByPathIcon(typeIconRect);
                }
                else
                {
                    DrawFolderByNameIcon(typeIconRect);
                }

                return;
            }

            EditorGUI.indentLevel++;

            var rawPropertyHeight = propertyPosition.height + Style.spacing;
            var sumPropertyHeight = rawPropertyHeight;

            propertyPosition.y += rawPropertyHeight;
            sumPropertyHeight  += rawPropertyHeight;
            //draw folder data type property
            EditorGUI.PropertyField(propertyPosition, typeProperty, false);
            propertyPosition.y += rawPropertyHeight;
            sumPropertyHeight  += rawPropertyHeight;

            //decide what property should be drawn depending on folder data type
            if (isPathBased)
            {
                propertyPosition.height = EditorGUI.GetPropertyHeight(pathProperty);
                EditorGUI.PropertyField(propertyPosition, pathProperty, false);
                propertyPosition.y     += propertyPosition.height + Style.spacing;
                sumPropertyHeight      += propertyPosition.height + Style.spacing;
                propertyPosition.height = Style.height;
            }
            else
            {
                EditorGUI.PropertyField(propertyPosition, nameProperty, false);
                propertyPosition.y += rawPropertyHeight;
                sumPropertyHeight  += rawPropertyHeight;
            }

            //draw tooltip property
            EditorGUI.PropertyField(propertyPosition, toolProperty, false);
            propertyPosition.y += rawPropertyHeight;
            sumPropertyHeight  += rawPropertyHeight;

            //adjust rect for folder icons + button strip
            propertyPosition.y    += Style.spacing;
            propertyPosition.x    += Style.iconsPadding;
            propertyPosition.width = Style.largeFolderWidth;

            //draw normal icon property picker
            if (GUI.Button(propertyPosition, Style.largeIconPickerContent, Style.largeIconPickerStyle))
            {
                var propertyHash = property.GetPropertyKey().GetHashCode();
                EditorGUIUtility.ShowObjectPicker <Texture>(largeIconProperty.objectReferenceValue, false, null, propertyHash + largeIconPickedId);
            }

            propertyPosition.x    += Style.largeFolderWidth;
            propertyPosition.width = Style.smallFolderWidth;

            //draw small icon property picker
            if (GUI.Button(propertyPosition, Style.smallIconPickerContent, Style.smallIconPickerStyle))
            {
                var propertyHash = property.GetPropertyKey().GetHashCode();
                EditorGUIUtility.ShowObjectPicker <Texture>(smallIconProperty.objectReferenceValue, false, null, propertyHash + smallIconPickedId);
            }

            //catch object selection event and assign it to proper property
            if (Event.current.commandName == selectorEventName)
            {
                //get proper action id by removing unique property hash code
                var controlId = EditorGUIUtility.GetObjectPickerControlID() - property.GetPropertyKey().GetHashCode();
                switch (controlId)
                {
                case largeIconPickedId:
                    largeIconProperty.objectReferenceValue = EditorGUIUtility.GetObjectPickerObject();
                    break;

                case smallIconPickedId:
                    smallIconProperty.objectReferenceValue = EditorGUIUtility.GetObjectPickerObject();
                    break;
                }
            }

            position.x += Style.iconsPadding;

            var largeFolderIconRect = new Rect(position.x, position.yMin + sumPropertyHeight, Style.largeFolderWidth, Style.largeFolderHeight);
            var smallFolderIconRect = new Rect(position.x + Style.largeFolderWidth,
                                               largeFolderIconRect.y + Style.smallFolderHeight / 2 - Style.spacing, Style.smallFolderWidth, Style.smallFolderHeight);

            GUI.DrawTexture(largeFolderIconRect, Style.folderTexture, ScaleMode.ScaleToFit);
            GUI.DrawTexture(smallFolderIconRect, Style.folderTexture, ScaleMode.ScaleToFit);

            if (largeIconProperty.objectReferenceValue)
            {
                var previewTexture = largeIconProperty.objectReferenceValue as Texture;
                GUI.DrawTexture(ToolboxEditorProject.GetLargeIconRect(largeFolderIconRect), previewTexture, ScaleMode.ScaleToFit);
            }

            if (smallIconProperty.objectReferenceValue)
            {
                var previewTexture = smallIconProperty.objectReferenceValue as Texture;
                GUI.DrawTexture(ToolboxEditorProject.GetSmallIconRect(smallFolderIconRect), previewTexture, ScaleMode.ScaleToFit);
            }

            EditorGUI.indentLevel--;
            EditorGUI.EndProperty();
        }