private static bool Load()
        {
            bool loaded = false;

            try {
                var dbPath = DBPath;

                if (File.Exists(dbPath))
                {
                    AssetReferenceDatabase db = AssetDatabase.LoadAssetAtPath <AssetReferenceDatabase>(dbPath);

                    if (db != null && db.m_version == DB_VERSION)
                    {
                        db.m_dictionary = new SortedList <string, AssetReference>();

                        foreach (var r in db.m_assets)
                        {
                            db.m_dictionary.Add(r.importFrom, r);
                        }

                        s_database = db;
                        loaded     = true;
                    }
                }
            } catch (Exception e) {
                LogUtility.Logger.LogWarning(LogUtility.kTag, e);
            }

            return(loaded);
        }
Пример #2
0
        void Load(BuildTarget target,
                  NodeData node,
                  IEnumerable <ConnectionData> connectionsToOutput,
                  PerformGraph.Output Output)
        {
            if (connectionsToOutput == null || Output == null)
            {
                return;
            }

            // SOMEWHERE_FULLPATH/PROJECT_FOLDER/Assets/
            var assetsFolderPath = Application.dataPath + AssetBundleGraphSettings.UNITY_FOLDER_SEPARATOR;

            var outputSource    = new List <AssetReference>();
            var targetFilePaths = FileUtility.GetAllFilePathsInFolder(node.GetLoaderFullLoadPath(target));

            foreach (var targetFilePath in targetFilePaths)
            {
                if (targetFilePath.Contains(AssetBundleGraphSettings.ASSETBUNDLEGRAPH_PATH))
                {
                    continue;
                }

                // already contained into Assets/ folder.
                // imported path is Assets/SOMEWHERE_FILE_EXISTS.
                if (targetFilePath.StartsWith(assetsFolderPath))
                {
                    var relativePath = targetFilePath.Replace(assetsFolderPath, AssetBundleGraphSettings.ASSETS_PATH);

                    var r = AssetReferenceDatabase.GetReference(relativePath);

                    if (!TypeUtility.IsLoadingAsset(r))
                    {
                        continue;
                    }

                    if (r != null)
                    {
                        outputSource.Add(AssetReferenceDatabase.GetReference(relativePath));
                    }
                    continue;
                }

                throw new NodeException(node.Name + ": Invalid Load Path. Path must start with Assets/", node.Name);
            }

            var output = new Dictionary <string, List <AssetReference> > {
                { "0", outputSource }
            };

            var dst = (connectionsToOutput == null || !connectionsToOutput.Any())?
                      null : connectionsToOutput.First();

            Output(dst, output);
        }
        public static void DeleteReference(string relativePath)
        {
            AssetReferenceDatabase db = GetDatabase();

            if (db.m_dictionary.ContainsKey(relativePath))
            {
                var r = db.m_dictionary[relativePath];
                db.m_assets.Remove(r);
                db.m_dictionary.Remove(relativePath);
                SetDBDirty();
            }
        }
        public static void MoveReference(string oldPath, string newPath)
        {
            AssetReferenceDatabase db = GetDatabase();

            if (db.m_dictionary.ContainsKey(oldPath))
            {
                var r = db.m_dictionary[oldPath];
                db.m_dictionary.Remove(oldPath);
                db.m_dictionary[newPath] = r;
                r.importFrom             = newPath;
            }
        }
Пример #5
0
        static void OnPostprocessAllAssets(string[] importedAssets,
                                           string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            LogUtility.Logger.Log("[OnPostprocessAllAssets]");
            AssetBundleGraphEditorWindow.OnAssetsReimported(importedAssets, deletedAssets, movedAssets, movedFromAssetPaths);

            foreach (string str in deletedAssets)
            {
                LogUtility.Logger.Log("Deleted Asset: " + str);
                AssetReferenceDatabase.DeleteReference(str);
            }

            for (int i = 0; i < movedAssets.Length; i++)
            {
                LogUtility.Logger.Log("Moved Asset: " + movedAssets[i] + " from: " + movedFromAssetPaths[i]);
                AssetReferenceDatabase.MoveReference(movedFromAssetPaths[i], movedAssets[i]);
            }
        }
        public static AssetReferenceDatabase GetDatabase()
        {
            if (s_database == null)
            {
                if (!Load())
                {
                    // Create vanilla db
                    s_database              = ScriptableObject.CreateInstance <AssetReferenceDatabase>();
                    s_database.m_assets     = new List <AssetReference>();
                    s_database.m_dictionary = new SortedList <string, AssetReference>();
                    s_database.m_version    = DB_VERSION;

                    AssetDatabase.CreateAsset(s_database, DBPath);
                }
            }

            return(s_database);
        }
        private static AssetReference GetReference(string relativePath, CreateReference creator)
        {
            AssetReferenceDatabase db = GetDatabase();

            if (db.m_dictionary.ContainsKey(relativePath))
            {
                return(db.m_dictionary[relativePath]);
            }
            else
            {
                try {
                    AssetReference r = creator();
                    db.m_assets.Add(r);
                    db.m_dictionary.Add(relativePath, r);
                    AssetReferenceDatabase.SetDBDirty();
                    return(r);
                } catch (AssetReferenceException) {
                    // if give asset is invalid, return null
                    return(null);
                }
            }
        }
        public void Run(BuildTarget target,
                        NodeData node,
                        IEnumerable <PerformGraph.AssetGroups> incoming,
                        IEnumerable <ConnectionData> connectionsToOutput,
                        PerformGraph.Output Output,
                        Action <NodeData, string, float> progressFunc)
        {
            if (incoming == null)
            {
                return;
            }

            var aggregatedGroups = new Dictionary <string, List <AssetReference> >();

            aggregatedGroups[key] = new List <AssetReference>();

            if (progressFunc != null)
            {
                progressFunc(node, "Collecting all inputs...", 0f);
            }

            foreach (var ag in incoming)
            {
                foreach (var name in ag.assetGroups.Keys)
                {
                    if (!aggregatedGroups.ContainsKey(name))
                    {
                        aggregatedGroups[name] = new List <AssetReference>();
                    }
                    aggregatedGroups[name].AddRange(ag.assetGroups[name].AsEnumerable());
                }
            }

            var bundleOutputDir = FileUtility.EnsureAssetBundleCacheDirExists(target, node);

            var bundleNames    = aggregatedGroups.Keys.ToList();
            var bundleVariants = new Dictionary <string, List <string> >();

            if (progressFunc != null)
            {
                progressFunc(node, "Building bundle variants map...", 0.2f);
            }

            // get all variant name for bundles
            foreach (var name in aggregatedGroups.Keys)
            {
                if (!bundleVariants.ContainsKey(name))
                {
                    bundleVariants[name] = new List <string>();
                }
                var assets = aggregatedGroups[name];
                foreach (var a in assets)
                {
                    var variantName = a.variantName;
                    if (!bundleVariants[name].Contains(variantName))
                    {
                        bundleVariants[name].Add(variantName);
                    }
                }
            }

            int validNames = 0;

            foreach (var name in bundleNames)
            {
                var assets = aggregatedGroups[name];
                // we do not build bundle without any asset
                if (assets.Count > 0)
                {
                    validNames += bundleVariants[name].Count;
                }
            }

            AssetBundleBuild[] bundleBuild = new AssetBundleBuild[validNames];

            int bbIndex = 0;

            foreach (var name in bundleNames)
            {
                foreach (var v in bundleVariants[name])
                {
                    var assets = aggregatedGroups[name];

                    if (assets.Count <= 0)
                    {
                        continue;
                    }

                    bundleBuild[bbIndex].assetBundleName    = name;
                    bundleBuild[bbIndex].assetBundleVariant = v;
                    bundleBuild[bbIndex].assetNames         = assets.Where(x => x.variantName == v).Select(x => x.importFrom).ToArray();
                    ++bbIndex;
                }
            }

            if (progressFunc != null)
            {
                progressFunc(node, "Building Asset Bundles...", 0.7f);
            }

            AssetBundleManifest m = BuildPipeline.BuildAssetBundles(bundleOutputDir, bundleBuild, (BuildAssetBundleOptions)node.BundleBuilderBundleOptions[target], target);

            var output = new Dictionary <string, List <AssetReference> >();

            output[key] = new List <AssetReference>();

            var generatedFiles = FileUtility.GetAllFilePathsInFolder(bundleOutputDir);

            // add manifest file
            bundleVariants.Add(BuildTargetUtility.TargetToAssetBundlePlatformName(target).ToLower(), new List <string> {
                null
            });
            foreach (var path in generatedFiles)
            {
                var fileName = path.Substring(bundleOutputDir.Length + 1);
                if (IsFileIntendedItem(fileName, bundleVariants))
                {
                    output[key].Add(AssetReferenceDatabase.GetAssetBundleReference(path));
                }
            }

            if (Output != null)
            {
                var dst = (connectionsToOutput == null || !connectionsToOutput.Any())?
                          null : connectionsToOutput.First();
                Output(dst, output);
            }

            AssetBundleBuildReport.AddBuildReport(new AssetBundleBuildReport(node, m, bundleBuild, output[key], aggregatedGroups, bundleVariants));
        }
        public void Setup(BuildTarget target,
                          NodeData node,
                          IEnumerable <PerformGraph.AssetGroups> incoming,
                          IEnumerable <ConnectionData> connectionsToOutput,
                          PerformGraph.Output Output)
        {
            // BundleBuilder do nothing without incoming connections
            if (incoming == null)
            {
                return;
            }

            var bundleNames    = incoming.SelectMany(v => v.assetGroups.Keys).Distinct().ToList();
            var bundleVariants = new Dictionary <string, List <string> >();

            // get all variant name for bundles
            foreach (var ag in incoming)
            {
                foreach (var name in ag.assetGroups.Keys)
                {
                    if (!bundleVariants.ContainsKey(name))
                    {
                        bundleVariants[name] = new List <string>();
                    }
                    var assets = ag.assetGroups[name];
                    foreach (var a in assets)
                    {
                        var variantName = a.variantName;
                        if (!bundleVariants[name].Contains(variantName))
                        {
                            bundleVariants[name].Add(variantName);
                        }
                    }
                }
            }

            // add manifest file
            var manifestName = BuildTargetUtility.TargetToAssetBundlePlatformName(target);

            bundleNames.Add(manifestName);
            bundleVariants[manifestName] = new List <string>()
            {
                ""
            };

            if (connectionsToOutput != null && Output != null)
            {
                UnityEngine.Assertions.Assert.IsTrue(connectionsToOutput.Any());

                var outputDict = new Dictionary <string, List <AssetReference> >();
                outputDict[key] = new List <AssetReference>();
                var bundleOutputDir = FileUtility.EnsureAssetBundleCacheDirExists(target, node);

                foreach (var name in bundleNames)
                {
                    foreach (var v in bundleVariants[name])
                    {
                        string         bundleName = (string.IsNullOrEmpty(v))? name : name + "." + v;
                        AssetReference bundle     = AssetReferenceDatabase.GetAssetBundleReference(FileUtility.PathCombine(bundleOutputDir, bundleName));
                        AssetReference manifest   = AssetReferenceDatabase.GetAssetBundleReference(FileUtility.PathCombine(bundleOutputDir, bundleName + AssetBundleGraphSettings.MANIFEST_FOOTER));
                        outputDict[key].Add(bundle);
                        outputDict[key].Add(manifest);
                    }
                }

                var dst = (connectionsToOutput == null || !connectionsToOutput.Any())?
                          null : connectionsToOutput.First();
                Output(dst, outputDict);
            }
        }
