Beispiel #1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property.serializedObject.isEditingMultipleObjects ||
                property.objectReferenceValue == null)
            {
                base.OnGUI(position, property, label);
                return;
            }

            var inner     = new SerializedObject(property.objectReferenceValue);
            var valueProp = inner.FindProperty("_value");
            var width     = GetPreviewSpace(valueProp.type) + (property.depth == 0 ? EditorGUIUtility.labelWidth : 0);
            var restRect  = IMGUIUtils.SnipRectH(position, width, out position, 6f);

            base.OnGUI(position, property, GUIContent.none);

            EditorGUI.BeginDisabledGroup(true);
            EditorGUI.PropertyField(restRect, valueProp, label, false);
            EditorGUI.EndDisabledGroup();
        }
Beispiel #2
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property.serializedObject.isEditingMultipleObjects)
            {
                EditorGUI.PropertyField(position, property, label, true);
                return;
            }

            EditorGUI.BeginProperty(position, label, property);

            DrawerData drawerData = GetDrawerData(property.propertyPath);

            var  isCreatingSO = drawerData.UserClickedToCreateAtom && property.objectReferenceValue == null;
            var  restWidth    = drawerData.UserClickedToCreateAtom ? 50 : 58;
            var  gutter       = drawerData.UserClickedToCreateAtom ? 2f : 6f;
            Rect restRect     = new Rect();
            Rect warningRect  = new Rect();

            if (drawerData.WarningText.Length > 0)
            {
                position = IMGUIUtils.SnipRectV(position, EditorGUIUtility.singleLineHeight, out warningRect, 2f);
            }

            if (property.objectReferenceValue == null)
            {
                position = IMGUIUtils.SnipRectH(position, position.width - restWidth, out restRect, gutter);
            }

            var defaultGUIColor = GUI.color;

            GUI.color = isCreatingSO ? Color.yellow : defaultGUIColor;
            position  = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), isCreatingSO && label != GUIContent.none ? new GUIContent("Name of New Atom") : label);
            GUI.color = defaultGUIColor;

            var indent = EditorGUI.indentLevel;

            EditorGUI.indentLevel = 0;

            GUI.SetNextControlName(NAMING_FIELD_CONTROL_NAME);
            drawerData.NameOfNewAtom = EditorGUI.TextField(isCreatingSO ? position : Rect.zero, drawerData.NameOfNewAtom);

            if (!isCreatingSO)
            {
                EditorGUI.BeginChangeCheck();
                var obj = EditorGUI.ObjectField(position, property.objectReferenceValue, typeof(T), false);
                if (EditorGUI.EndChangeCheck())
                {
                    property.objectReferenceValue = obj;
                }
            }

            if (property.objectReferenceValue == null)
            {
                if (isCreatingSO)
                {
                    var  buttonWidth = 24;
                    Rect secondButtonRect;
                    Rect firstButtonRect = IMGUIUtils.SnipRectH(restRect, restRect.width - buttonWidth, out secondButtonRect, gutter);
                    if (GUI.Button(firstButtonRect, "✓") ||
                        (Event.current.keyCode == KeyCode.Return &&
                         GUI.GetNameOfFocusedControl() == NAMING_FIELD_CONTROL_NAME))
                    {
                        if (drawerData.NameOfNewAtom.Length > 0)
                        {
                            try
                            {
                                string path = AssetDatabase.GetAssetPath(property.serializedObject.targetObject);
                                path = path == "" ? "Assets/" : Path.GetDirectoryName(path) + "/";
                                // Create asset
                                T so = ScriptableObject.CreateInstance <T>();
                                AssetDatabase.CreateAsset(so, path + drawerData.NameOfNewAtom + ".asset");
                                AssetDatabase.SaveAssets();
                                // Assign the newly created SO
                                property.objectReferenceValue = so;
                            }
                            catch
                            {
                                Debug.LogError("Not able to create Atom");
                            }

                            drawerData.UserClickedToCreateAtom = false;
                            drawerData.WarningText             = "";
                        }
                        else
                        {
                            drawerData.WarningText = "Name of new Atom must be specified!";
                            EditorGUI.FocusTextInControl(NAMING_FIELD_CONTROL_NAME);
                        }
                    }
                    if (GUI.Button(secondButtonRect, "✗") ||
                        (Event.current.keyCode == KeyCode.Escape &&
                         GUI.GetNameOfFocusedControl() == NAMING_FIELD_CONTROL_NAME))
                    {
                        drawerData.UserClickedToCreateAtom = false;
                        drawerData.WarningText             = "";
                    }

                    if (drawerData.WarningText.Length > 0)
                    {
                        EditorGUI.HelpBox(warningRect, drawerData.WarningText, MessageType.Warning);
                    }
                }
                else
                {
                    if (GUI.Button(restRect, "Create"))
                    {
                        drawerData.NameOfNewAtom           = "";
                        drawerData.UserClickedToCreateAtom = true;

                        EditorGUI.FocusTextInControl(NAMING_FIELD_CONTROL_NAME);
                    }
                }
            }

            EditorGUI.indentLevel = indent;
            EditorGUI.EndProperty();
        }
        private void OnPropertyGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            Rect lower;
            Rect buttonRect = new Rect();
            var  scene      = property.FindPropertyRelative("_sceneAsset")?.objectReferenceValue;

            if (scene == null)
            {
                // Update values cause the build index could've changed
                property.FindPropertyRelative("_sceneName").stringValue = "";
                property.FindPropertyRelative("_scenePath").stringValue = "";
                property.FindPropertyRelative("_buildIndex").intValue   = -1;
            }

            position = IMGUIUtils.SnipRectV(position, EditorGUIUtility.singleLineHeight, out lower, 2f);
            if (HasValidBuildIndex(property))
            {
                if (scene != null)
                {
                    // Update values cause the build index could've changed
                    property.FindPropertyRelative("_sceneName").stringValue = scene.name;
                    property.FindPropertyRelative("_scenePath").stringValue = AssetDatabase.GetAssetPath(scene);
                    property.FindPropertyRelative("_buildIndex").intValue   = SceneUtility.GetBuildIndexByScenePath(
                        property.FindPropertyRelative("_scenePath").stringValue
                        );
                }
            }
            else
            {
                position = IMGUIUtils.SnipRectH(position, position.width - 70, out buttonRect, 6f);
                property.FindPropertyRelative("_buildIndex").intValue = -1;
            }

            SceneAsset sceneAsset = scene as SceneAsset;

            position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

            EditorGUI.BeginChangeCheck();
            scene = EditorGUI.ObjectField(position, scene, typeof(SceneAsset), false);
            if (EditorGUI.EndChangeCheck())
            {
                property.FindPropertyRelative("_sceneAsset").objectReferenceValue = scene;
                sceneAsset = scene as SceneAsset;
                if (sceneAsset != null)
                {
                    property.FindPropertyRelative("_sceneName").stringValue = scene.name;
                    property.FindPropertyRelative("_scenePath").stringValue = AssetDatabase.GetAssetPath(scene);
                    property.FindPropertyRelative("_buildIndex").intValue   = SceneUtility.GetBuildIndexByScenePath(
                        property.FindPropertyRelative("_scenePath").stringValue
                        );
                }
            }

            if (property.FindPropertyRelative("_buildIndex").intValue != -1)
            {
                return;
            }

            if (scene != null && scene is SceneAsset)
            {
                EditorGUI.HelpBox(lower, "Scene is not added in the build settings", MessageType.Warning);
                if (GUI.Button(buttonRect, "Fix Now"))
                {
                    AddSceneToBuildSettings(sceneAsset);
                }
            }
        }
