WebGLBuildScript.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (c) 2024 Vuplex Inc. All rights reserved.
  2. //
  3. // Licensed under the Vuplex Commercial Software Library License, you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // https://vuplex.com/commercial-library-license
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if UNITY_WEBGL
  15. using System;
  16. using System.IO;
  17. using System.Linq;
  18. using UnityEditor;
  19. using UnityEditor.Build;
  20. using UnityEditor.Callbacks;
  21. namespace Vuplex.WebView.Editor {
  22. /// <summary>
  23. /// Modifies the compiled project's index.html file to replace instances of
  24. /// unityInstance.SetFullscreen(1) with window.vuplex.SetFullscreen(1).
  25. /// https://support.vuplex.com/articles/webgl-fullscreen
  26. /// </summary>
  27. public class WebGLBuildScript {
  28. [PostProcessBuild]
  29. public static void OnPostProcessBuild(BuildTarget target, string builtProjectPath) {
  30. if (target != BuildTarget.WebGL) {
  31. return;
  32. }
  33. #if !VUPLEX_WEBGL_DISABLE_FULLSCREEN_OVERRIDE
  34. var indexHtmlFilePath = Path.Combine(builtProjectPath, "index.html");
  35. if (!File.Exists(indexHtmlFilePath)) {
  36. return;
  37. }
  38. var indexHtmlFileText = File.ReadAllText(indexHtmlFilePath);
  39. #if UNITY_2019_1_OR_NEWER
  40. var stringToReplace = "unityInstance.SetFullscreen(1)";
  41. #else
  42. // The template for Unity 2018 uses the variable name gameInstance instead.
  43. var stringToReplace = "gameInstance.SetFullscreen(1)";
  44. #endif
  45. var updatedIndexHtmlFileText = indexHtmlFileText.Replace(stringToReplace, "window.vuplex.SetFullscreen(1)/*https://support.vuplex.com/articles/webgl-fullscreen*/");
  46. File.WriteAllText(indexHtmlFilePath, updatedIndexHtmlFileText);
  47. #endif
  48. }
  49. }
  50. }
  51. #endif