internal static BuildPlayerOptions GetBuildPlayerOptionsInternal(bool askForBuildLocation, BuildPlayerOptions defaultBuildPlayerOptions)
            {
                var options = defaultBuildPlayerOptions;

                bool updateExistingBuild = false;

                BuildTarget      buildTarget      = EditorUserBuildSettingsUtils.CalculateSelectedBuildTarget();
                BuildTargetGroup buildTargetGroup = EditorUserBuildSettings.selectedBuildTargetGroup;
                int subtarget = EditorUserBuildSettings.GetSelectedSubtargetFor(buildTarget);

                // Pick location for the build
                string newLocation = "";

                //Check if Lz4 is supported for the current buildtargetgroup and enable it if need be
                if (PostprocessBuildPlayer.SupportsLz4Compression(buildTargetGroup, buildTarget))
                {
                    var compression = EditorUserBuildSettings.GetCompressionType(buildTargetGroup);
                    if (compression < 0)
                    {
                        compression = PostprocessBuildPlayer.GetDefaultCompression(buildTargetGroup, buildTarget);
                    }
                    if (compression == Compression.Lz4)
                    {
                        options.options |= BuildOptions.CompressWithLz4;
                    }
                    else if (compression == Compression.Lz4HC)
                    {
                        options.options |= BuildOptions.CompressWithLz4HC;
                    }
                }

                bool developmentBuild = EditorUserBuildSettings.development;

                if (developmentBuild)
                {
                    options.options |= BuildOptions.Development;
                }
                if (EditorUserBuildSettings.allowDebugging && developmentBuild)
                {
                    options.options |= BuildOptions.AllowDebugging;
                }
                if (EditorUserBuildSettings.symlinkSources)
                {
                    options.options |= BuildOptions.SymlinkSources;
                }
                if (EditorUserBuildSettings.connectProfiler && (developmentBuild || buildTarget == BuildTarget.WSAPlayer))
                {
                    options.options |= BuildOptions.ConnectWithProfiler;
                }
                if (EditorUserBuildSettings.buildWithDeepProfilingSupport && developmentBuild)
                {
                    options.options |= BuildOptions.EnableDeepProfilingSupport;
                }
                if (EditorUserBuildSettings.buildScriptsOnly)
                {
                    options.options |= BuildOptions.BuildScriptsOnly;
                }
                if (!string.IsNullOrEmpty(ProfilerUserSettings.customConnectionID) && developmentBuild)
                {
                    options.options |= BuildOptions.CustomConnectionID;
                }


                if (IsInstallInBuildFolderOption())
                {
                    options.options |= BuildOptions.InstallInBuildFolder;
                }
                else if ((options.options & BuildOptions.PatchPackage) == 0)
                {
                    if (askForBuildLocation && !PickBuildLocation(buildTargetGroup, buildTarget, subtarget, options.options, out updateExistingBuild))
                    {
                        throw new BuildMethodException();
                    }

                    newLocation = EditorUserBuildSettings.GetBuildLocation(buildTarget);

                    if (newLocation.Length == 0)
                    {
                        throw new BuildMethodException("Build location for buildTarget " + buildTarget + " is not valid.");
                    }

                    if (!askForBuildLocation)
                    {
                        switch (BuildPipeline.BuildCanBeAppended(buildTarget, newLocation))
                        {
                        case CanAppendBuild.Unsupported:
                            break;

                        case CanAppendBuild.Yes:
                            updateExistingBuild = true;
                            break;

                        case CanAppendBuild.No:
                            if (!PickBuildLocation(buildTargetGroup, buildTarget, subtarget, options.options, out updateExistingBuild))
                            {
                                throw new BuildMethodException();
                            }

                            newLocation = EditorUserBuildSettings.GetBuildLocation(buildTarget);
                            if (!BuildLocationIsValid(newLocation))
                            {
                                throw new BuildMethodException("Build location for buildTarget " + buildTarget + " is not valid.");
                            }

                            break;
                        }
                    }
                }

                if (updateExistingBuild)
                {
                    options.options |= BuildOptions.AcceptExternalModificationsToPlayer;
                }

                options.target                  = buildTarget;
                options.subtarget               = subtarget;
                options.targetGroup             = buildTargetGroup;
                options.locationPathName        = EditorUserBuildSettings.GetBuildLocation(buildTarget);
                options.assetBundleManifestPath = PostprocessBuildPlayer.GetStreamingAssetsBundleManifestPath();

                // Build a list of scenes that are enabled
                ArrayList scenesList = new ArrayList();

                EditorBuildSettingsScene[] editorScenes = EditorBuildSettings.scenes;
                foreach (EditorBuildSettingsScene scene in editorScenes)
                {
                    if (scene.enabled)
                    {
                        scenesList.Add(scene.path);
                    }
                }

                options.scenes = scenesList.ToArray(typeof(string)) as string[];

                return(options);
            }
