internal virtual void BuiltInResourcesToDependenciesMap(string[] resourcePaths)
        {
            for (int sceneIndex = 0; sceneIndex < resourcePaths.Length; ++sceneIndex)
            {
                string path = resourcePaths[sceneIndex];
                if (EditorUtility.DisplayCancelableProgressBar("Generating built-in resource dependency map",
                                                               "Checking " + path + " for duplicates with Addressables content.",
                                                               (float)sceneIndex / resourcePaths.Length))
                {
                    m_ResourcesToDependencies.Clear();
                    EditorUtility.ClearProgressBar();
                    return;
                }
                string[] dependencies;
                if (path.EndsWith(".unity"))
                {
#if UNITY_2019_3_OR_NEWER
                    using (var w = new BuildInterfacesWrapper())
                    {
                        var           usageTags = new BuildUsageTagSet();
                        BuildSettings settings  = new BuildSettings
                        {
                            group      = EditorUserBuildSettings.selectedBuildTargetGroup,
                            target     = EditorUserBuildSettings.activeBuildTarget,
                            typeDB     = null,
                            buildFlags = ContentBuildFlags.None
                        };

                        SceneDependencyInfo sceneInfo =
                            ContentBuildInterface.CalculatePlayerDependenciesForScene(path, settings, usageTags);
                        dependencies = new string[sceneInfo.referencedObjects.Count];
                        for (int i = 0; i < sceneInfo.referencedObjects.Count; ++i)
                        {
                            if (string.IsNullOrEmpty(sceneInfo.referencedObjects[i].filePath))
                            {
                                dependencies[i] = AssetDatabase.GUIDToAssetPath(sceneInfo.referencedObjects[i].guid.ToString());
                            }
                            else
                            {
                                dependencies[i] = sceneInfo.referencedObjects[i].filePath;
                            }
                        }
                    }
#else
                    HashSet <string> assetPaths = new HashSet <string>();
                    assetPaths.Add(path);
                    var s = EditorSceneManager.OpenScene(path, OpenSceneMode.Additive);
                    List <UnityEngine.Object> roots = new List <UnityEngine.Object>(s.GetRootGameObjects());

                    var sceneHierarchyStack = new Stack <GameObject>();
                    for (int i = roots.Count - 1; i >= 0; --i)
                    {
                        GameObject go = (GameObject)roots[i];
                        if (go.CompareTag("EditorOnly"))
                        {
                            UnityEngine.Object.DestroyImmediate(roots[i]);
                            roots.RemoveAt(i);
                        }
                        else
                        {
                            sceneHierarchyStack.Push(go);
                        }
                    }

                    while (sceneHierarchyStack.Count > 0)
                    {
                        var item = sceneHierarchyStack.Pop();
                        for (int i = 0; i < item.transform.childCount; ++i)
                        {
                            GameObject go = item.transform.GetChild(i).gameObject;
                            if (go.CompareTag("EditorOnly"))
                            {
                                UnityEngine.Object.DestroyImmediate(go);
                            }
                            else
                            {
                                sceneHierarchyStack.Push(go);
                            }
                        }
                    }

                    UnityEngine.Object[] deps = EditorUtility.CollectDependencies(roots.ToArray());
                    foreach (UnityEngine.Object o in deps)
                    {
                        string p = AssetDatabase.GetAssetPath(o.GetInstanceID());
                        if (!string.IsNullOrEmpty(p))
                        {
                            assetPaths.Add(p);
                        }
                    }

                    EditorSceneManager.CloseScene(s, true);
                    dependencies = assetPaths.ToArray();
#endif
                }
                else
                {
                    dependencies = AssetDatabase.GetDependencies(path);
                }

                if (!m_ResourcesToDependencies.ContainsKey(path))
                {
                    m_ResourcesToDependencies.Add(path, new List <GUID>(dependencies.Length));
                }
                else
                {
                    m_ResourcesToDependencies[path].Capacity += dependencies.Length;
                }

                foreach (string dependency in dependencies)
                {
                    if (dependency.EndsWith(".cs") || dependency.EndsWith(".dll"))
                    {
                        continue;
                    }
                    m_ResourcesToDependencies[path].Add(new GUID(AssetDatabase.AssetPathToGUID(dependency)));
                }
            }
            EditorUtility.ClearProgressBar();
        }
