private void DrawDropDown(Rect position, SerializedProperty property)
        {
            using (EditorGUI.ChangeCheckScope changedCheck = new EditorGUI.ChangeCheckScope())
            {
                int selectedIndex = 0;

                if (collectableItem != null)
                {
                    selectedIndex = Array.IndexOf(options, collectableItem) + 1;
                }


                int newSelectedIndex = EditorGUI.Popup(position, selectedIndex,
                                                       GUIContents, EditorStyles.popup);

                newSelectedIndex -= 1;

                collectableItem = newSelectedIndex >= 0 ? options[newSelectedIndex] : null;
                if (changedCheck.changed)
                {
                    property.objectReferenceValue = collectableItem;
                    property.serializedObject.ApplyModifiedProperties();
                }
            }
        }
Exemplo n.º 2
0
        public static AssetDeleteResult OnWillDeleteAsset(string targetAssetPath, RemoveAssetOptions removeAssetOptions)
        {
            Object mainAssetAtPath = AssetDatabase.LoadMainAssetAtPath(targetAssetPath);

            if (mainAssetAtPath == null)
            {
                return(AssetDeleteResult.DidNotDelete);
            }

            Type type = mainAssetAtPath.GetType();

            if (type.IsSubclassOf(typeof(CollectableScriptableObject)))
            {
                CollectableScriptableObject collectable =
                    AssetDatabase.LoadAssetAtPath <CollectableScriptableObject>(targetAssetPath);

                collectable.Collection.Remove(collectable);
                return(AssetDeleteResult.DidNotDelete);
            }

            if (type.IsSubclassOf(typeof(ScriptableObjectCollection)))
            {
                ScriptableObjectCollection collection =
                    AssetDatabase.LoadAssetAtPath <ScriptableObjectCollection>(targetAssetPath);

                CollectionsRegistry.Instance.DeleteCollection(collection);
                return(AssetDeleteResult.DidNotDelete);
            }

            return(AssetDeleteResult.DidNotDelete);
        }
 private void DrawSelectItem(CollectableScriptableObject collectionItem)
 {
     if (GUILayout.Button(CollectionEditorGUI.ARROW_RIGHT_CHAR, EditorStyles.miniButton, GUILayout.Width(30),
                          CollectionEditorGUI.DEFAULT_HEIGHT))
     {
         Selection.activeObject = collectionItem;
     }
 }
        public static bool CanPasteToTarget(CollectableScriptableObject target)
        {
            if (source == null)
            {
                return(false);
            }

            return(target.GetType() == source.GetType());
        }
        private void DrawItem(int index)
        {
            CollectableScriptableObject collectionItem = filteredItemList[index];

            using (new EditorGUILayout.VerticalScope("Box"))
            {
                using (new EditorGUILayout.HorizontalScope())
                {
                    CollectionUtility.SetFoldoutOpen(collectionItem,
                                                     EditorGUILayout.Toggle(GUIContent.none, CollectionUtility.IsFoldoutOpen(collectionItem), EditorStyles.foldout,
                                                                            GUILayout.Width(13)));

                    using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
                    {
                        string newName = EditorGUILayout.DelayedTextField(collectionItem.name, CollectionEditorGUI.ItemNameStyle,
                                                                          GUILayout.ExpandWidth(true));

                        if (changeCheck.changed)
                        {
                            AssetDatabaseUtils.RenameAsset(collectionItem, newName);
                        }
                    }

                    DrawSelectItem(collectionItem);
                    DrawMoveItemDownButton(collectionItem);
                    DrawMoveItemUpButton(collectionItem);
                    DrawDeleteButton(collectionItem);
                }

                if (CollectionUtility.IsFoldoutOpen(collectionItem))
                {
                    EditorGUI.indentLevel++;
                    Editor editor = CollectionUtility.GetEditorForItem(collectionItem);
                    using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
                    {
                        GUILayout.Space(10);
                        editor.OnInspectorGUI();
                        EditorGUILayout.Space();

                        if (changeCheck.changed)
                        {
                            if (index > filteredSerializedList.Count - 1 || filteredSerializedList[index] == null)
                            {
                                filteredItemListDirty = true;
                            }
                            else
                            {
                                filteredSerializedList[index].ApplyModifiedProperties();
                            }
                        }
                    }
                    EditorGUI.indentLevel--;
                }
            }
        }
        public static void ApplySourceToStart(CollectableScriptableObject target)
        {
            if (source == null)
            {
                return;
            }

            Undo.RecordObject(target, "Paste Changes");
            EditorUtility.CopySerializedManagedFieldsOnly(source, target);
            EditorUtility.SetDirty(target);
        }
        public static Editor GetEditorForItem(CollectableScriptableObject collectionItem)
        {
            if (itemToEditor.TryGetValue(collectionItem, out Editor customEditor))
            {
                return(customEditor);
            }

            Editor.CreateCachedEditor(collectionItem, null, ref customEditor);
            itemToEditor.Add(collectionItem, customEditor);
            return(customEditor);
        }