Example #2
0
            /// <summary>
            /// Default implementation of the build player method.
            /// </summary>
            /// <param name="options"></param>
            public static void BuildPlayer(BuildPlayerOptions options)
            {
                if (!UnityConnect.instance.canBuildWithUPID)
                {
                    if (!EditorUtility.DisplayDialog("Missing Project ID", "Because you are not a member of this project this build will not access Unity services.\nDo you want to continue?", "Yes", "No"))
                    {
                        throw new BuildMethodException();
                    }
                }

                if (!BuildPipeline.IsBuildTargetSupported(options.targetGroup, options.target))
                {
                    throw new BuildMethodException("Build target is not supported.");
                }

                string module = ModuleManager.GetTargetStringFrom(EditorUserBuildSettings.selectedBuildTargetGroup, options.target);
                IBuildWindowExtension buildWindowExtension = ModuleManager.GetBuildWindowExtension(module);

                if (buildWindowExtension != null && (options.options & BuildOptions.AutoRunPlayer) != 0 && !buildWindowExtension.EnabledBuildAndRunButton())
                {
                    throw new BuildMethodException();
                }

                if (Unsupported.IsBleedingEdgeBuild())
                {
                    var sb = new System.Text.StringBuilder();
                    sb.AppendLine("This version of Unity is a BleedingEdge build that has not seen any manual testing.");
                    sb.AppendLine("You should consider this build unstable.");
                    sb.AppendLine("We strongly recommend that you use a normal version of Unity instead.");

                    if (EditorUtility.DisplayDialog("BleedingEdge Build", sb.ToString(), "Cancel", "OK"))
                    {
                        throw new BuildMethodException();
                    }
                }

                // See if we need to switch platforms and delay the build.  We do this whenever
                // we're trying to build for a target different from the active one so as to ensure
                // that the compiled script code we have loaded is built for the same platform we
                // are building for.  As we can't reload while our editor stuff is still executing,
                // we need to defer to after the next script reload then.
                bool delayToAfterScriptReload = false;

                if (EditorUserBuildSettings.activeBuildTarget != options.target ||
                    EditorUserBuildSettings.activeBuildTargetGroup != options.targetGroup)
                {
                    if (!EditorUserBuildSettings.SwitchActiveBuildTargetAsync(options.targetGroup, options.target))
                    {
                        // Switching the build target failed.  No point in trying to continue
                        // with a build.
                        var errStr = string.Format("Could not switch to build target '{0}', '{1}'.",
                                                   BuildPipeline.GetBuildTargetGroupDisplayName(options.targetGroup),
                                                   BuildPlatforms.instance.GetBuildTargetDisplayName(options.targetGroup, options.target, options.subtarget));
                        throw new BuildMethodException(errStr);
                    }

                    if (EditorApplication.isCompiling)
                    {
                        delayToAfterScriptReload = true;
                    }
                }

                bool locationPathExistedBeforeBuild = System.IO.Directory.Exists(options.locationPathName);
                // Trigger build.
                // Note: report will be null, if delayToAfterScriptReload = true
                var report = BuildPipeline.BuildPlayerInternalNoCheck(options.scenes, options.locationPathName, PostprocessBuildPlayer.GetStreamingAssetsBundleManifestPath(), options.targetGroup, options.target, options.subtarget, options.options, options.extraScriptingDefines, delayToAfterScriptReload);

                if (report != null
                    )
                {
                    var resultStr = String.Format("Build completed with a result of '{0}' in {1} seconds ({2} ms)",
                                                  report.summary.result.ToString("g"),
                                                  Convert.ToInt32(report.summary.totalTime.TotalSeconds),
                                                  Convert.ToInt32(report.summary.totalTime.TotalMilliseconds));

                    switch (report.summary.result)
                    {
                    case Build.Reporting.BuildResult.Unknown:
                        Debug.LogWarning(resultStr);
                        break;

                    case Build.Reporting.BuildResult.Failed:
                        //  On some platforms the user creates the build folder, therefore they own the folder and
                        // it should not be automatically deleted by the Unity Editor, even if it is empty (case 1073851)
                        if (options.target != BuildTarget.XboxOne && !locationPathExistedBeforeBuild)
                        {
                            DeleteBuildFolderIfEmpty(report.summary.outputPath);
                        }
                        Debug.LogError(resultStr);
                        throw new BuildMethodException(report.SummarizeErrors());

                    default:
                        Debug.Log(resultStr);
                        break;
                    }

                    buildCompletionHandler?.Invoke(report);
                }
            }