Exemple #1
0
        internal void DrawCollectionItemDrawer(Rect position, ScriptableObjectCollectionItem collectionItem, GUIContent label,
                                               Action <ScriptableObjectCollectionItem> callback)
        {
            float originY = position.y;

            position.height = 15;
            Rect prefixPosition = EditorGUI.PrefixLabel(position, label);
            int  indent         = EditorGUI.indentLevel;

            EditorGUI.indentLevel = 0;
            if (collectionItem != null)
            {
                if (currentObject == null)
                {
                    currentObject = collectionItem;
                }

                DrawEditFoldoutButton(ref prefixPosition, collectionItem);
                DrawGotoButton(ref prefixPosition);
            }

            DrawCollectionItemDropDown(ref prefixPosition, collectionItem, callback);
            DrawEditorPreview(ref position, collectionItem);
            EditorGUI.indentLevel = indent;
            totalHeight           = position.y - originY;
        }
Exemple #2
0
        protected override AdvancedDropdownItem BuildRoot()
        {
            AdvancedDropdownItem root = new AdvancedDropdownItem(itemType.Name);

            root.AddChild(new AdvancedDropdownItem("None"));
            root.AddSeparator();

            AdvancedDropdownItem targetParent = root;
            bool multipleCollections          = collections.Count > 1;

            for (int i = 0; i < collections.Count; i++)
            {
                ScriptableObjectCollection collection = collections[i];

                if (multipleCollections)
                {
                    AdvancedDropdownItem collectionParent = new AdvancedDropdownItem(collection.name);
                    root.AddChild(collectionParent);
                    targetParent = collectionParent;
                }

                for (int j = 0; j < collection.Count; j++)
                {
                    ScriptableObjectCollectionItem collectionItem = collection[j];
                    targetParent.AddChild(new CollectionItemDropdownItem(collectionItem));
                }
            }

            if (!multipleCollections)
            {
                root.AddSeparator();
                root.AddChild(new AdvancedDropdownItem(CREATE_NEW_TEXT));
            }
            return(root);
        }
Exemple #3
0
        private void DrawEditorPreview(ref Rect rect, ScriptableObjectCollectionItem scriptableObjectCollectionItem)
        {
            if (scriptableObjectCollectionItem == null)
            {
                return;
            }

            if (!CollectionUtility.IsFoldoutOpen(scriptableObjectCollectionItem, currentObject))
            {
                return;
            }

            rect.y     += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
            rect.width -= 10;
            rect.y     += 10;
            float beginPositionY = rect.y;

            SerializedObject collectionItemSerializedObject = new SerializedObject(scriptableObjectCollectionItem);

            EditorGUI.indentLevel++;
            rect = EditorGUI.IndentedRect(rect);
            SerializedProperty iterator = collectionItemSerializedObject.GetIterator();

            using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
            {
                for (bool enterChildren = true; iterator.NextVisible(enterChildren); enterChildren = false)
                {
                    bool guiEnabled = GUI.enabled;
                    if (iterator.displayName.Equals("Script"))
                    {
                        GUI.enabled = false;
                    }

                    EditorGUI.PropertyField(rect, iterator, true);

                    GUI.enabled = guiEnabled;

                    rect.y += EditorGUI.GetPropertyHeight(iterator, true) + EditorGUIUtility.standardVerticalSpacing;
                }

                if (changeCheck.changed)
                {
                    iterator.serializedObject.ApplyModifiedProperties();
                }
            }

            EditorGUI.indentLevel--;
            rect = EditorGUI.IndentedRect(rect);

            Rect boxPosition = rect;

            boxPosition.y      = beginPositionY - 5;
            boxPosition.height = (rect.y - beginPositionY) + 10;
            boxPosition.width += 10;
            boxPosition.x     += 5;
            rect.y            += 10;
            GUI.Box(boxPosition, GUIContent.none, EditorStyles.helpBox);
        }
Exemple #4
0
        public static bool CanPasteToTarget(ScriptableObjectCollectionItem target)
        {
            if (source == null)
            {
                return(false);
            }

            return(target.GetType() == source.GetType());
        }
Exemple #5
0
        public static void ApplySourceToTarget(ScriptableObjectCollectionItem target)
        {
            if (source == null)
            {
                return;
            }

            Undo.RecordObject(target, "Paste Changes");
            EditorUtility.CopySerializedManagedFieldsOnly(source, target);
            EditorUtility.SetDirty(target);
        }
Exemple #6
0
        private void DrawCollectionItemDropDown(ref Rect position, ScriptableObjectCollectionItem collectionItem,
                                                Action <ScriptableObjectCollectionItem> callback)
        {
            GUIContent displayValue = new GUIContent("None");

            if (collectionItem != null)
            {
                displayValue = new GUIContent(collectionItem.name);
            }

            if (GUI.Button(position, displayValue, EditorStyles.popup))
            {
                collectionItemDropdown.Show(position, callback.Invoke);
            }
        }
Exemple #7
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            Initialize(property);

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

            item = property.objectReferenceValue as ScriptableObjectCollectionItem;

            DrawCollectionItemDrawer(position, item, label,
                                     newItem =>
            {
                property.objectReferenceValue = newItem;
                property.serializedObject.ApplyModifiedProperties();
            });
        }
Exemple #8
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(ScriptableObjectCollectionItem)))
            {
                ScriptableObjectCollectionItem collectionItem =
                    AssetDatabase.LoadAssetAtPath <ScriptableObjectCollectionItem>(targetAssetPath);

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

            return(AssetDeleteResult.DidNotDelete);
        }
