public static bool TryLoad <T>(string path, out T asset) where T : ScriptableObject
        {
            var assetDatabasePath = PathUtility.FromProject(path);

            if (File.Exists(path))
            {
                // Try loading the existing asset file.
                asset = AssetDatabase.LoadAssetAtPath <T>(assetDatabasePath);

                if (asset == null)
                {
                    // The file exists, but it isn't a valid asset.
                    // Warn and leave the asset as is to prevent losing its serialized contents
                    // because we might be able to salvage them by deserializing later on.
                    // Return a new empty instance in the mean time.
                    Debug.LogWarning($"Loading {typeof(T).FullName} failed:\n{assetDatabasePath}");
                    asset = ScriptableObject.CreateInstance <T>();
                    return(false);
                }
            }
            else
            {
                // The file doesn't exist, so create a new asset and save it.
                asset = ScriptableObject.CreateInstance <T>();
                PathUtility.CreateParentDirectoryIfNeeded(path);
                AssetDatabase.CreateAsset(asset, assetDatabasePath);
                AssetDatabase.SaveAssets();
            }

            return(true);
        }
Exemple #2
0
        public static void SaveVariableAsset(VariablesAsset asset, string fileName)
        {
            var path = Path.Combine(BoltCore.Paths.variableResources, fileName + ".asset");

            if (String.IsNullOrEmpty(AssetDatabase.GetAssetPath(asset)))
            {
                var assetDatabasePath = PathUtility.FromProject(path);
                PathUtility.CreateParentDirectoryIfNeeded(path);
                AssetDatabase.CreateAsset(asset, assetDatabasePath);
                AssetDatabase.SaveAssets();
            }
        }
Exemple #3
0
        private void SerializeProjectSettingsAssetToDisk()
        {
            if (_projectSettingsAssetDirty)
            {
                // make sure the path exists or file write will fail
                PathUtility.CreateParentDirectoryIfNeeded(projectSettingsStoragePath);

                const bool saveAsText = true;
                InternalEditorUtility.SaveToSerializedFileAndForget(new UnityEngine.Object[] { projectSettingsAsset },
                                                                    projectSettingsStoragePath, saveAsText);
            }

            _projectSettingsAssetDirty   = false;
            EditorApplication.delayCall -= SerializeProjectSettingsAssetToDisk;
        }
Exemple #4
0
        public static void Build(bool initialBuild = false)
        {
            if (IsUnitOptionsBuilt())
            {
                return;
            }

            if (initialBuild)
            {
                ProgressUtility.SetTitleOverride("Visual Scripting: Initial Node Generation...");
            }

            const string progressTitle = "Visual Scripting: Building node database...";

            lock (@lock)
            {
                using (ProfilingUtility.SampleBlock("Update Node Database"))
                {
                    using (NativeUtility.Module("sqlite3.dll"))
                    {
                        SQLiteConnection database = null;

                        try
                        {
                            ProgressUtility.DisplayProgressBar(progressTitle, "Creating database...", 0);

                            PathUtility.CreateParentDirectoryIfNeeded(BoltFlow.Paths.unitOptions);
                            database = new SQLiteConnection(BoltFlow.Paths.unitOptions);
                            database.CreateTable <UnitOptionRow>();

                            ProgressUtility.DisplayProgressBar(progressTitle, "Updating codebase...", 0);

                            UpdateCodebase();

                            ProgressUtility.DisplayProgressBar(progressTitle, "Updating type mappings...", 0);

                            UpdateTypeMappings();

                            ProgressUtility.DisplayProgressBar(progressTitle,
                                                               "Converting codebase to node options...", 0);

                            options = new HashSet <IUnitOption>(GetStaticOptions());

                            var rows = new HashSet <UnitOptionRow>();

                            var progress          = 0;
                            var lastShownProgress = 0f;

                            foreach (var option in options)
                            {
                                try
                                {
                                    var shownProgress = (float)progress / options.Count;

                                    if (shownProgress > lastShownProgress + 0.01f)
                                    {
                                        ProgressUtility.DisplayProgressBar(progressTitle,
                                                                           "Converting codebase to node options...", shownProgress);
                                        lastShownProgress = shownProgress;
                                    }

                                    rows.Add(option.Serialize());
                                }
                                catch (Exception ex)
                                {
                                    Debug.LogError($"Failed to save option '{option.GetType()}'.\n{ex}");
                                }

                                progress++;
                            }

                            ProgressUtility.DisplayProgressBar(progressTitle, "Writing to database...", 1);

                            try
                            {
                                database.CreateTable <UnitOptionRow>();
                                database.InsertAll(rows);
                            }
                            catch (Exception ex)
                            {
                                Debug.LogError($"Failed to write options to database.\n{ex}");
                            }
                        }
                        finally
                        {
                            database?.Close();
                            ProgressUtility.ClearProgressBar();
                            ProgressUtility.ClearTitleOverride();
                            AssetDatabase.Refresh();
                            //ConsoleProfiler.Dump();
                        }
                    }
                }
            }
        }