Postprocessor.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEditor.Callbacks;
  4. using UnityEngine;
  5. namespace WebGLSupport
  6. {
  7. public class Postprocessor
  8. {
  9. const string MenuPath = "Assets/WebGLSupport/OverwriteFullscreenButton";
  10. #if UNITY_2021_1_OR_NEWER
  11. static readonly bool supportedPostprocessor = true;
  12. static readonly string defaultFullscreenFunc = "unityInstance.SetFullscreen(1);";
  13. static readonly string fullscreenNode = "unity-container";
  14. #else
  15. static readonly bool supportedPostprocessor = false;
  16. static readonly string defaultFullscreenFunc = "";
  17. static readonly string fullscreenNode = "";
  18. #endif
  19. private static bool IsEnable => PlayerPrefs.GetInt(MenuPath, 1) == 1;
  20. [PostProcessBuild(1)]
  21. public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
  22. {
  23. if (target != BuildTarget.WebGL) return;
  24. if (!supportedPostprocessor) return;
  25. if (!IsEnable) return;
  26. var path = Path.Combine(pathToBuiltProject, "index.html");
  27. if (!File.Exists(path)) return;
  28. var html = File.ReadAllText(path);
  29. // check node is exist
  30. if (html.Contains(fullscreenNode))
  31. {
  32. html = html.Replace(defaultFullscreenFunc, $"document.makeFullscreen('{fullscreenNode}');");
  33. File.WriteAllText(path, html);
  34. }
  35. }
  36. [MenuItem(MenuPath)]
  37. public static void OverwriteDefaultFullscreenButton()
  38. {
  39. var flag = !Menu.GetChecked(MenuPath);
  40. Menu.SetChecked(MenuPath, flag);
  41. PlayerPrefs.SetInt(MenuPath, flag ? 1 : 0);
  42. }
  43. [MenuItem(MenuPath, validate = true)]
  44. private static bool OverwriteDefaultFullscreenButtonValidator()
  45. {
  46. Menu.SetChecked(MenuPath, IsEnable);
  47. return true;
  48. }
  49. }
  50. }