Пример #1
0
        private IEnumerator CreateDocumentation()
        {
            documentationInProgress = true;

            string converterPath = TravrsalSettingsManager.Get("tiledPath", SDKUtil.TILED_PATH_DEFAULT);

            if (!string.IsNullOrEmpty(converterPath))
            {
                converterPath = Path.GetDirectoryName(converterPath) + "/tmxrasterizer.exe";
            }

            foreach (string dir in GetLevelPaths())
            {
                string levelName = Path.GetFileName(dir);
                string docuPath  = GetDocuPath(levelName);
                try
                {
                    if (Directory.Exists(docuPath))
                    {
                        Directory.Delete(docuPath, true);
                    }
                    Directory.CreateDirectory(docuPath);
                }
                catch
                {
                    EditorUtility.DisplayDialog("Error", $"Could not access documentation directory ({docuPath}). Most likely it is open somewhere in an explorer.", "OK");
                    continue;
                }

                string root = $"Assets/Levels/{levelName}/";

                // fill HTML template
                string id   = AssetDatabase.FindAssets("_LevelDocu")[0];
                string path = AssetDatabase.GUIDToAssetPath(id);
                DirectoryUtil.Copy(Application.dataPath + "/../" + path, docuPath);
                AssetDatabase.Refresh();

                string html = File.ReadAllText(docuPath + "index.html");
                html = html.Replace("{LevelName}", levelName);
                html = html.Replace("{LevelKey}", levelName);
                html = html.Replace("{AppVersion}", Application.version); // FIXME: points to wrong version
                html = html.Replace("{Date}", DateTime.Now.ToString("yyyy-MM-dd HH:mm"));

                foreach (string folder in new[] { "Data", "Images", "Logic", "Materials", "Pieces", "Sceneries", "Audio/Effects", "Audio/Music" })
                {
                    HashSet <string> doneAlready = new HashSet <string>();
                    string[]         assets      = new string[0];
                    string           objects     = "";
                    int    objCount     = 0;
                    string variableName = Path.GetFileName(folder);

                    if (Directory.Exists($"{root}{folder}"))
                    {
                        Directory.CreateDirectory(docuPath + folder);

                        assets = AssetDatabase.FindAssets("*", new[] { $"{root}{folder}" });
                        foreach (string asset in assets)
                        {
                            string assetPath = AssetDatabase.GUIDToAssetPath(asset);
                            if (doneAlready.Contains(assetPath))
                            {
                                continue;
                            }
                            doneAlready.Add(assetPath);
                            if (assetPath.ToLower().EndsWith(".md"))
                            {
                                continue;
                            }

                            string imageLink       = "NoPreview.png";
                            bool   withExtension   = true;
                            bool   generatePreview = false;
                            Type   type            = null;

                            switch (folder)
                            {
                            case "Data":
                                if (!assetPath.ToLower().EndsWith(".tmx"))
                                {
                                    continue;
                                }
                                imageLink = folder + "/" + Path.GetFileName(assetPath) + ".png";
                                TileMapUtil.TileMapToImage(assetPath, docuPath + imageLink, converterPath);
                                variableName  = "Rooms";
                                withExtension = false;
                                break;

                            case "Images":
                                generatePreview = true;
                                type            = typeof(Texture2D);
                                break;

                            case "Materials":
                                if (!assetPath.ToLower().EndsWith(".mat"))
                                {
                                    continue;
                                }
                                withExtension   = false;
                                generatePreview = true;
                                type            = typeof(Material);
                                break;

                            case "Pieces":
                                if (!assetPath.ToLower().EndsWith(".prefab"))
                                {
                                    continue;
                                }
                                withExtension   = false;
                                generatePreview = true;
                                break;

                            case "Sceneries":
                                if (!assetPath.ToLower().EndsWith(".prefab"))
                                {
                                    continue;
                                }
                                withExtension   = false;
                                generatePreview = true;
                                break;

                            case "Audio/Music":
                            case "Audio/Effects":
                                generatePreview = true;
                                type            = typeof(AudioClip);
                                break;
                            }
                            objCount++;

                            if (generatePreview)
                            {
                                GameObject         prefab = null;
                                UnityEngine.Object obj    = null;
                                if (type != null)
                                {
                                    obj = AssetDatabase.LoadAssetAtPath(assetPath, type);
                                }
                                else
                                {
                                    prefab = PrefabUtility.LoadPrefabContents(assetPath);
                                    obj    = prefab;
                                }
                                if (obj != null)
                                {
                                    Texture2D icon = AssetPreview.GetAssetPreview(obj);

                                    while (icon == null && AssetPreview.IsLoadingAssetPreview(obj.GetInstanceID()))
                                    {
                                        Repaint();
                                        yield return(new EditorWaitForSeconds(0.05f));

                                        icon = AssetPreview.GetAssetPreview(obj);
                                    }
                                    if (prefab != null)
                                    {
                                        PrefabUtility.UnloadPrefabContents(prefab);
                                    }

                                    // still will not return something for all assets
                                    if (icon == null || !icon.isReadable)
                                    {
                                        continue;
                                    }

                                    byte[] bytes = icon.EncodeToPNG();
                                    if (bytes == null)
                                    {
                                        continue;
                                    }

                                    File.WriteAllBytes(docuPath + folder + "/" + Path.GetFileName(assetPath) + ".png", bytes);
                                }
                                else
                                {
                                    // most likely a directory
                                    continue;
                                }
                            }

                            objects += "<div class=\"media mb-1\">";
                            string imageName = folder + "/" + Path.GetFileName(assetPath) + ".png";
                            string accessKey = assetPath.Substring((root + folder).Length + 1);
                            if (!withExtension)
                            {
                                accessKey = accessKey.Substring(0, accessKey.LastIndexOf('.'));
                            }

                            if (generatePreview)
                            {
                                imageLink = File.Exists(docuPath + imageName) ? imageName : "MissingPreview.png";
                            }
                            objects += "<img src=\"" + imageLink + "\" class=\"mr-3\" width=\"128\">";
                            objects += "<div class=\"media-body\">/" + levelName + "/" + accessKey;
                            objects += "</div></div>";
                        }
                        objects += "</table>";
                    }
                    html = html.Replace($"{{{variableName}List}}", objects);
                    html = html.Replace($"{{{variableName}Count}}", objCount.ToString());
                }

                // copy data contents and level descriptor
                DirectoryUtil.Copy(root + "/Data", docuPath + "/Data");
                FileUtil.CopyFileOrDirectory(root + "/level.json", docuPath + "/level.json");

                // remove all meta files
                foreach (string fileName in Directory.EnumerateFiles(docuPath, "*.meta", SearchOption.AllDirectories))
                {
                    FileUtil.DeleteFileOrDirectory(fileName);
                }

                File.WriteAllText(docuPath + "index.html", html);
            }
            documentationInProgress = false;
        }