예제 #2
0
        /// <summary>
        /// Default implementation of generating Asset Bundles using the Scriptable Build Pipeline.
        /// </summary>
        /// <param name="parameters">Set of parameters used for building asset bundles.</param>
        /// <param name="content">Set of content and explicit asset bundle layout to build.</param>
        /// <param name="result">Results from building the content and explicit asset bundle layout.</param>
        /// <param name="taskList">Custom task list for building asset bundles.</param>
        /// <param name="contextObjects">Additional context objects to make available to the build.</param>
        /// <returns>Return code with status information about success or failure causes.</returns>
        public static ReturnCode BuildAssetBundles(IBundleBuildParameters parameters, IBundleBuildContent content, out IBundleBuildResults result, IList <IBuildTask> taskList, params IContextObject[] contextObjects)
        {
            // Avoid throwing exceptions in here as we don't want them bubbling up to calling user code
            if (parameters == null)
            {
                result = null;
                BuildLogger.LogException(new ArgumentNullException("parameters"));
                return(ReturnCode.Exception);
            }

            // Avoid throwing exceptions in here as we don't want them bubbling up to calling user code
            if (taskList.IsNullOrEmpty())
            {
                result = null;
                BuildLogger.LogException(new ArgumentException("Argument cannot be null or empty.", "taskList"));
                return(ReturnCode.Exception);
            }

            // Don't run if there are unsaved changes
            if (ValidationMethods.HasDirtyScenes())
            {
                result = null;
                return(ReturnCode.UnsavedChanges);
            }

            AssetDatabase.SaveAssets();

            ReturnCode exitCode;

            result = new BundleBuildResults();

            using (var interfacesWrapper = new BuildInterfacesWrapper())
#if !CI_TESTRUNNER_PROJECT
                using (new SceneStateCleanup())
                    using (var progressTracker = new ProgressTracker())
#else
                using (var progressTracker = new ProgressLoggingTracker())
#endif
                        using (var buildCache = new BuildCache(parameters.CacheServerHost, parameters.CacheServerPort))
                        {
                            Directory.CreateDirectory(parameters.TempOutputFolder);

                            BuildContext buildContext;
                            BuildLog     buildLog = null;

                            try
                            {
                                buildContext = new BuildContext(contextObjects);
                                buildContext.SetContextObject(parameters);
                                buildContext.SetContextObject(content);
                                buildContext.SetContextObject(result);
                                buildContext.SetContextObject(interfacesWrapper);
                                buildContext.SetContextObject(progressTracker);
                                buildContext.SetContextObject(buildCache);
                                // If IDeterministicIdentifiers was passed in with contextObjects, don't add the default
                                if (!buildContext.ContainsContextObject(typeof(IDeterministicIdentifiers)))
                                {
                                    buildContext.SetContextObject(new Unity5PackedIdentifiers());
                                }
                                buildContext.SetContextObject(new BuildDependencyData());
                                buildContext.SetContextObject(new BundleWriteData());
                                buildContext.SetContextObject(BuildCallbacks);

                                IBuildLogger logger;
                                if (!buildContext.TryGetContextObject <IBuildLogger>(out logger))
                                {
                                    logger = buildLog = new BuildLog();
                                    buildContext.SetContextObject(buildLog);
                                }
                                buildCache.SetBuildLogger(logger);
                            }
                            catch (Exception e)
                            {
                                // Avoid throwing exceptions in here as we don't want them bubbling up to calling user code
                                result = null;
                                BuildLogger.LogException(e);
                                return(ReturnCode.Exception);
                            }

                            exitCode = BuildTasksRunner.Validate(taskList, buildContext);
                            if (exitCode >= ReturnCode.Success)
#if SBP_PROFILER_ENABLE
                            { exitCode = BuildTasksRunner.RunProfiled(taskList, buildContext); }
#else
                            { exitCode = BuildTasksRunner.Run(taskList, buildContext); }
#endif

                            if (Directory.Exists(parameters.TempOutputFolder))
                            {
                                Directory.Delete(parameters.TempOutputFolder, true);
                            }

                            if (buildLog != null)
                            {
                                string buildLogPath = parameters.GetOutputFilePathForIdentifier("buildlog.txt");
                                Directory.CreateDirectory(Path.GetDirectoryName(buildLogPath));
                                File.WriteAllText(buildLogPath, buildLog.FormatAsText());
                                File.WriteAllText(parameters.GetOutputFilePathForIdentifier("buildlogtep.json"), buildLog.FormatAsTraceEventProfiler());
                            }
                        }


            long maximumCacheSize = ScriptableBuildPipeline.maximumCacheSize * 1073741824L; // gigabytes to bytes
            ThreadPool.QueueUserWorkItem(PruneCache, maximumCacheSize);
            return(exitCode);
        }