Exemplo n.º 8
0
        private AdvancedDropdownItem GetOrCreateDropdownItemForType(AdvancedDropdownItem root,
                                                                    CollectableScriptableObject collectionItem)
        {
            AdvancedDropdownItem item = root.children.FirstOrDefault(dropdownItem =>
                                                                     dropdownItem.name.Equals(collectionItem.GetType().Name, StringComparison.Ordinal));

            if (item == null)
            {
                item = new AdvancedDropdownItem(collectionItem.GetType().Name);
                root.AddChild(item);
            }

            return(item);
        }
        private void DrawMoveItemUpButton(CollectableScriptableObject collectable)
        {
            int index = collection.IndexOf(collectable);

            EditorGUI.BeginDisabledGroup(index <= 0);
            {
                if (GUILayout.Button(CollectionEditorGUI.ARROW_UP_CHAR, EditorStyles.miniButtonRight, GUILayout.Width(30), CollectionEditorGUI.DEFAULT_HEIGHT))
                {
                    collection.Swap(index, index - 1);
                    filteredItemListDirty = true;
                    return;
                }
                EditorGUI.EndDisabledGroup();
            }
        }
        private void DrawDeleteButton(CollectableScriptableObject item)
        {
            Color previousColor = GUI.backgroundColor;

            GUI.backgroundColor = CollectionEditorGUI.DELETE_BUTTON_COLOR;
            if (GUILayout.Button(CollectionEditorGUI.X_CHAR, EditorStyles.miniButton, GUILayout.Width(30),
                                 CollectionEditorGUI.DEFAULT_HEIGHT))
            {
                int index = collection.IndexOf(item);
                collection.RemoveAt(index);
                AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(item));
                ObjectUtility.SetDirty(collection);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
                filteredItemListDirty = true;
            }

            GUI.backgroundColor = previousColor;
        }