Пример #10
0
        public void Setup(BuildTarget target,
                          NodeData node,
                          IEnumerable <PerformGraph.AssetGroups> incoming,
                          IEnumerable <ConnectionData> connectionsToOutput,
                          PerformGraph.Output Output)
        {
            ValidatePrefabBuilder(node, target, incoming,
                                  () => {
                throw new NodeException(node.Name + " :PrefabBuilder is not configured. Please configure from Inspector.", node.Id);
            },
                                  () => {
                throw new NodeException(node.Name + " :Failed to create PrefabBuilder from settings. Please fix settings from Inspector.", node.Id);
            },
                                  (string groupKey) => {
                throw new NodeException(string.Format("{0} :Can not create prefab with incoming assets for group {1}.", node.Name, groupKey), node.Id);
            },
                                  (AssetReference badAsset) => {
                throw new NodeException(string.Format("{0} :Can not import incoming asset {1}.", node.Name, badAsset.fileNameAndExtension), node.Id);
            }
                                  );

            if (incoming == null)
            {
                return;
            }

            var builder = PrefabBuilderUtility.CreatePrefabBuilder(node, target);

            UnityEngine.Assertions.Assert.IsNotNull(builder);


            var prefabOutputDir = FileUtility.EnsurePrefabBuilderCacheDirExists(target, node);
            Dictionary <string, List <AssetReference> > output = null;

            if (Output != null)
            {
                output = new Dictionary <string, List <AssetReference> >();
            }

            var aggregatedGroups = new Dictionary <string, List <AssetReference> >();

            foreach (var ag in incoming)
            {
                foreach (var key in ag.assetGroups.Keys)
                {
                    if (!aggregatedGroups.ContainsKey(key))
                    {
                        aggregatedGroups[key] = new List <AssetReference>();
                    }
                    aggregatedGroups[key].AddRange(ag.assetGroups[key].AsEnumerable());
                }
            }

            foreach (var key in aggregatedGroups.Keys)
            {
                var assets   = aggregatedGroups[key];
                var thresold = PrefabBuilderUtility.GetPrefabBuilderAssetThreshold(node.ScriptClassName);
                if (thresold < assets.Count)
                {
                    var guiName = PrefabBuilderUtility.GetPrefabBuilderGUIName(node.ScriptClassName);
                    throw new NodeException(string.Format("{0} :Too many assets passed to {1} for group:{2}. {3}'s threshold is set to {4}",
                                                          node.Name, guiName, key, guiName, thresold), node.Id);
                }

                List <UnityEngine.Object> allAssets = LoadAllAssets(aggregatedGroups[key]);
                var prefabFileName = builder.CanCreatePrefab(key, allAssets);
                if (output != null && prefabFileName != null)
                {
                    output[key] = new List <AssetReference> ()
                    {
                        AssetReferenceDatabase.GetPrefabReference(FileUtility.PathCombine(prefabOutputDir, prefabFileName + ".prefab"))
                    };
                }
                allAssets.ForEach(o => Resources.UnloadAsset(o));
            }

            if (Output != null)
            {
                var dst = (connectionsToOutput == null || !connectionsToOutput.Any())?
                          null : connectionsToOutput.First();
                Output(dst, output);
            }
        }