예제 #3
0
        /// <summary>
        /// Build map of resources to corresponding dependencies
        /// </summary>
        /// <param name="resourcePaths"> Array of resource paths</param>
        protected internal virtual void BuiltInResourcesToDependenciesMap(string[] resourcePaths)
        {
            for (int sceneIndex = 0; sceneIndex < resourcePaths.Length; ++sceneIndex)
            {
                string path = resourcePaths[sceneIndex];
                if (EditorUtility.DisplayCancelableProgressBar("Generating built-in resource dependency map",
                                                               "Checking " + path + " for duplicates with Addressables content.",
                                                               (float)sceneIndex / resourcePaths.Length))
                {
                    m_ResourcesToDependencies.Clear();
                    EditorUtility.ClearProgressBar();
                    return;
                }
                string[] dependencies;
                if (path.EndsWith(".unity"))
                {
                    using (var w = new BuildInterfacesWrapper())
                    {
                        var           usageTags = new BuildUsageTagSet();
                        BuildSettings settings  = new BuildSettings
                        {
                            group      = EditorUserBuildSettings.selectedBuildTargetGroup,
                            target     = EditorUserBuildSettings.activeBuildTarget,
                            typeDB     = null,
                            buildFlags = ContentBuildFlags.None
                        };

                        SceneDependencyInfo sceneInfo =
                            ContentBuildInterface.CalculatePlayerDependenciesForScene(path, settings, usageTags);
                        dependencies = new string[sceneInfo.referencedObjects.Count];
                        for (int i = 0; i < sceneInfo.referencedObjects.Count; ++i)
                        {
                            if (string.IsNullOrEmpty(sceneInfo.referencedObjects[i].filePath))
                            {
                                dependencies[i] = AssetDatabase.GUIDToAssetPath(sceneInfo.referencedObjects[i].guid.ToString());
                            }
                            else
                            {
                                dependencies[i] = sceneInfo.referencedObjects[i].filePath;
                            }
                        }
                    }
                }
                else
                {
                    dependencies = AssetDatabase.GetDependencies(path);
                }

                if (!m_ResourcesToDependencies.ContainsKey(path))
                {
                    m_ResourcesToDependencies.Add(path, new List <GUID>(dependencies.Length));
                }
                else
                {
                    m_ResourcesToDependencies[path].Capacity += dependencies.Length;
                }

                foreach (string dependency in dependencies)
                {
                    if (dependency.EndsWith(".cs") || dependency.EndsWith(".dll"))
                    {
                        continue;
                    }
                    m_ResourcesToDependencies[path].Add(new GUID(AssetDatabase.AssetPathToGUID(dependency)));
                }
            }
            EditorUtility.ClearProgressBar();
        }