Beispiel #1
0
        /// <summary>
        /// Build tile system and apply stripping rules.
        /// </summary>
        /// <param name="system">Tile system.</param>
        /// <param name="progressHandler">Progress handler delegate.</param>
        /// <returns>Tile system builder that was used to build tile system.</returns>
        private static TileSystemBuilder BuildTileSystemHelper(TileSystem system, ProgressDelegate progressHandler = null)
        {
            var builder = new TileSystemBuilder(progressHandler);

            BuildTileSystemHelper(builder, system);
            return(builder);
        }
Beispiel #2
0
        /// <summary>
        /// Build tile system and apply stripping rules.
        /// </summary>
        /// <param name="builder">Tile system builder.</param>
        /// <param name="system">Tile system.</param>
        private static void BuildTileSystemHelper(TileSystemBuilder builder, TileSystem system)
        {
            builder.SetContext(system);

            if (PrepareTileSystem != null)
            {
                PrepareTileSystem(builder);
            }

            builder.Build(system);

            if (FinalizeTileSystem != null)
            {
                FinalizeTileSystem(builder);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Build all tile systems in scene.
        /// </summary>
        /// <remarks>
        /// <para>Each tile system in scene is built in sequence and then the built
        /// version of the scene is saved. Various user interfaces will appear during
        /// the build process.</para>
        /// </remarks>
        public static void BuildScene()
        {
            if (!EditorUtility.DisplayDialog(
                    TileLang.ParticularText("Action", "Build Scene"),
                    TileLang.Text("Scene must be saved before tile systems can be built.\n\nWould you like to save the scene now?"),
                    TileLang.ParticularText("Action", "Save and Proceed"),
                    TileLang.ParticularText("Action", "Cancel")
                    ))
            {
                //EditorUtility.DisplayDialog(ProductInfo.name, "Could not build scene because it was not saved.", "Close");
                return;
            }

            // Force user to save scene
            if (!EditorSceneManager.SaveOpenScenes())
            {
                //EditorUtility.DisplayDialog(ProductInfo.name, "Could not build scene because it was not saved.", "Close");
                return;
            }

            string originalScenePath = EditorApplication.currentScene;

            if (!EditorUtility.DisplayDialog(
                    string.Format(
                        /* 0: path of scene */
                        TileLang.Text("Building Scene '{0}'"),
                        originalScenePath
                        ),
                    TileLang.Text("Open scene was saved successfully.\n\nProceed to save built variation of scene."),
                    TileLang.ParticularText("Action", "Proceed"),
                    TileLang.ParticularText("Action", "Cancel")
                    ))
            {
                Debug.Log(TileLang.Text("Building of tile systems was cancelled."));
                return;
            }

            // Prompt user to save built scene.
            string outputPath = SaveBuildSceneAs();

            if (outputPath == null)
            {
                return;
            }

            // Save output scene straight away!
            if (!EditorApplication.SaveScene(outputPath))
            {
                EditorUtility.DisplayDialog(
                    TileLang.Text("Error"),
                    string.Format(
                        /* 0: path to output asset */
                        TileLang.ParticularText("Error", "Was unable to save output scene '{0}'"),
                        outputPath
                        ),
                    TileLang.ParticularText("Action", "Close")
                    );
                return;
            }

            if (BuildSceneStart != null)
            {
                BuildSceneStart();
            }

            bool hasErrors = false;

            // Fetch all tile systems in scene.
            TileSystem[] systems = GameObject.FindObjectsOfType <TileSystem>();

            float overallTaskCount  = 1f;
            float overallTaskOffset = 0f;
            float overallTaskRatio  = 0f;

            TileSystemBuilder builder = new TileSystemBuilder();

            builder.progressHandler = delegate(string title, string status, float percentage) {
                float progress = (builder.Task + overallTaskOffset) * overallTaskRatio;
                return(EditorUtility.DisplayCancelableProgressBar(title, status, progress));
            };

            // Count tasks in advance.
            foreach (TileSystem system in systems)
            {
                // Skip non-editable tile systems and output warning message to console.
                if (!system.IsEditable)
                {
                    Debug.LogWarning(string.Format(
                                         /* 0: name of tile system */
                                         TileLang.Text("Skipping non-editable tile system '{0}'."),
                                         system.name
                                         ), system);
                    continue;
                }

                overallTaskCount += builder.CountTasks(system);
            }

            overallTaskRatio = 1f / overallTaskCount;

            try {
                // Build each tile system in turn.
                foreach (TileSystem system in systems)
                {
                    // Skip non-editable tile systems.
                    if (!system.IsEditable)
                    {
                        continue;
                    }

                    BuildTileSystemHelper(builder, system);

                    // Adjust overall task offset.
                    overallTaskOffset += builder.TaskCount;
                }

                builder.progressHandler(
                    TileLang.Text("Building tile systems"),
                    TileLang.Text("Cleaning up..."),
                    progress: (overallTaskCount - 1f) * overallTaskRatio
                    );
                builder = null;

                UnityEngine.Resources.UnloadUnusedAssets();
                GC.Collect();
            }
            catch (Exception ex) {
                Debug.LogError(ex.ToString());
                hasErrors = true;
            }
            finally {
                // Finished with progress bar!
                EditorUtility.ClearProgressBar();
            }

            // Save output scene.
            if (!EditorApplication.SaveScene(outputPath))
            {
                Debug.LogError(string.Format(
                                   /* 0: scene file path */
                                   TileLang.ParticularText("Error", "Was unable to save output scene '{0}'"),
                                   outputPath
                                   ));
            }

            // Raise event for end of build process.
            if (BuildSceneComplete != null)
            {
                BuildSceneComplete();
            }

            // Rollback changes.
            EditorApplication.OpenScene(originalScenePath);

            if (hasErrors)
            {
                EditorUtility.DisplayDialog(
                    TileLang.Text("Warning"),
                    TileLang.Text("Errors were encountered whilst building scene. Please check console for any additional information."),
                    TileLang.ParticularText("Action", "Close")
                    );
            }
        }