public void SetStaticFileGeneratorTypeForCollection(ScriptableObjectCollection targetCollection, GeneratedStaticFileType staticCodeGeneratorType)
        {
            CollectionToSettings settings = GetOrCreateSettingsForCollection(targetCollection);

            settings.generatedStaticFileGeneratorType = staticCodeGeneratorType;
            ObjectUtility.SetDirty(this);
        }
Exemplo n.º 2
0
        private static bool CanGenerateStaticFile(ScriptableObjectCollection collection, out string errorMessage)
        {
            List <ScriptableObjectCollection> collectionsOfSameType = CollectionsRegistry.Instance.GetCollectionsByItemType(collection.GetItemType());

            if (collectionsOfSameType.Count > 1)
            {
                for (int i = 0; i < collectionsOfSameType.Count; i++)
                {
                    ScriptableObjectCollection collectionA = collectionsOfSameType[i];
                    SerializedObject           collectionASerializedObject = new SerializedObject(collectionA);
                    for (int j = 0; j < collectionsOfSameType.Count; j++)
                    {
                        if (i == j)
                        {
                            continue;
                        }

                        ScriptableObjectCollection collectionB = collectionsOfSameType[j];
                        SerializedObject           collectionBSerializedObject = new SerializedObject(collectionB);

                        if (collectionASerializedObject.FindProperty("generatedStaticClassFileName").stringValue.Equals(collectionBSerializedObject.FindProperty("generatedStaticClassFileName").stringValue) &&
                            collectionASerializedObject.FindProperty("generateStaticFileNamespace").stringValue.Equals(collectionASerializedObject.FindProperty("generateStaticFileNamespace").stringValue))
                        {
                            errorMessage =
                                $"Two collections ({collectionA.name} and {collectionB.name}) with the same name and namespace already exist, please use custom ones";
                            return(false);
                        }
                    }
                }
            }

            errorMessage = String.Empty;
            return(true);
        }
Exemplo n.º 3
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);
        }
        public void SetStaticFileFolderForCollection(ScriptableObjectCollection targetCollection, string targetFolder)
        {
            CollectionToSettings settings = GetOrCreateSettingsForCollection(targetCollection);

            settings.staticGeneratedFileParentFolder = targetFolder;
            ObjectUtility.SetDirty(this);
        }
        private void ValidateCurrentGUIDs()
        {
            ValidateItems();
            if (collectionGUIDs.Count != collections.Count)
            {
                ReloadCollections();
                return;
            }

            for (int i = 0; i < collectionGUIDs.Count; i++)
            {
                string guid      = collectionGUIDs[i];
                bool   guidFound = false;
                for (int j = 0; j < collections.Count; j++)
                {
                    ScriptableObjectCollection collection = collections[j];
                    if (string.Equals(collection.GUID, guid, StringComparison.Ordinal))
                    {
                        guidFound = true;
                        break;
                    }
                }

                if (!guidFound)
                {
                    ReloadCollections();
                    break;
                }
            }
        }
        public void SetGenerateCustomStaticFileName(ScriptableObjectCollection targetCollection, string targetName)
        {
            CollectionToSettings settings = GetOrCreateSettingsForCollection(targetCollection);

            settings.customStaticFileName = targetName;
            ObjectUtility.SetDirty(this);
        }
Exemplo n.º 7
0
        public void ValidateGUIDs()
        {
            for (int i = 0; i < collections.Count; i++)
            {
                ScriptableObjectCollection collectionA = collections[i];
                for (int j = 0; j < collections.Count; j++)
                {
                    ScriptableObjectCollection collectionB = collections[j];

                    if (i == j)
                    {
                        continue;
                    }

                    if (string.Equals(collectionA.GUID, collectionB.GUID, StringComparison.OrdinalIgnoreCase))
                    {
                        collectionB.GenerateNewGUID();
                        ObjectUtility.SetDirty(collectionB);
                        Debug.LogWarning(
                            $"Found duplicated collection GUID, please regenerate code of collection {collectionB.name}",
                            this
                            );
                    }
                }

                collectionA.ValidateGUIDs();
            }
        }
        public void SetCollectionAutomaticallyLoaded(ScriptableObjectCollection targetCollection, bool isAutomaticallyLoaded)
        {
            CollectionToSettings settings = GetOrCreateSettingsForCollection(targetCollection);

            settings.isAutomaticallyLoaded = isAutomaticallyLoaded;
            ObjectUtility.SetDirty(this);
        }
