Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.63 KB | None | 0 0
  1. /*
  2.   This script adds a build menu "HTML5 Export" to a Unity project to ease exporting to WebGL with
  3.   different options.
  4.   To set up for a project:
  5.    1. Copy this script under ProjectRoot/Assets/Editor/buildWebGL.cs, and
  6.    2. edit the variable levelsToPack below to list all the scene files that need to be packed as
  7.       part of the build. If there are multiple scenes, have the main/startup scene listed first.
  8.  
  9.   When one of the build entries in the HTML5 Export dropdown menu is activated, a WebGL build will
  10.   be generated under a sibling directory of the project named
  11.   html5_builds/<ProjectName>_<timestamp>_<options>/, and a build log file is generated in the
  12.   outputted directory.
  13. */
  14. using UnityEditor;
  15. using UnityEngine;
  16. using System;
  17. using System.IO;
  18.  
  19. public class ExportWebGL : MonoBehaviour
  20. {
  21.     private static string[] levelsToPack = new string[] {
  22.         "Assets/Scenes/Client.unity",
  23.     };
  24.  
  25.     private static string buildLogFile = null;
  26.  
  27.     static void WriteLog(string s)
  28.     {
  29.         try
  30.         {
  31.             StreamWriter sw = new StreamWriter(buildLogFile, true);
  32.             sw.WriteLine(s);
  33.             sw.Close();
  34.         }
  35.         catch (Exception)
  36.         {
  37.         }
  38.     }
  39.     static string FormatBytes(ulong bytes)
  40.     {
  41.         double gb = bytes / (1024.0 * 1024.0 * 1024.0);
  42.         double mb = bytes / (1024.0 * 1024.0);
  43.         double kb = bytes / 1024.0;
  44.         if (mb >= 1000) return gb.ToString("#.000") + "GB";
  45.         if (kb >= 1000) return mb.ToString("#.000") + "MB";
  46.         if (kb >= 1) return kb.ToString("#.000") + "KB";
  47.         return bytes + "B";
  48.     }
  49.  
  50.     static string TimeSpanToHHMMSSString(TimeSpan s)
  51.     {
  52.         if (s.ToString("hh") != "00")
  53.             return s.ToString("hh") + "h " + s.ToString("mm") + "m " + s.ToString("ss") + "s";
  54.         else
  55.             return s.ToString("mm") + "m " + s.ToString("ss") + "s";
  56.     }
  57.  
  58.     static string PathRelativeTo(string path, string basePathRelativeTo)
  59.     {
  60.         // Abuse URI computation to compute a path relative to another
  61.         return new Uri(basePathRelativeTo + "/").MakeRelativeUri(new Uri(path)).ToString();
  62.     }
  63.  
  64.     static void DoHtml5BuildToDirectory(string path, string emscriptenLinkerFlags, WebGLCompressionFormat compressionFormat, bool wasm)
  65.     {
  66.         PlayerSettings.WebGL.linkerTarget = wasm ? WebGLLinkerTarget.Wasm : WebGLLinkerTarget.Asm;
  67.         PlayerSettings.WebGL.threadsSupport = false;
  68.         PlayerSettings.WebGL.memorySize = 256;
  69.         PlayerSettings.WebGL.emscriptenArgs = " -s TOTAL_STACK=1MB " + " -s ERROR_ON_UNDEFINED_SYMBOLS=0 " +  emscriptenLinkerFlags;
  70.         PlayerSettings.WebGL.compressionFormat = compressionFormat;
  71.  
  72.         Debug.Log("Starting a HTML5 build with Emscripten linker flags \"" + PlayerSettings.WebGL.emscriptenArgs + "\" to directory \"" + path + "\"...");
  73.  
  74.         if (!System.IO.Directory.Exists(path))
  75.             System.IO.Directory.CreateDirectory(path);
  76.         buildLogFile = path + "/build_log.txt";
  77.  
  78.         WriteLog("Unity version: " + Application.unityVersion);
  79.         WriteLog("Project: " + Application.companyName + " " + Application.productName + " " + Application.version);
  80.         WriteLog("Build date: " + DateTime.Now.ToString("yyyy MMM dd HH:mm:ss"));
  81.         WriteLog("");
  82.         var buildStart = DateTime.Now;
  83.         UnityEditor.Build.Reporting.BuildReport report = BuildPipeline.BuildPlayer(levelsToPack, path, BuildTarget.WebGL, path.Contains("development") ? BuildOptions.Development : BuildOptions.None);
  84.         var buildEnd = DateTime.Now;
  85.         Debug.Log("HTML5 build finished in " + TimeSpanToHHMMSSString(buildEnd.Subtract(buildStart)) + " to directory " + path);
  86.         WriteLog("HTML5 build finished in " + TimeSpanToHHMMSSString(buildEnd.Subtract(buildStart)));
  87.         WriteLog("");
  88.         ulong totalSize = 0;
  89.         foreach (var f in report.files)
  90.         {
  91.             string relativePath = PathRelativeTo(f.path, path);
  92.             if (relativePath.StartsWith(".."))
  93.                 continue; // report.files contains paths that are not part of the HTML5/WebGL build output (such as "Temp/StagingArea/Data/Managed/System.Xml.Linq.dll"), so ignore all those
  94.             Debug.Log(relativePath + ": " + FormatBytes(f.size));
  95.             WriteLog(relativePath + ": " + FormatBytes(f.size));
  96.             totalSize += f.size;
  97.         }
  98.         Debug.Log("Total output size (Compression " + compressionFormat.ToString() + "): " + FormatBytes(totalSize));
  99.         WriteLog("");
  100.         WriteLog("Total output size (Compression " + compressionFormat.ToString() + "): " + FormatBytes(totalSize));
  101.     }
  102.  
  103.     static void DoHtml5Build(string kind, string emscriptenLinkerFlags, WebGLCompressionFormat compressionFormat, bool wasm)
  104.     {
  105.         var date = DateTime.Now.ToString("yyyyMMdd_HHmmss");
  106.         var path = System.IO.Path.GetFullPath(Application.dataPath + "/../../html5_builds/" + Application.productName + "_" + date + "_" + kind);
  107.         DoHtml5BuildToDirectory(path, emscriptenLinkerFlags, compressionFormat, wasm);
  108.     }
  109.  
  110.     static void DoHtml5BuildAskDirectory(string emscriptenLinkerFlags, WebGLCompressionFormat compressionFormat, bool wasm)
  111.     {
  112.         var path = EditorUtility.SaveFolderPanel("Choose Output Location for HTML5 Export", "", "");
  113.         DoHtml5BuildToDirectory(path, emscriptenLinkerFlags, compressionFormat, wasm);
  114.     }
  115.  
  116.     [MenuItem("HTML5 Export/Asm.js+Development uncompressed...")]
  117.     static void DoAsmDevelopmentExportUncompressed()
  118.     {
  119.         DoHtml5Build("asm_development_uncompressed", "", WebGLCompressionFormat.Disabled, false);
  120.     }
  121.  
  122.     [MenuItem("HTML5 Export/Asm.js+Development gzip compressed...")]
  123.     static void DoAsmDevelopmentExportGzipCompressed()
  124.     {
  125.         DoHtml5Build("asm_development_gzip_compressed", "", WebGLCompressionFormat.Gzip, false);
  126.     }
  127.  
  128.     [MenuItem("HTML5 Export/Asm.js+Release uncompressed...")]
  129.     static void DoAsmReleaseExportUncompressed()
  130.     {
  131.         DoHtml5Build("asm_release_uncompressed", "", WebGLCompressionFormat.Disabled, false);
  132.     }
  133.  
  134.     [MenuItem("HTML5 Export/Asm.js+Release gzip compressed...")]
  135.     static void DoAsmReleaseExportGzipCompressed()
  136.     {
  137.         DoHtml5Build("asm_release_gzip_compressed", "", WebGLCompressionFormat.Gzip, false);
  138.     }
  139.  
  140.     [MenuItem("HTML5 Export/Wasm+Development uncompressed...")]
  141.     static void DoDevelopmentExportUncompressed()
  142.     {
  143.         DoHtml5Build("wasm_development_uncompressed", "", WebGLCompressionFormat.Disabled, true);
  144.     }
  145.  
  146.     [MenuItem("HTML5 Export/Wasm+Release gzipped...")]
  147.     static void DoReleaseExportGzipped()
  148.     {
  149.         DoHtml5Build("wasm_release_gzipped", "", WebGLCompressionFormat.Gzip, true);
  150.     }
  151.  
  152.     [MenuItem("HTML5 Export/Wasm+Release uncompressed...")]
  153.     static void DoReleaseExportUncompressed()
  154.     {
  155.         DoHtml5Build("wasm_release", "", WebGLCompressionFormat.Disabled, true);
  156.     }
  157.  
  158.     [MenuItem("HTML5 Export/Wasm+Release+Profiling uncompressed...")]
  159.     static void DoProfilingExport()
  160.     {
  161.         DoHtml5Build("wasm_release_profiling", "--profiling-funcs", WebGLCompressionFormat.Disabled, true);
  162.     }
  163.  
  164.     [MenuItem("HTML5 Export/Wasm+Release+CpuProfiler uncompressed...")]
  165.     static void DoCpuProfilerExport()
  166.     {
  167.         DoHtml5Build("wasm_release_cpuprofiler", "--profiling-funcs --cpuprofiler", WebGLCompressionFormat.Disabled, true);
  168.     }
  169.  
  170.     [MenuItem("HTML5 Export/Wasm+Release+MemoryProfiler uncompressed...")]
  171.     static void DoMemoryProfilerExport()
  172.     {
  173.         DoHtml5Build("wasm_release_memoryprofiler", "--profiling-funcs --memoryprofiler", WebGLCompressionFormat.Disabled, true);
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement