using System; using System.Linq; using Cysharp.Threading.Tasks; using HotPatch; using TMPro; using UnityEngine; using UnityEngine.UI; [UnityEngine.Scripting.APIUpdating.MovedFrom(true, sourceNamespace: "", sourceAssembly: "Assembly-CSharp", sourceClassName: "Launch")] public sealed class Launch : MonoBehaviour, ILaunchView { public Image progressFill; public TMP_Text statusText; public TMP_Text percentText; LaunchApp launchApp; float lastProgress; public bool IsAvailable => this != null && isActiveAndEnabled; void Start() { CreateView(); launchApp = new LaunchApp(gameObject.scene, this, new HotPatchLaunchResources(), destroyCancellationToken); launchApp.RunAsync().Forget(Debug.LogException); } public void ResetProgress() { lastProgress = 0; ReportProgress("Checking version", 0); } public void ReportProgress(string stage, float progress) { if (this == null) return; lastProgress = Mathf.Max(lastProgress, Mathf.Clamp01(progress)); progressFill.fillAmount = lastProgress; percentText.text = Mathf.RoundToInt(lastProgress * 100) + "%"; statusText.text = stage; } public void ShowError(string message, Action retry) { if (!IsAvailable) return; statusText.text = message; NativeDialog.ShowRetryDialog(retry); } public void CreateView() { var canvas = gameObject.scene.GetRootGameObjects().SelectMany(g => g.GetComponentsInChildren(true)).First(); if (progressFill == null) progressFill = canvas.GetComponentsInChildren(true).First(i => i.name == "bar"); progressFill.type = Image.Type.Filled; progressFill.fillMethod = Image.FillMethod.Horizontal; progressFill.fillOrigin = 0; progressFill.fillAmount = 0; var scaler = canvas.GetComponent(); scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; scaler.referenceResolution = new Vector2(750, 1334); scaler.matchWidthOrHeight = .5f; //if (statusText == null) statusText = TextMeshProUGUI(canvas.transform, "HotPatchStatus", -550, new Vector2(680, 110), 24); //if (percentText == null) percentText = TextMeshProUGUI(canvas.transform, "HotPatchPercent", -420, new Vector2(300, 50), 28); statusText.text = "Checking version"; percentText.text = "0%"; } static Text MakeText(Transform parent, string name, float y, Vector2 size, int fontSize) { var go = new GameObject(name, typeof(RectTransform), typeof(Text)); go.transform.SetParent(parent, false); var rect = (RectTransform)go.transform; rect.sizeDelta = size; rect.anchoredPosition = new Vector2(0, y); var text = go.GetComponent(); text.font = Resources.GetBuiltinResource("LegacyRuntime.ttf"); text.fontSize = fontSize; text.color = Color.white; text.alignment = TextAnchor.MiddleCenter; text.raycastTarget = false; text.horizontalOverflow = HorizontalWrapMode.Wrap; return text; } }