Exemplo n.º 9
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);
        }
        public void ReloadCollections()
        {
#if UNITY_EDITOR
            if (Application.isPlaying)
            {
                return;
            }

            string[] collectionsGUIDs = UnityEditor.AssetDatabase.FindAssets($"t:{nameof(ScriptableObjectCollection)}");

            collections.Clear();
            collectionGUIDs.Clear();

            for (int i = 0; i < collectionsGUIDs.Length; i++)
            {
                string collectionGUID = collectionsGUIDs[i];

                ScriptableObjectCollection collection =
                    UnityEditor.AssetDatabase.LoadAssetAtPath <ScriptableObjectCollection>(
                        UnityEditor.AssetDatabase.GUIDToAssetPath(collectionGUID));

                if (collection == null)
                {
                    continue;
                }

                collection.RefreshCollection();
                collections.Add(collection);
                collectionGUIDs.Add(collection.GUID);
            }

            ObjectUtility.SetDirty(this);
#endif
        }
        public void SetOverridingStaticFileLocation(ScriptableObjectCollection collection, bool isOverriding)
        {
            CollectionToSettings settings = GetOrCreateSettingsForCollection(collection);

            settings.overrideStaticFileLocation = isOverriding;
            ObjectUtility.SetDirty(this);
        }
        public void SetGenerateCustomStaticFile(ScriptableObjectCollection targetCollection, bool isUsingCustom)
        {
            CollectionToSettings settings = GetOrCreateSettingsForCollection(targetCollection);

            settings.generateCustomStaticFile = isUsingCustom;
            ObjectUtility.SetDirty(this);
        }
        public static void GenerateStaticCollectionScript(ScriptableObjectCollection collection)
        {
            string fileName = $"{collection.GetCollectionType().Name}Static";
            bool   isGeneratingCustomStaticFile = ScriptableObjectCollectionSettings.Instance.IsGeneratingCustomStaticFile(collection);

            if (isGeneratingCustomStaticFile)
            {
                fileName = ScriptableObjectCollectionSettings.Instance.GetGeneratedStaticFileName(collection);
            }

            string nameSpace   = collection.GetCollectionType().Namespace;
            string finalFolder = ScriptableObjectCollectionSettings.Instance.GetStaticFileFolderForCollection(collection);

            if (string.IsNullOrEmpty(finalFolder))
            {
                Debug.LogError("Static Code Generation folder not assigned, please assign it on the ScriptableObjectCollectionSettings");
                EditorGUIUtility.PingObject(ScriptableObjectCollectionSettings.Instance);
                Selection.objects = new Object[] { ScriptableObjectCollectionSettings.Instance };
                return;
            }

            AssetDatabaseUtils.CreatePathIfDontExist(finalFolder);
            using (StreamWriter writer = new StreamWriter(Path.Combine(finalFolder, $"{fileName}.cs")))
            {
                int indentation = 0;

                List <string> directives = new List <string>();
                directives.Add(typeof(CollectionsRegistry).Namespace);
                directives.Add(collection.GetType().Namespace);
                directives.AddRange(GetCollectionDirectives(collection));

                if (!isGeneratingCustomStaticFile)
                {
                    AppendHeader(writer, ref indentation, nameSpace, "",
                                 collection.GetCollectionType().Name, true, false, directives.Distinct().ToArray());
                }
                else
                {
                    AppendHeader(writer, ref indentation, nameSpace, "",
                                 fileName, false, false, directives.Distinct().ToArray());
                }

                GeneratedStaticFileType staticFileTypeForCollection = ScriptableObjectCollectionSettings.Instance.GetStaticFileTypeForCollection(collection);
                if (staticFileTypeForCollection == GeneratedStaticFileType.DirectAccess)
                {
                    WriteDirectAccessCollectionStatic(collection, writer, ref indentation);
                }
                else
                {
                    WriteTryGetAccessCollectionStatic(collection, writer, ref indentation);
                }

                indentation--;
                AppendFooter(writer, ref indentation, nameSpace);
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
        public bool IsOverridingStaticFileLocation(ScriptableObjectCollection collection)
        {
            if (!TryGetSettingsForCollection(collection, out CollectionToSettings settings))
            {
                return(false);
            }

            return(settings.overrideStaticFileLocation);
        }
Exemplo n.º 15
0
        public void UnregisterCollection(ScriptableObjectCollection targetCollection)
        {
            if (!collections.Contains(targetCollection))
            {
                return;
            }

            collections.Remove(targetCollection);
        }
        public bool IsGeneratingCustomStaticFile(ScriptableObjectCollection collection)
        {
            if (!TryGetSettingsForCollection(collection, out CollectionToSettings settings))
            {
                return(false);
            }

            return(settings.generateCustomStaticFile);
        }
        public GeneratedStaticFileType GetStaticFileTypeForCollection(ScriptableObjectCollection collection)
        {
            if (!TryGetSettingsForCollection(collection, out CollectionToSettings settings))
            {
                return(defaultGenerator);
            }

            return(settings.generatedStaticFileGeneratorType);
        }
Exemplo n.º 18
0
        public void RegisterCollection(ScriptableObjectCollection targetCollection)
        {
            if (collections.Contains(targetCollection))
            {
                return;
            }

            collections.Add(targetCollection);
        }
        public bool IsCollectionAutomaticallyLoaded(ScriptableObjectCollection collection)
        {
            if (!TryGetSettingsForCollection(collection, out CollectionToSettings settings))
            {
                return(true);
            }

            return(settings.isAutomaticallyLoaded);
        }
        private static string[] GetCollectionDirectives(ScriptableObjectCollection collection)
        {
            HashSet <string> directives = new HashSet <string>();

            for (int i = 0; i < collection.Count; i++)
            {
                directives.Add(collection[i].GetType().Namespace);
            }

            return(directives.ToArray());
        }
        public bool TryGetCollectionByGUID <T>(string targetGUID, out ScriptableObjectCollection <T> resultCollection) where T : CollectableScriptableObject
        {
            if (TryGetCollectionByGUID(targetGUID, out ScriptableObjectCollection foundCollection))
            {
                resultCollection = foundCollection as ScriptableObjectCollection <T>;
                return(true);
            }

            resultCollection = null;
            return(false);
        }
        public bool TryGetCollectionFromCollectableType <TargetType>(out ScriptableObjectCollection <TargetType> scriptableObjectCollection) where TargetType : CollectableScriptableObject
        {
            if (TryGetCollectionFromCollectableType(typeof(TargetType), out ScriptableObjectCollection resultCollection))
            {
                scriptableObjectCollection = (ScriptableObjectCollection <TargetType>)resultCollection;
                return(true);
            }

            scriptableObjectCollection = null;
            return(false);
        }
        private CollectionToSettings GetOrCreateSettingsForCollection(ScriptableObjectCollection targetCollection)
        {
            if (!TryGetSettingsForCollection(targetCollection, out CollectionToSettings settings))
            {
                settings = new CollectionToSettings {
                    collection = targetCollection
                };
                collectionsSettings.Add(settings);
            }

            return(settings);
        }
        public void OnEnable()
        {
            collection = (ScriptableObjectCollection)target;

            if (!CollectionsRegistry.Instance.IsKnowCollectionGUID(collection.GUID))
            {
                CollectionsRegistry.Instance.ReloadCollections();
            }


            FixCollectionItems();
            ValidateGUIDS();
        }
Exemplo n.º 25
0
        public bool IsKnowCollection(ScriptableObjectCollection targetCollection)
        {
            for (int i = 0; i < collections.Count; i++)
            {
                ScriptableObjectCollection collection = collections[i];
                if (collection != null && collection.GUID.Equals(targetCollection.GUID, StringComparison.Ordinal))
                {
                    return(true);
                }
            }

            return(false);
        }
        public static void DrawGotoButton(ScriptableObjectCollection enumValues, ref Rect popupRect)
        {
            Rect buttonRect = popupRect;

            buttonRect.width  = 30;
            buttonRect.height = 18;
            popupRect.width  -= buttonRect.width;
            buttonRect.x     += popupRect.width;
            if (GUI.Button(buttonRect, CollectionEditorGUI.ARROW_RIGHT_CHAR))
            {
                Selection.activeObject = enumValues;
            }
        }
        public void RemoveNonAutomaticallyInitializedCollections()
        {
            for (int i = collections.Count - 1; i >= 0; i--)
            {
                ScriptableObjectCollection collection = collections[i];
                if (ScriptableObjectCollectionSettings.Instance.IsCollectionAutomaticallyLoaded(collection))
                {
                    continue;
                }

                collections.Remove(collection);
                collectionGUIDs.Remove(collection.GUID);
            }
        }
        public string GetStaticFileFolderForCollection(ScriptableObjectCollection collection)
        {
            if (!TryGetSettingsForCollection(collection, out CollectionToSettings settings))
            {
                return(DefaultGeneratedCodeFolder);
            }

            if (!settings.overrideStaticFileLocation || string.IsNullOrEmpty(settings.staticGeneratedFileParentFolder))
            {
                return(DefaultGeneratedCodeFolder);
            }

            return(settings.staticGeneratedFileParentFolder);
        }
        public string GetGeneratedStaticFileName(ScriptableObjectCollection collection)
        {
            if (!TryGetSettingsForCollection(collection, out CollectionToSettings settings))
            {
                return(collection.GetCollectionType().Name);
            }

            if (string.IsNullOrEmpty(settings.customStaticFileName))
            {
                return(collection.GetCollectionType().Name);
            }

            return(settings.customStaticFileName);
        }
Exemplo n.º 30
0
        public static void GenerateStaticCollectionScript(ScriptableObjectCollection collection)
        {
            CollectionsRegistry.Instance.ValidateGUIDs();
            if (!CanGenerateStaticFile(collection, out string errorMessage))
            {
                Debug.LogError(errorMessage);
                return;
            }

            SerializedObject collectionSerializedObject = new SerializedObject(collection);


            string fileName  = collectionSerializedObject.FindProperty("generatedStaticClassFileName").stringValue;
            string nameSpace = collectionSerializedObject.FindProperty("generateStaticFileNamespace").stringValue;

            string finalFolder    = collectionSerializedObject.FindProperty("generatedFileLocationPath").stringValue;
            bool   writeAsPartial = collectionSerializedObject.FindProperty("generateAsPartialClass").boolValue;


            AssetDatabaseUtils.CreatePathIfDontExist(finalFolder);
            using (StreamWriter writer = new StreamWriter(Path.Combine(finalFolder, $"{fileName}.cs")))
            {
                int indentation = 0;

                List <string> directives = new List <string>();
                directives.Add(typeof(CollectionsRegistry).Namespace);
                directives.Add(collection.GetType().Namespace);
                directives.Add(typeof(List <>).Namespace);
                directives.Add("System.Linq");
                directives.AddRange(GetCollectionDirectives(collection));
                string className = collection.GetItemType().Name;
                if (!writeAsPartial)
                {
                    className = fileName;
                }

                AppendHeader(writer, ref indentation, nameSpace, "", className,
                             writeAsPartial,
                             false, directives.Distinct().ToArray()
                             );

                WriteDirectAccessCollectionStatic(collection, writer, ref indentation);

                indentation--;
                AppendFooter(writer, ref indentation, nameSpace);
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }