Launch.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using DevilLink2.Loading;
  3. using System.Linq;
  4. using Cysharp.Threading.Tasks;
  5. using HotPatch;
  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 Text statusText;
  13. public Text percentText;
  14. LaunchApp launchApp;
  15. GameLoadingView loading;
  16. string failureDetail;
  17. void OnEnable(){Application.logMessageReceived += CaptureFailure;}
  18. void OnDisable(){Application.logMessageReceived -= CaptureFailure;}
  19. void CaptureFailure(string condition,string stack,LogType type){if(type==LogType.Exception && launchApp!=null)failureDetail=condition.Split('\n')[0];}
  20. public bool IsAvailable => this != null && isActiveAndEnabled;
  21. void Start()
  22. {
  23. CreateView();
  24. ILaunchResources resources = new HotPatchLaunchResources();
  25. #if UNITY_EDITOR
  26. var settings=Resources.Load<AppSettings>("AppSettings");
  27. if(settings && !settings.UseLocalHotPatchResources && string.IsNullOrWhiteSpace(settings.DownloadUrl) && AppSettings.RequireGameSceneName()=="Main")
  28. resources=new EditorLocalGameResources();
  29. #endif
  30. launchApp = new LaunchApp(gameObject.scene, this, resources, destroyCancellationToken);
  31. launchApp.RunAsync(() => OpenAfterIntro().Forget(Debug.LogException)).Forget(Debug.LogException);
  32. }
  33. async UniTask OpenAfterIntro()
  34. {
  35. // Initialization proceeds immediately; the Main presentation gate owns the minimum visible duration.
  36. await launchApp.OpenSceneAsync();
  37. }
  38. public void ResetProgress()
  39. {
  40. failureDetail=null;
  41. loading.InitializationStarted();
  42. loading.Indeterminate("Checking resources");
  43. }
  44. public void ReportProgress(string stage, float progress)
  45. {
  46. if (this == null || !loading) return;
  47. // LaunchApp combines weighted update stages (.95 + .04 * scene). It is not raw completion.
  48. // Resource readiness is not scene/UI readiness. Keep the actual remaining stage visible.
  49. if(stage=="Update complete")
  50. loading.StageCompleted(false,"Resources ready · preparing game scene");
  51. else if(stage=="Loading resources" && progress>=1)
  52. loading.StageCompleted(true,"Preparing game interface");
  53. else loading.Indeterminate(stage);
  54. }
  55. public void ShowError(string message, Action retry)
  56. {
  57. if (!IsAvailable) return;
  58. string detail=string.IsNullOrWhiteSpace(failureDetail) ? "Initialization failed without exception details. Check Unity Console before retrying." : failureDetail;
  59. loading.ShowFailure(detail, () => { failureDetail=null; loading.Indeterminate("Retrying initialization"); retry(); });
  60. }
  61. public void CreateView()
  62. {
  63. if (loading) return;
  64. // Retire only the legacy Launch-scene UI; preserve the loader and input services.
  65. foreach (var canvas in gameObject.scene.GetRootGameObjects().SelectMany(g => g.GetComponentsInChildren<Canvas>(true)))
  66. foreach (var graphic in canvas.GetComponentsInChildren<Graphic>(true)) graphic.enabled=false;
  67. loading = GameLoadingView.Create(transform);
  68. }
  69. static Text MakeText(Transform parent, string name, float y, Vector2 size, int fontSize)
  70. {
  71. var go = new GameObject(name, typeof(RectTransform), typeof(Text));
  72. go.transform.SetParent(parent, false);
  73. var rect = (RectTransform)go.transform; rect.sizeDelta = size; rect.anchoredPosition = new Vector2(0, y);
  74. var text = go.GetComponent<Text>();
  75. text.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
  76. text.fontSize = fontSize; text.color = Color.white; text.alignment = TextAnchor.MiddleCenter;
  77. text.raycastTarget = false; text.horizontalOverflow = HorizontalWrapMode.Wrap;
  78. return text;
  79. }
  80. }