Launch.cs 3.0 KB

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