Launch.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Linq;
  3. using Cysharp.Threading.Tasks;
  4. using HotPatch;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. [UnityEngine.Scripting.APIUpdating.MovedFrom(true, sourceNamespace: "", sourceAssembly: "Assembly-CSharp", sourceClassName: "Launch")]
  8. public sealed class Launch : MonoBehaviour, ILaunchView
  9. {
  10. public Image progressFill;
  11. public Text statusText;
  12. public Text percentText;
  13. LaunchApp launchApp;
  14. float lastProgress;
  15. public bool IsAvailable => this != null && isActiveAndEnabled;
  16. public bool PlatformOpenedDuringLaunch { get; private set; }
  17. // The runtime can open the platform before the game scene has loaded.
  18. void Awake() => AppRuntimesEvents.OnPlatformOpen += RememberPlatformOpen;
  19. void OnDestroy() => AppRuntimesEvents.OnPlatformOpen -= RememberPlatformOpen;
  20. void RememberPlatformOpen() => PlatformOpenedDuringLaunch = true;
  21. void Start()
  22. {
  23. CreateView();
  24. launchApp = new LaunchApp(gameObject.scene, this, new HotPatchLaunchResources(), destroyCancellationToken);
  25. launchApp.RunAsync().Forget(Debug.LogException);
  26. }
  27. public void ResetProgress()
  28. {
  29. lastProgress = 0;
  30. ReportProgress("Checking version", 0);
  31. }
  32. public void ReportProgress(string stage, float progress)
  33. {
  34. if (this == null) return;
  35. lastProgress = Mathf.Max(lastProgress, Mathf.Clamp01(progress));
  36. progressFill.fillAmount = lastProgress;
  37. percentText.text = Mathf.RoundToInt(lastProgress * 100) + "%";
  38. statusText.text = stage;
  39. }
  40. public void ShowError(string message, Action retry)
  41. {
  42. if (!IsAvailable) return;
  43. statusText.text = message;
  44. NativeDialog.ShowRetryDialog(retry);
  45. }
  46. public void CreateView()
  47. {
  48. var canvas = gameObject.scene.GetRootGameObjects().SelectMany(g => g.GetComponentsInChildren<Canvas>(true)).First();
  49. if (progressFill == null) progressFill = canvas.GetComponentsInChildren<Image>(true).First(i => i.name == "bar");
  50. progressFill.type = Image.Type.Filled; progressFill.fillMethod = Image.FillMethod.Horizontal;
  51. progressFill.fillOrigin = 0; progressFill.fillAmount = 0;
  52. var scaler = canvas.GetComponent<CanvasScaler>();
  53. scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
  54. scaler.referenceResolution = new Vector2(750, 1334); scaler.matchWidthOrHeight = .5f;
  55. if (statusText == null) statusText = MakeText(canvas.transform, "HotPatchStatus", -550, new Vector2(680, 110), 24);
  56. if (percentText == null) percentText = MakeText(canvas.transform, "HotPatchPercent", -420, new Vector2(300, 50), 28);
  57. statusText.text = "Checking version"; percentText.text = "0%";
  58. }
  59. static Text MakeText(Transform parent, string name, float y, Vector2 size, int fontSize)
  60. {
  61. var go = new GameObject(name, typeof(RectTransform), typeof(Text));
  62. go.transform.SetParent(parent, false);
  63. var rect = (RectTransform)go.transform; rect.sizeDelta = size; rect.anchoredPosition = new Vector2(0, y);
  64. var text = go.GetComponent<Text>();
  65. text.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
  66. text.fontSize = fontSize; text.color = Color.white; text.alignment = TextAnchor.MiddleCenter;
  67. text.raycastTarget = false; text.horizontalOverflow = HorizontalWrapMode.Wrap;
  68. return text;
  69. }
  70. }