Beispiel #4
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property.serializedObject.isEditingMultipleObjects)
            {
                EditorGUI.PropertyField(position, property, label, true);
                return;
            }

            EditorGUI.BeginProperty(position, label, property);

            DrawerData drawerData = GetDrawerData(property.propertyPath);

            var  isCreatingSO = drawerData.UserClickedToCreateAtom && property.objectReferenceValue == null;
            var  restWidth    = drawerData.UserClickedToCreateAtom ? 50 : 58;
            var  gutter       = drawerData.UserClickedToCreateAtom ? 2f : 6f;
            Rect restRect     = new Rect();
            Rect warningRect  = new Rect();

            if (drawerData.WarningText.Length > 0)
            {
                position = IMGUIUtils.SnipRectV(position, EditorGUIUtility.singleLineHeight, out warningRect, 2f);
            }

            if (property.objectReferenceValue == null)
            {
                position = IMGUIUtils.SnipRectH(position, position.width - restWidth, out restRect, gutter);
            }

            var defaultGUIColor = GUI.color;

            GUI.color = isCreatingSO ? Color.yellow : defaultGUIColor;
            position  = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), isCreatingSO ? new GUIContent("Name of New Atom") : label);
            GUI.color = defaultGUIColor;

            var indent = EditorGUI.indentLevel;

            EditorGUI.indentLevel = 0;

            if (isCreatingSO)
            {
                drawerData.NameOfNewAtom = EditorGUI.TextField(position, drawerData.NameOfNewAtom);
            }
            else
            {
                property.objectReferenceValue = EditorGUI.ObjectField(position, property.objectReferenceValue, typeof(T), false);
            }

            if (property.objectReferenceValue == null)
            {
                if (isCreatingSO)
                {
                    var  buttonWidth = 24;
                    Rect secondButtonRect;
                    Rect firstButtonRect = IMGUIUtils.SnipRectH(restRect, restRect.width - buttonWidth, out secondButtonRect, gutter);
                    if (GUI.Button(firstButtonRect, "✓"))
                    {
                        if (drawerData.NameOfNewAtom.Length > 0)
                        {
                            try
                            {
                                // Create asset
                                T so = ScriptableObject.CreateInstance <T>();
                                AssetDatabase.CreateAsset(so, "Assets/" + drawerData.NameOfNewAtom + ".asset");
                                AssetDatabase.SaveAssets();
                                // Assign the newly created SO
                                property.objectReferenceValue = so;
                            }
                            catch
                            {
                                Debug.LogError("Not able to create Atom");
                            }

                            drawerData.UserClickedToCreateAtom = false;
                            drawerData.WarningText             = "";
                        }
                        else
                        {
                            drawerData.WarningText = "Name of new Atom must be specified!";
                        }
                    }
                    if (GUI.Button(secondButtonRect, "✗"))
                    {
                        drawerData.UserClickedToCreateAtom = false;
                        drawerData.WarningText             = "";
                    }

                    if (drawerData.WarningText.Length > 0)
                    {
                        EditorGUI.HelpBox(warningRect, drawerData.WarningText, MessageType.Warning);
                    }
                }
                else
                {
                    if (GUI.Button(restRect, "Create"))
                    {
                        drawerData.NameOfNewAtom           = "";
                        drawerData.UserClickedToCreateAtom = true;
                    }
                }
            }

            EditorGUI.indentLevel = indent;
            EditorGUI.EndProperty();
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            AtomListAttribute atomListAttr = attribute as AtomListAttribute;

            label = EditorGUI.BeginProperty(position, label, property);
            var proColor   = new Color(83f / 255f, 83f / 255f, 83f / 255f);
            var basicColor = new Color(174f / 255f, 174f / 255f, 174f / 255f);

            EditorGUI.DrawRect(position, EditorGUIUtility.isProSkin ? proColor : basicColor);

            var indent = EditorGUI.indentLevel;

            EditorGUI.indentLevel = 0;
            EditorGUI.BeginChangeCheck();

            var listArrayProperty = property.FindPropertyRelative(atomListAttr != null && !string.IsNullOrWhiteSpace(atomListAttr.ChildPropName) ? atomListAttr.ChildPropName : SERIALIZED_LIST_PROPNAME);

            var restRect        = new Rect();
            var initialPosition = new Rect(position);

            initialPosition.y     = initialPosition.y + DRAWER_MARGIN;
            initialPosition.x     = initialPosition.x + DRAWER_MARGIN;
            initialPosition.width = initialPosition.width - DRAWER_MARGIN * 2f;

            var labelPosition = IMGUIUtils.SnipRectH(initialPosition, initialPosition.width - BUTTON_WIDTH, out restRect);

            labelPosition.height = EditorGUIUtility.singleLineHeight + LINE_BOTTOM_MARGIN;
            EditorGUI.PrefixLabel(initialPosition, new GUIContent(atomListAttr != null ? (atomListAttr.Label ?? label.text) : LIST_LABEL_NAME));

            var addButtonPosition = IMGUIUtils.SnipRectH(restRect, restRect.width, out restRect);

            addButtonPosition.height = EditorGUIUtility.singleLineHeight;
            var insertIndex = -1;

            if (GUI.Button(addButtonPosition, PLUS_ICON))
            {
                insertIndex = listArrayProperty.arraySize;
            }

            var linePosition = new Rect(initialPosition);

            linePosition.height = EditorGUIUtility.singleLineHeight;
            linePosition.y     += EditorGUIUtility.singleLineHeight + LINE_BOTTOM_MARGIN;

            var indexToDelete = -1;
            var length        = listArrayProperty.arraySize;

            for (var i = 0; i < length; ++i)
            {
                var itemProp = listArrayProperty.GetArrayElementAtIndex(i);

                var indexLabelPos = IMGUIUtils.SnipRectH(linePosition, INDEX_LABEL_WIDTH, out restRect, GUTTER);
                EditorGUI.PrefixLabel(indexLabelPos, new GUIContent(i.ToString()));

                var itemPos = IMGUIUtils.SnipRectH(restRect, linePosition.width - BUTTON_WIDTH - INDEX_LABEL_WIDTH - GUTTER * 2, out restRect, GUTTER);
                EditorGUI.PropertyField(itemPos, itemProp, GUIContent.none, atomListAttr != null ? atomListAttr.IncludeChildrenForItems : false);

                var removeButtonPosition = new Rect(restRect);
                removeButtonPosition.height = EditorGUIUtility.singleLineHeight;

                if (GUI.Button(removeButtonPosition, MINUS_ICON))
                {
                    indexToDelete = i;
                }

                linePosition.y += EditorGUI.GetPropertyHeight(itemProp) + LINE_BOTTOM_MARGIN;
            }

            if (insertIndex != -1)
            {
                if (listArrayProperty != null)
                {
                    listArrayProperty.InsertArrayElementAtIndex(insertIndex);
                    var newProp = listArrayProperty.GetArrayElementAtIndex(insertIndex);
                    newProp.isExpanded = true;
                }
            }

            if (indexToDelete != -1)
            {
                if (listArrayProperty != null)
                {
                    listArrayProperty.RemoveArrayElement(indexToDelete);
                }
            }

            if (EditorGUI.EndChangeCheck())
            {
                property.serializedObject.ApplyModifiedProperties();
            }

            EditorGUI.indentLevel = indent;
            EditorGUI.EndProperty();
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            label = EditorGUI.BeginProperty(position, label, property);
            var proColor   = new Color(83f / 255f, 83f / 255f, 83f / 255f);
            var basicColor = new Color(174f / 255f, 174f / 255f, 174f / 255f);

            EditorGUI.DrawRect(position, EditorGUIUtility.isProSkin ? proColor : basicColor);

            var indent = EditorGUI.indentLevel;

            EditorGUI.indentLevel = 0;
            EditorGUI.BeginChangeCheck();

            var keyArrayProperty    = property.FindPropertyRelative(SERIALIZED_KEYS_PROPNAME);
            var valueArrayProperty  = property.FindPropertyRelative(SERIALIZED_VALUES_PROPNAME);
            var duplicateKeyIndices = property.FindPropertyRelative(DUPLICATE_KEY_INDICES_PROPNAME);

            var restRect        = new Rect();
            var initialPosition = new Rect(position);

            initialPosition.y     = initialPosition.y + DRAWER_MARGIN;
            initialPosition.x     = initialPosition.x + DRAWER_MARGIN;
            initialPosition.width = initialPosition.width - DRAWER_MARGIN * 2f;

            var labelPosition = IMGUIUtils.SnipRectH(initialPosition, initialPosition.width - BUTTON_WIDTH, out restRect);

            labelPosition.height = EditorGUIUtility.singleLineHeight + LINE_BOTTOM_MARGIN;
            EditorGUI.PrefixLabel(initialPosition, new GUIContent(COLLECTION_LABEL_NAME));

            var addButtonPosition = IMGUIUtils.SnipRectH(restRect, restRect.width, out restRect);

            addButtonPosition.height = EditorGUIUtility.singleLineHeight;
            var insertIndex = -1;

            if (GUI.Button(addButtonPosition, PLUS_ICON))
            {
                insertIndex = keyArrayProperty.arraySize;
            }

            var linePosition = new Rect(initialPosition);

            linePosition.height = EditorGUIUtility.singleLineHeight;
            linePosition.y     += EditorGUIUtility.singleLineHeight + LINE_BOTTOM_MARGIN;

            var indexToDelete    = -1;
            var invalidKeyExists = false;

            foreach (var entry in EnumerateEntries(keyArrayProperty, valueArrayProperty))
            {
                var keyProperty   = entry.keyProperty;
                var valueProperty = entry.valueProperty;
                var currentIndex  = entry.index;
                var isKeyValid    = !duplicateKeyIndices.ArrayContainsInt(currentIndex);

                var keyPosition = IMGUIUtils.SnipRectH(linePosition, (linePosition.width - BUTTON_WIDTH - GUTTER * 2) / 2, out restRect, GUTTER);
                if (!isKeyValid)
                {
                    invalidKeyExists = true;
                    EditorGUI.DrawRect(keyPosition, Color.red);
                }
                EditorGUI.PropertyField(keyPosition, keyProperty, GUIContent.none, false);

                EditorGUI.BeginDisabledGroup(!isKeyValid);
                var valuePosition = IMGUIUtils.SnipRectH(restRect, (linePosition.width - BUTTON_WIDTH - GUTTER * 2) / 2, out restRect, GUTTER);
                EditorGUI.PropertyField(valuePosition, valueProperty, GUIContent.none, false);
                EditorGUI.EndDisabledGroup();

                var removeButtonPosition = new Rect(restRect);
                removeButtonPosition.height = EditorGUIUtility.singleLineHeight;

                if (GUI.Button(removeButtonPosition, MINUS_ICON))
                {
                    indexToDelete = currentIndex;
                }

                linePosition.y += EditorGUIUtility.singleLineHeight + LINE_BOTTOM_MARGIN;
            }

            var drawerData = GetDrawerData(property.propertyPath);

            drawerData.InvalidKeyExists = invalidKeyExists;
            if (invalidKeyExists)
            {
                linePosition.height = GetHelpboxHeight();
                EditorGUI.HelpBox(linePosition, WARNING_TEXT, MessageType.Error);
            }

            if (insertIndex != -1)
            {
                if (keyArrayProperty != null)
                {
                    keyArrayProperty.InsertArrayElementAtIndex(insertIndex);
                }

                if (valueArrayProperty != null)
                {
                    valueArrayProperty.InsertArrayElementAtIndex(insertIndex);
                }
            }

            if (indexToDelete != -1)
            {
                if (keyArrayProperty != null)
                {
                    keyArrayProperty.RemoveArrayElement(indexToDelete);
                }

                if (valueArrayProperty != null)
                {
                    valueArrayProperty.RemoveArrayElement(indexToDelete);
                }
            }

            if (EditorGUI.EndChangeCheck())
            {
                property.serializedObject.ApplyModifiedProperties();
            }

            EditorGUI.indentLevel = indent;
            EditorGUI.EndProperty();
        }