Launch.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. void Start()
  17. {
  18. CreateView();
  19. launchApp = new LaunchApp(gameObject.scene, this, new HotPatchLaunchResources(), destroyCancellationToken);
  20. launchApp.RunAsync().Forget(Debug.LogException);
  21. }
  22. public void ResetProgress()
  23. {
  24. lastProgress = 0;
  25. ReportProgress("Checking version", 0);
  26. }
  27. public void ReportProgress(string stage, float progress)
  28. {
  29. if (this == null) return;
  30. lastProgress = Mathf.Max(lastProgress, Mathf.Clamp01(progress));
  31. progressFill.fillAmount = lastProgress;
  32. percentText.text = Mathf.RoundToInt(lastProgress * 100) + "%";
  33. statusText.text = stage;
  34. }
  35. public void ShowError(string message, Action retry)
  36. {
  37. if (!IsAvailable) return;
  38. statusText.text = message;
  39. NativeDialog.ShowRetryDialog(retry);
  40. }
  41. public void CreateView()
  42. {
  43. var canvas = gameObject.scene.GetRootGameObjects().SelectMany(g => g.GetComponentsInChildren<Canvas>(true)).First();
  44. if (progressFill == null) progressFill = canvas.GetComponentsInChildren<Image>(true).First(i => i.name == "bar");
  45. progressFill.type = Image.Type.Filled; progressFill.fillMethod = Image.FillMethod.Horizontal;
  46. progressFill.fillOrigin = 0; progressFill.fillAmount = 0;
  47. var scaler = canvas.GetComponent<CanvasScaler>();
  48. scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
  49. scaler.referenceResolution = new Vector2(750, 1334); scaler.matchWidthOrHeight = .5f;
  50. if (statusText == null) statusText = MakeText(canvas.transform, "HotPatchStatus", -550, new Vector2(680, 110), 24);
  51. if (percentText == null) percentText = MakeText(canvas.transform, "HotPatchPercent", -420, new Vector2(300, 50), 28);
  52. statusText.text = "Checking version"; percentText.text = "0%";
  53. }
  54. static Text MakeText(Transform parent, string name, float y, Vector2 size, int fontSize)
  55. {
  56. var go = new GameObject(name, typeof(RectTransform), typeof(Text));
  57. go.transform.SetParent(parent, false);
  58. var rect = (RectTransform)go.transform; rect.sizeDelta = size; rect.anchoredPosition = new Vector2(0, y);
  59. var text = go.GetComponent<Text>();
  60. text.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
  61. text.fontSize = fontSize; text.color = Color.white; text.alignment = TextAnchor.MiddleCenter;
  62. text.raycastTarget = false; text.horizontalOverflow = HorizontalWrapMode.Wrap;
  63. return text;
  64. }
  65. }