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);
            }