Exemple #9
0
        private void DrawEditFoldoutButton(ref Rect popupRect, ScriptableObjectCollectionItem targetItem)
        {
            Rect buttonRect = popupRect;

            buttonRect.width  = 30;
            buttonRect.height = 18;
            popupRect.width  -= buttonRect.width;
            buttonRect.x     += popupRect.width;

            GUIContent guiContent = CollectionEditorGUI.EditGUIContent;

            if (CollectionUtility.IsFoldoutOpen(targetItem, currentObject))
            {
                guiContent = CollectionEditorGUI.CloseGUIContent;
            }

            if (GUI.Button(buttonRect, guiContent))
            {
                CollectionUtility.SetFoldoutOpen(!CollectionUtility.IsFoldoutOpen(targetItem, currentObject), targetItem, currentObject);
                ObjectUtility.SetDirty(targetItem);
            }
        }
Exemple #10
0
        protected override void ItemSelected(AdvancedDropdownItem item)
        {
            base.ItemSelected(item);

            if (item.name.Equals(CREATE_NEW_TEXT, StringComparison.OrdinalIgnoreCase))
            {
                ScriptableObjectCollection     collection     = collections.First();
                ScriptableObjectCollectionItem collectionItem = collection.AddNew(itemType);
                callback.Invoke(collectionItem);
                Selection.objects = new Object[] { collection };
                CollectionCustomEditor.SetLastAddedEnum(collectionItem);
                return;
            }

            if (item is CollectionItemDropdownItem dropdownItem)
            {
                callback.Invoke(dropdownItem.CollectionItem);
            }
            else
            {
                callback.Invoke(null);
            }
        }
Exemple #11
0
 public CollectionItemDropdownItem(ScriptableObjectCollectionItem target) : base(target.name)
 {
     CollectionItem = target;
 }
Exemple #12
0
        private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection collection, StreamWriter writer,
                                                              ref int indentation)
        {
            string cachedValuesName = "values";

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

            AppendLine(writer, indentation);

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

            AppendLine(writer, indentation);

            string valuesName = $"Values";

            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++)
            {
                ScriptableObjectCollectionItem 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}.GetItemByGUID(\"{collectionItem.GUID}\");");
                indentation--;
                AppendLine(writer, indentation, $"return {privateStaticName};");
                indentation--;
                AppendLine(writer, indentation, "}");
                indentation--;
                AppendLine(writer, indentation, "}");
                AppendLine(writer, indentation);
            }


            AppendLine(writer, indentation, $"public static IEnumerable<T> GetValues<T>() where T : {collection.GetItemType().Name}");
            AppendLine(writer, indentation, "{");
            indentation++;
            AppendLine(writer, indentation, $"return Values.Where(item => item is T).Cast<T>();");
            indentation--;
            AppendLine(writer, indentation, "}");

            AppendLine(writer, indentation);
        }
Exemple #13
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (collectionItemType == null)
            {
                Type arrayOrListType = fieldInfo.FieldType.GetArrayOrListType();
                Type properFieldType = arrayOrListType ?? fieldInfo.FieldType;
                collectionItemType = GetGenericItemType(properFieldType).GetGenericArguments().First();
            }

            if (collectionItemPropertyDrawer == null)
            {
                collectionItemPropertyDrawer = new CollectionItemItemPropertyDrawer();
                collectionItemPropertyDrawer.Initialize(collectionItemType, null);
            }

            SerializedProperty collectionItemGUIDSerializedProperty = property.FindPropertyRelative(COLLECTION_ITEM_GUID_PROPERTY_PATH);
            SerializedProperty collectionGUIDSerializedProperty     = property.FindPropertyRelative(COLLECTION_GUID_PROPERTY_PATh);

            ScriptableObjectCollectionItem collectionItem = null;

            if (!string.IsNullOrEmpty(collectionItemGUIDSerializedProperty.stringValue) &&
                !string.IsNullOrEmpty(collectionGUIDSerializedProperty.stringValue))
            {
                if (CollectionsRegistry.Instance.TryGetCollectionByGUID(collectionGUIDSerializedProperty.stringValue,
                                                                        out ScriptableObjectCollection collection))
                {
                    if (collection.TryGetItemByGUID(collectionItemGUIDSerializedProperty.stringValue,
                                                    out ScriptableObjectCollectionItem resultCollection))
                    {
                        collectionItem = resultCollection;
                    }
                }
            }

            int indexOfArrayPart = property.propertyPath.IndexOf('[');

            if (indexOfArrayPart > -1)
            {
                if (string.Equals(label.text, collectionItemGUIDSerializedProperty.stringValue, StringComparison.Ordinal))
                {
                    label.text = $"Element {property.propertyPath.Substring(indexOfArrayPart+1, 1)}";
                }
            }

            collectionItemPropertyDrawer.DrawCollectionItemDrawer(
                position, collectionItem, label,
                item =>
            {
                string collectionItemGUID = string.Empty;
                string collectionGUID     = string.Empty;
                if (item != null)
                {
                    collectionItemGUID = item.GUID;
                    collectionGUID     = item.Collection.GUID;
                }

                collectionItemGUIDSerializedProperty.stringValue = collectionItemGUID;
                collectionGUIDSerializedProperty.stringValue     = collectionGUID;
                collectionItem = item;
                property.serializedObject.ApplyModifiedProperties();
            }
                );
        }
Exemple #14
0
 public static void SetSource(ScriptableObjectCollectionItem targetSource)
 {
     source = targetSource;
 }