Пример #11
0
        public void Run(BuildTarget target,
                        NodeData node,
                        IEnumerable <PerformGraph.AssetGroups> incoming,
                        IEnumerable <ConnectionData> connectionsToOutput,
                        PerformGraph.Output Output,
                        Action <NodeData, string, float> progressFunc)
        {
            if (incoming == null)
            {
                return;
            }

            var builder = PrefabBuilderUtility.CreatePrefabBuilder(node, target);

            UnityEngine.Assertions.Assert.IsNotNull(builder);

            var prefabOutputDir = FileUtility.EnsurePrefabBuilderCacheDirExists(target, node);
            Dictionary <string, List <AssetReference> > output = null;

            if (Output != null)
            {
                output = new Dictionary <string, List <AssetReference> >();
            }

            var aggregatedGroups = new Dictionary <string, List <AssetReference> >();

            foreach (var ag in incoming)
            {
                foreach (var key in ag.assetGroups.Keys)
                {
                    if (!aggregatedGroups.ContainsKey(key))
                    {
                        aggregatedGroups[key] = new List <AssetReference>();
                    }
                    aggregatedGroups[key].AddRange(ag.assetGroups[key].AsEnumerable());
                }
            }

            foreach (var key in aggregatedGroups.Keys)
            {
                var assets = aggregatedGroups[key];

                var allAssets = LoadAllAssets(assets);

                var prefabFileName = builder.CanCreatePrefab(key, allAssets);
                var prefabSavePath = FileUtility.PathCombine(prefabOutputDir, prefabFileName + ".prefab");

                if (PrefabBuildInfo.DoesPrefabNeedRebuilding(node, target, key, assets))
                {
                    UnityEngine.GameObject obj = builder.CreatePrefab(key, allAssets);
                    if (obj == null)
                    {
                        throw new AssetBundleGraphException(string.Format("{0} :PrefabBuilder {1} returned null in CreatePrefab() [groupKey:{2}]",
                                                                          node.Name, builder.GetType().FullName, key));
                    }

                    LogUtility.Logger.LogFormat(LogType.Log, "{0} is (re)creating Prefab:{1} with {2}({3})", node.Name, prefabFileName,
                                                PrefabBuilderUtility.GetPrefabBuilderGUIName(node.ScriptClassName),
                                                PrefabBuilderUtility.GetPrefabBuilderVersion(node.ScriptClassName));

                    if (progressFunc != null)
                    {
                        progressFunc(node, string.Format("Creating {0}", prefabFileName), 0.5f);
                    }

                    PrefabUtility.CreatePrefab(prefabSavePath, obj, node.ReplacePrefabOptions);
                    PrefabBuildInfo.SavePrefabBuildInfo(node, target, key, assets);
                    GameObject.DestroyImmediate(obj);
                }
                allAssets.ForEach(o => Resources.UnloadAsset(o));

                if (output != null)
                {
                    output[key] = new List <AssetReference> ()
                    {
                        AssetReferenceDatabase.GetPrefabReference(prefabSavePath)
                    };
                }
                aggregatedGroups[key].ForEach(a => a.ReleaseData());
            }

            if (Output != null)
            {
                var dst = (connectionsToOutput == null || !connectionsToOutput.Any())?
                          null : connectionsToOutput.First();
                Output(dst, output);
            }
        }