Exemplo n.º 11
0
        protected override AdvancedDropdownItem BuildRoot()
        {
            Type collectableType      = collection.GetCollectionType();
            AdvancedDropdownItem root = new AdvancedDropdownItem(collectableType.Name);

            root.AddChild(new AdvancedDropdownItem("None"));
            for (int i = 0; i < collection.Count; i++)
            {
                CollectableScriptableObject collectionItem = collection[i];
                if (collectionItem.GetType() == collectableType)
                {
                    root.AddChild(new CollectableDropdownItem(collectionItem));
                }
                else
                {
                    AdvancedDropdownItem parent = GetOrCreateDropdownItemForType(root, collectionItem);
                    parent.AddChild(new CollectableDropdownItem(collectionItem));
                }
            }
            return(root);
        }
 public static void SetSource(CollectableScriptableObject targetSource)
 {
     source = targetSource;
 }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            Initialize(property);

            collectableItem = property.objectReferenceValue as CollectableScriptableObject;

            if (optionsAttribute.DrawType == DrawType.AsReference)
            {
                EditorGUI.PropertyField(position, property, label, true);
                return;
            }


            Rect popupRect = position;

            popupRect.height = 15;

            if (collectableItem != null)
            {
                DrawEditFoldoutButton(ref popupRect);
                DrawGotoButton(collection, ref popupRect);
            }

            using (new EditorGUI.PropertyScope(position, label, property))
            {
                popupRect = EditorGUI.PrefixLabel(popupRect, label);

                int indent = EditorGUI.indentLevel;
                EditorGUI.indentLevel = 0;

                bool showMixedValue = EditorGUI.showMixedValue;


                switch (optionsAttribute.DrawType)
                {
                case DrawType.Dropdown:
                {
                    EditorGUI.showMixedValue = property.hasMultipleDifferentValues;

                    DrawDropDown(popupRect, property);

                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException("DrawType: " + optionsAttribute.DrawType);
                }

                if (collectableItem != null)
                {
                    if (CollectionUtility.IsFoldoutOpen(foldoutObject))
                    {
                        EditorGUI.indentLevel++;
                        using (new EditorGUILayout.VerticalScope("Box"))
                        {
                            Editor editor = CollectionUtility.GetEditorForItem(collectableItem);
                            using (EditorGUI.ChangeCheckScope changedCheck = new EditorGUI.ChangeCheckScope())
                            {
                                GUILayout.Space(10);
                                using (new EditorGUILayout.VerticalScope())
                                {
                                    editor.OnInspectorGUI();
                                }

                                EditorGUILayout.Space();

                                if (changedCheck.changed)
                                {
                                    property.serializedObject.ApplyModifiedProperties();
                                }
                            }
                        }
                        EditorGUI.indentLevel--;
                    }
                }

                EditorGUI.showMixedValue = showMixedValue;

                EditorGUI.indentLevel = indent;
            }
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            objectAssetProperty     = property.FindPropertyRelative(OBJECT_ASSET_PROPERTY_PATH);
            collectableGUIDProperty = property.FindPropertyRelative(COLLECTABLE_GUID_PROPERTY_PATH);
            collectionGUIDProperty  = property.FindPropertyRelative(COLLECTION_GUID_PROPERTY_PATh);

            if (objectAssetProperty.objectReferenceValue != null)
            {
                if (string.IsNullOrEmpty(collectableGUIDProperty.stringValue) ||
                    string.IsNullOrEmpty(collectionGUIDProperty.stringValue))
                {
                    CollectableScriptableObject collectable = objectAssetProperty.objectReferenceValue as CollectableScriptableObject;
                    collectableGUIDProperty.stringValue = collectable.GUID;
                    collectionGUIDProperty.stringValue  = collectable.Collection.GUID;
                    objectAssetProperty.serializedObject.ApplyModifiedProperties();
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(collectableGUIDProperty.stringValue) &&
                    !string.IsNullOrEmpty(collectionGUIDProperty.stringValue))
                {
                    if (CollectionsRegistry.Instance.TryGetCollectionByGUID(collectionGUIDProperty.stringValue,
                                                                            out ScriptableObjectCollection collection))
                    {
                        if (collection.TryGetCollectableByGUID(collectableGUIDProperty.stringValue,
                                                               out CollectableScriptableObject collectable))
                        {
                            objectAssetProperty.objectReferenceValue = collectable;
                            objectAssetProperty.serializedObject.ApplyModifiedProperties();
                        }
                    }
                }
            }

            if (cachedReference == null && objectAssetProperty.objectReferenceValue != null)
            {
                cachedReference = objectAssetProperty.objectReferenceValue as CollectableScriptableObject;
            }

            EditorGUI.PropertyField(position, objectAssetProperty, label, true);
            if (objectAssetProperty.objectReferenceValue != cachedReference)
            {
                string collectableGUID = string.Empty;
                string collectionGUID  = string.Empty;

                if (objectAssetProperty.objectReferenceValue != null &&
                    objectAssetProperty.objectReferenceValue is CollectableScriptableObject collectable)
                {
                    collectableGUID = collectable.GUID;
                    collectionGUID  = collectable.Collection.GUID;
                    cachedReference = collectable;
                }
                else
                {
                    cachedReference = null;
                }

                collectableGUIDProperty.stringValue = collectableGUID;
                collectionGUIDProperty.stringValue  = collectionGUID;
                objectAssetProperty.serializedObject.ApplyModifiedProperties();
                EditorUtility.SetDirty(objectAssetProperty.serializedObject.targetObject);
            }
        }
        private static void WriteTryGetAccessCollectionStatic(ScriptableObjectCollection collection, StreamWriter writer,
                                                              ref int indentation)
        {
            string cachedValuesName = $"values";
            string valuesName       = $"Values";
            string tryGetValuesName = $"TryGet{valuesName}";

            AppendLine(writer, indentation, $"private static {collection.GetType().Name} {cachedValuesName};");

            for (int i = 0; i < collection.Items.Count; i++)
            {
                CollectableScriptableObject collectionItem = collection.Items[i];
                AppendLine(writer, indentation,
                           $"private static {collectionItem.GetType().Name} {collectionItem.name.Sanitize().FirstToLower()};");
            }

            AppendLine(writer, indentation);

            AppendLine(writer, indentation, $"public static bool {tryGetValuesName}(out {collection.GetType().Name} result)");
            AppendLine(writer, indentation, "{");
            indentation++;
            AppendLine(writer, indentation, $"if ({cachedValuesName} != null)");
            AppendLine(writer, indentation, "{");
            indentation++;
            AppendLine(writer, indentation, $"result = {cachedValuesName};");
            AppendLine(writer, indentation, "return true;");
            indentation--;
            AppendLine(writer, indentation, "}");
            AppendLine(writer, indentation);
            AppendLine(writer, indentation, $"if (!CollectionsRegistry.Instance.TryGetCollectionByGUID(\"{collection.GUID}\", out ScriptableObjectCollection resultCollection))");
            AppendLine(writer, indentation, "{");
            indentation++;
            AppendLine(writer, indentation, $"result = {cachedValuesName};");
            AppendLine(writer, indentation, "return false;");
            indentation--;
            AppendLine(writer, indentation, "}");
            AppendLine(writer, indentation);
            AppendLine(writer, indentation, $"{cachedValuesName} = ({collection.GetType().Name}) resultCollection;");
            AppendLine(writer, indentation, $"result = {cachedValuesName};");
            AppendLine(writer, indentation, $"return true;");
            indentation--;
            AppendLine(writer, indentation, "}");

            AppendLine(writer, indentation);

            for (int i = 0; i < collection.Items.Count; i++)
            {
                CollectableScriptableObject collectionItem = collection.Items[i];
                string pascalizedItemName = collectionItem.name.Sanitize().FirstToUpper();
                string camelizedItemName  = collectionItem.name.Sanitize().FirstToLower();
                Type   itemType           = collectionItem.GetType();


                AppendLine(writer, indentation, $"public static bool TryGet{pascalizedItemName}(out {itemType.Name} result)");
                AppendLine(writer, indentation, "{");
                indentation++;
                AppendLine(writer, indentation, $"if ({camelizedItemName} != null)");
                AppendLine(writer, indentation, "{");
                indentation++;
                AppendLine(writer, indentation, $"result = {camelizedItemName};");
                AppendLine(writer, indentation, "return true;");
                indentation--;
                AppendLine(writer, indentation, "}");
                AppendLine(writer, indentation);

                AppendLine(writer, indentation, $"if (!{tryGetValuesName}(out {collection.GetType().Name} collectionResult))");
                AppendLine(writer, indentation, "{");
                indentation++;
                AppendLine(writer, indentation, "result = null;");
                AppendLine(writer, indentation, "return false;");
                indentation--;
                AppendLine(writer, indentation, "}");
                AppendLine(writer, indentation);


                AppendLine(writer, indentation, $"if (!collectionResult.TryGetCollectableByGUID(\"{collectionItem.GUID}\", out CollectableScriptableObject resultCollectable))");
                AppendLine(writer, indentation, "{");
                indentation++;
                AppendLine(writer, indentation, "result = null;");
                AppendLine(writer, indentation, "return false;");
                indentation--;
                AppendLine(writer, indentation);
                AppendLine(writer, indentation, "}");


                AppendLine(writer, indentation, $"{camelizedItemName} = ({itemType.Name}) resultCollectable;");
                AppendLine(writer, indentation, $"result = {camelizedItemName};");
                AppendLine(writer, indentation, "return true;");
                indentation--;
                AppendLine(writer, indentation, "}");
                AppendLine(writer, indentation);
            }
        }
        private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection collection, StreamWriter writer,
                                                              ref int indentation)
        {
            bool isGeneratingCustomStaticFile = ScriptableObjectCollectionSettings.Instance.IsGeneratingCustomStaticFile(collection);

            string cachedValuesName = "values";

            AppendLine(writer, indentation, $"private static {collection.GetType().Name} {cachedValuesName};");

            for (int i = 0; i < collection.Items.Count; i++)
            {
                CollectableScriptableObject collectionItem = collection.Items[i];
                AppendLine(writer, indentation,
                           $"private static {collectionItem.GetType().Name} {collectionItem.name.Sanitize().FirstToLower()};");
            }

            AppendLine(writer, indentation);

            string valuesName = $"Values";

            if (!isGeneratingCustomStaticFile)
            {
                AppendLine(writer, indentation,
                           $"public static {collection.GetType().Name} {valuesName}");
            }
            else
            {
                AppendLine(writer, indentation,
                           $"public static {collection.GetType().Name} {valuesName}");
            }
            AppendLine(writer, indentation, "{");
            indentation++;
            AppendLine(writer, indentation, "get");
            AppendLine(writer, indentation, "{");
            indentation++;
            AppendLine(writer, indentation, $"if ({cachedValuesName} == null)");
            indentation++;
            AppendLine(writer, indentation,
                       $"{cachedValuesName} = ({collection.GetType()})CollectionsRegistry.Instance.GetCollectionByGUID(\"{collection.GUID}\");");
            indentation--;
            AppendLine(writer, indentation, $"return {cachedValuesName};");
            indentation--;
            AppendLine(writer, indentation, "}");
            indentation--;
            AppendLine(writer, indentation, "}");
            AppendLine(writer, indentation);

            AppendLine(writer, indentation);

            for (int i = 0; i < collection.Items.Count; i++)
            {
                CollectableScriptableObject collectionItem = collection.Items[i];
                string collectionNameFirstUpper            = collectionItem.name.Sanitize().FirstToUpper();
                string privateStaticName = collectionItem.name.Sanitize().FirstToLower();

                AppendLine(writer, indentation,
                           $"public static {collectionItem.GetType().Name} {collectionNameFirstUpper}");
                AppendLine(writer, indentation, "{");
                indentation++;
                AppendLine(writer, indentation, "get");
                AppendLine(writer, indentation, "{");
                indentation++;

                AppendLine(writer, indentation, $"if ({privateStaticName} == null)");
                indentation++;
                AppendLine(writer, indentation,
                           $"{privateStaticName} = ({collectionItem.GetType().Name}){valuesName}.GetCollectableByGUID(\"{collectionItem.GUID}\");");
                indentation--;
                AppendLine(writer, indentation, $"return {privateStaticName};");
                indentation--;
                AppendLine(writer, indentation, "}");
                indentation--;
                AppendLine(writer, indentation, "}");
                AppendLine(writer, indentation);
            }
        }
 public CollectableDropdownItem(CollectableScriptableObject targetCollectable) : base(targetCollectable.name)
 {
     Collectable = targetCollectable;
 }