Exemple #1
0
        public bool Build(PostBuildOption option)
        {
            try
            {
                var settings = this.target as BuildSettings;
                var scenes   = this.GetScenePaths();

                //set version
                settings.Version.Build++;
                EditorUtility.SetDirty(settings);
                PlayerSettings.bundleVersion = settings.Version.ToString();
                AssetDatabase.SaveAssets();

                //get output directory
                var    dir = EditorProjectPrefs.Local.GetString("LastBuildDirectory", string.Empty);
                string path;
                switch (settings.BuildTarget)
                {
                case BuildTarget.StandaloneWindows:
                case BuildTarget.StandaloneWindows64:
                    path = EditorUtility.SaveFilePanel("Build", dir, Application.productName + ".exe", "exe");
                    break;

                case BuildTarget.StandaloneLinux:
                case BuildTarget.StandaloneLinuxUniversal:
                    path = EditorUtility.SaveFilePanel("Build", dir, Application.productName + ".x86", "x86");
                    break;

                case BuildTarget.StandaloneLinux64:
                    path = EditorUtility.SaveFilePanel("Build", dir, Application.productName + ".x86_64", "x86_64");
                    break;

                case BuildTarget.StandaloneOSXIntel:
                case BuildTarget.StandaloneOSXIntel64:
                case BuildTarget.StandaloneOSXUniversal:
                    path = EditorUtility.SaveFilePanel("Build", dir, Application.productName + ".app", "app");
                    break;

                default:
                    path = EditorUtility.SaveFilePanel("Build", dir, Application.productName, "");
                    break;
                }

                //build
                if (!string.IsNullOrEmpty(path))
                {
                    EditorProjectPrefs.Local.SetString("LastBuildDirectory", System.IO.Path.GetDirectoryName(path));

                    if (settings.InputSettings != null)
                    {
                        var copy = InputSettings.LoadGlobalInputSettings(false);
                        settings.InputSettings.ApplyToGlobal();

                        BuildPipeline.BuildPlayer(scenes, path, settings.BuildTarget, settings.BuildOptions);

                        copy.ApplyToGlobal();
                    }
                    else
                    {
                        BuildPipeline.BuildPlayer(scenes, path, settings.BuildTarget, settings.BuildOptions);
                    }

                    if ((option & PostBuildOption.OpenFolder) != 0)
                    {
                        EditorUtility.RevealInFinder(path);
                    }
                    if ((option & PostBuildOption.Run) != 0)
                    {
                        var proc = new System.Diagnostics.Process();
                        proc.StartInfo.FileName = path;
                        proc.Start();
                    }

                    return(true);
                }
            }
            catch (System.Exception ex)
            {
                Debug.LogException(ex);
            }

            return(false);
        }
        public virtual bool Build(string path, PostBuildOption option)
        {
            try
            {
                AssetDatabase.SaveAssets();

                var scenes     = this.GetScenePaths();
                var buildGroup = BuildPipeline.GetBuildTargetGroup(this.BuildTarget);

                //set version
                this.Version.Build++;
                EditorUtility.SetDirty(this);
                PlayerSettings.bundleVersion = this.Version.ToString();
                AssetDatabase.SaveAssets();

                //build
                if (!string.IsNullOrEmpty(path))
                {
                    //save last build directory
                    EditorProjectPrefs.Local.SetString("LastBuildDirectory", System.IO.Path.GetDirectoryName(path));


                    //do build
                    InputSettings cacheInputs  = null;
                    string        cacheSymbols = null;
                    Dictionary <BuildSettings.PlayerSettingOverride, object> cachePlayerSettings = null;

                    if (this.InputSettings != null)
                    {
                        cacheInputs = InputSettings.LoadGlobalInputSettings(false);
                        this.InputSettings.ApplyToGlobal();
                    }
                    if (this.DefineSymbols)
                    {
                        cacheSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(buildGroup) ?? string.Empty;
                        PlayerSettings.SetScriptingDefineSymbolsForGroup(buildGroup, this.Symbols);
                    }

                    if (_playerSettingOverrides.Count > 0)
                    {
                        cachePlayerSettings = new Dictionary <PlayerSettingOverride, object>();
                        foreach (var setting in _playerSettingOverrides)
                        {
                            if (setting.SettingInfo != null)
                            {
                                cachePlayerSettings[setting] = setting.SettingInfo.GetValue(null, null);
                                try
                                {
                                    setting.SettingInfo.SetValue(null, setting.SettingValue, null);
                                }
                                catch (System.Exception)
                                { }
                            }
                        }
                    }

                    var report = BuildPipeline.BuildPlayer(scenes, path, this.BuildTarget, this.BuildOptions);

                    if (cacheInputs != null)
                    {
                        cacheInputs.ApplyToGlobal();
                    }
                    if (cacheSymbols != null)
                    {
                        PlayerSettings.SetScriptingDefineSymbolsForGroup(buildGroup, cacheSymbols);
                    }
                    if (cachePlayerSettings != null)
                    {
                        //loop backwards when resetting from cache
                        for (int i = _playerSettingOverrides.Count - 1; i >= 0; i--)
                        {
                            var setting = _playerSettingOverrides[i];
                            if (setting.SettingInfo != null)
                            {
                                try
                                {
                                    setting.SettingInfo.SetValue(null, cachePlayerSettings[setting], null);
                                }
                                catch (System.Exception)
                                { }
                            }
                        }
                    }

                    EditorUtility.SetDirty(this);
                    AssetDatabase.SaveAssets();

                    if (report != null && report.summary.result == UnityEditor.Build.Reporting.BuildResult.Succeeded)
                    {
                        //save
                        if ((option & PostBuildOption.OpenFolder) != 0)
                        {
                            EditorUtility.RevealInFinder(path);
                        }
                        if ((option & PostBuildOption.Run) != 0)
                        {
                            var proc = new System.Diagnostics.Process();
                            proc.StartInfo.FileName = path;
                            proc.Start();
                        }

                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            catch (System.Exception ex)
            {
                Debug.LogException(ex);
            }

            return(false);
        }