public void Run()
        {
            EditorPrefs.SetString(BuildUtility.GetLastSettingsKey <EditorPlaySettings>(), m_Id);

            if (m_RunScene == null)
            {
                Debug.LogWarning(string.Format("No run scene linked in settings {0}.", name), this);
                return;
            }

            EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();

            PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup,
                                                             m_Defines);

            EditorUtility.RequestScriptReload();

            m_PreRunEvent.Invoke();
            EditorSceneManager.OpenScene(AssetDatabase.GetAssetPath(m_RunScene));
            EditorApplication.isPlaying = true;
        }
        public void Run(bool deploy)
        {
            PlayerPrefs.SetString(BuildUtility.GetLastSettingsKey <PlayerBuildSettings>(), m_Id);
            AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);

            List <string> scenes = new List <string>(m_Scenes.Length);

            for (int i = 0; i < m_Scenes.Length; i++)
            {
                SceneAsset sceneAsset = m_Scenes[i];

                if (sceneAsset == null)
                {
                    throw new BuildException("Missing scene asset reference in BuildSettings. Has this scene been deleted?");
                }

                scenes.Add(AssetDatabase.GetAssetPath(sceneAsset));
            }

            if (scenes.Count == 0)
            {
                throw new BuildException("No scene asset references in BuildSettings. At least one scene must be added when building a player.");
            }

            BuildOptions buildOptions = BuildOptions.None;

            if (m_IsDevelopment)
            {
                buildOptions |= BuildOptions.Development;
                buildOptions |= BuildOptions.AllowDebugging;
            }

            if (deploy)
            {
                buildOptions |= BuildOptions.AutoRunPlayer;
            }

            var settingsCache = new Dictionary <string, object>();

            try
            {
                PushPlayerSettings(settingsCache);
                PreProcessBuild();

                BuildManifest manifest   = CreateOrUpdateBuildManifest();
                string        outputPath = GetOutputPath(manifest);
                string        buildPath  = PrepareBuildPath(outputPath);
                PushManifestPlayerSettings(settingsCache, manifest);

#if UNITY_2018_1_OR_NEWER
                BuildReport report = BuildPipeline.BuildPlayer(scenes.ToArray(), buildPath, BuildTarget, buildOptions);

                if (report.summary.result == BuildResult.Failed)
                {
                    throw new BuildException("Build player FAILED: BuildResult is Failed");
                }

                if (report.summary.totalErrors > 0 &&
                    m_FailBuildOnErrors)
                {
                    throw new BuildException($"Build player FAILED: {report.summary.totalErrors} error(s)");
                }
#else
                string error = BuildPipeline.BuildPlayer(scenes.ToArray(), buildPath, BuildTarget, buildOptions);

                if (!string.IsNullOrEmpty())
                {
                    throw new BuildException("Build player FAILED: " + error);
                }
#endif

                PostProcessBuild(outputPath);
            }
            finally
            {
                PopPlayerSettings(settingsCache);
            }
        }
 public static void RunAndDeployWithLastSettings()
 {
     BuildUtility.GetLastBuildSettings <PlayerBuildSettings>().RunAndDeploy();
 }
 public static void RunWithLastSettings()
 {
     BuildUtility.GetLastBuildSettings <EditorPlaySettings>().Run();
 }
        /// <summary>
        /// OnGUI is called for rendering and handling GUI events.
        /// This function can be called multiple times per frame (one call per event).
        /// </summary>
        private void OnGUI()
        {
            PlayerBuildSettings lastPlayerBuildSettings = BuildUtility.GetLastBuildSettings <PlayerBuildSettings>();
            EditorPlaySettings  lastEditorPlaySettings  = BuildUtility.GetLastBuildSettings <EditorPlaySettings>();
            string lastPlayerName = lastPlayerBuildSettings == null ? string.Empty : lastPlayerBuildSettings.name;
            string lastEditorName = lastEditorPlaySettings == null ? string.Empty : lastEditorPlaySettings.name;

            if (lastPlayerBuildSettings != null)
            {
                if (GUILayout.Button("Build: " + lastPlayerName))
                {
                    lastPlayerBuildSettings.Run();
                    return;
                }

                if (GUILayout.Button("Build and Run: " + lastPlayerName))
                {
                    lastPlayerBuildSettings.RunAndDeploy();
                    return;
                }
            }
            else
            {
            }

            if (lastEditorPlaySettings != null)
            {
                if (GUILayout.Button("Play: " + lastEditorName))
                {
                    lastEditorPlaySettings.Run();
                    return;
                }
            }
            else
            {
            }

            PlayerBuildSettings[] playerBuildSettings = AssetDatabase.FindAssets("t:PlayerBuildSettings")
                                                        .Select <string, string>(AssetDatabase.GUIDToAssetPath)
                                                        .Select <string, PlayerBuildSettings>(AssetDatabase.LoadAssetAtPath <PlayerBuildSettings>)
                                                        .ToArray();

            if (playerBuildSettings.Length == 0)
            {
                EditorGUILayout.TextArea("No editor play settings found. To create a settings object right click a location in the Project window and select Create->Build Settings->Editor Play");
            }
            else
            {
                GUILayout.Label("Players", _guiStyleHeading);

                for (int i = 0; i < playerBuildSettings.Length; i++)
                {
                    PlayerBuildSettings setting = playerBuildSettings[i];

                    GUILayout.BeginHorizontal();
                    GUILayout.Label(setting.name);

                    if (GUILayout.Button("Build", _SettingsButtonLayoutOptions))
                    {
                        setting.Run();
                        return;
                    }

                    if (GUILayout.Button("Build and Run", _SettingsButtonLayoutOptions))
                    {
                        setting.RunAndDeploy();
                        return;
                    }

                    if (GUILayout.Button("?", _SelectButtonLayoutOptions))
                    {
                        Selection.activeObject = setting;
                    }

                    GUILayout.EndHorizontal();
                }
            }


            EditorPlaySettings[] editorPlaySettings = AssetDatabase.FindAssets("t:EditorPlaySettings")
                                                      .Select <string, string>(AssetDatabase.GUIDToAssetPath)
                                                      .Select <string, EditorPlaySettings>(AssetDatabase.LoadAssetAtPath <EditorPlaySettings>)
                                                      .ToArray();

            if (editorPlaySettings.Length == 0)
            {
                EditorGUILayout.TextArea("No editor play settings found. To create a settings object right click a location in the Project window and select Create->Build Settings->Editor Play");
            }
            else
            {
                GUILayout.Label("Editor Play", _guiStyleHeading);

                for (int i = 0; i < editorPlaySettings.Length; i++)
                {
                    EditorPlaySettings setting = editorPlaySettings[i];

                    GUILayout.BeginHorizontal();

                    GUILayout.Label(setting.name);
                    if (GUILayout.Button("Play", _SettingsButtonLayoutOptions))
                    {
                        setting.Run();
                        return;
                    }

                    if (GUILayout.Button("?", _SelectButtonLayoutOptions))
                    {
                        Selection.activeObject = setting;
                    }

                    GUILayout.EndHorizontal();
                }
            }
        }