GameScreenView.Panels.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public partial class GameScreenView
  7. {
  8. const string FailReasonTimeUp = "time_up";
  9. const string FailReasonNoMoves = "no_moves";
  10. TMP_Text pauseModeInfo;
  11. TMP_Text pauseMovesCount;
  12. TMP_Text pauseFruitsLeftCount;
  13. TMP_Text pauseServedCount;
  14. GameObject winPanel;
  15. GameObject losePanel;
  16. TMP_Text winModeInfo;
  17. TMP_Text winSummary;
  18. TMP_Text winLevelBonusValue;
  19. TMP_Text winMovesBonusValue;
  20. TMP_Text winServedValue;
  21. TMP_Text winTotalReward;
  22. GameObject winTicketBadge;
  23. TMP_Text winTicketText;
  24. TMP_Text winNextLabel;
  25. TMP_Text loseTimeUpModeInfo;
  26. TMP_Text loseNoMovesModeInfo;
  27. TMP_Text loseMessage;
  28. GameObject loseTimeUpGroup;
  29. GameObject loseNoMovesGroup;
  30. TMP_Text loseServedLabel;
  31. TMP_Text loseServedValue;
  32. TMP_Text loseFruitLeftLabel;
  33. TMP_Text loseFruitLeftValue;
  34. TMP_Text loseMovesLeftLabel;
  35. TMP_Text loseMovesLeftValue;
  36. TMP_Text loseNextLabel;
  37. bool resultIsWin;
  38. string resultFailTitle = "";
  39. [Header("Panel Prefabs")]
  40. [SerializeField] GameObject pausePanelPrefab;
  41. [SerializeField] GameObject winPanelPrefab;
  42. [SerializeField] GameObject losePanelPrefab;
  43. void BuildBanner()
  44. {
  45. var bannerGo = new GameObject("Banner", typeof(RectTransform), typeof(CanvasGroup));
  46. bannerGo.transform.SetParent(transform, false);
  47. var rect = (RectTransform)bannerGo.transform;
  48. rect.anchorMin = new Vector2(0.5f, 0.5f);
  49. rect.anchorMax = new Vector2(0.5f, 0.5f);
  50. rect.pivot = new Vector2(0.5f, 0.5f);
  51. rect.anchoredPosition = new Vector2(0f, 95f);
  52. rect.sizeDelta = new Vector2(640f, 110f);
  53. bannerGroup = bannerGo.GetComponent<CanvasGroup>();
  54. bannerGroup.alpha = 0f;
  55. bannerGroup.blocksRaycasts = false;
  56. bannerGroup.interactable = false;
  57. bannerText = CreateText(bannerGo.transform, "BannerText", textTemplate, "NICE!", 44f, new Color(1f, 0.85f, 0.32f, 1f), TextAlignmentOptions.Center, Vector2.zero, new Vector2(640f, 110f));
  58. if (bannerText != null)
  59. {
  60. var outline = bannerText.gameObject.AddComponent<Outline>();
  61. outline.effectColor = new Color(0.28f, 0.07f, 0.01f, 1f);
  62. outline.effectDistance = new Vector2(3f, -3f);
  63. }
  64. }
  65. public void SetPanelPrefabs(GameObject pause, GameObject win, GameObject lose, GameObject pop)
  66. {
  67. pausePanelPrefab = pause;
  68. winPanelPrefab = win;
  69. losePanelPrefab = lose;
  70. popViewPanelPrefab = pop;
  71. }
  72. void BuildPanels()
  73. {
  74. BuildPausePanel();
  75. BuildWinPanel();
  76. BuildLosePanel();
  77. BuildPopView();
  78. }
  79. GameObject InstantiatePanel(GameObject prefab, string resourcePath, string panelName)
  80. {
  81. if (prefab == null)
  82. prefab = Resources.Load<GameObject>(resourcePath);
  83. if (prefab == null)
  84. {
  85. Debug.LogError("[GameScreen] Missing panel prefab: " + resourcePath);
  86. return null;
  87. }
  88. GameObject panel = Instantiate(prefab, transform, false);
  89. panel.name = panelName;
  90. panel.SetActive(false);
  91. return panel;
  92. }
  93. static Transform PanelChild(GameObject panel, string path)
  94. {
  95. if (panel == null)
  96. return null;
  97. Transform child = panel.transform.Find(path);
  98. if (child == null)
  99. Debug.LogWarning("[GameScreen] Missing panel node: " + path);
  100. return child;
  101. }
  102. static TMP_Text PanelText(GameObject panel, string path)
  103. {
  104. Transform child = PanelChild(panel, path);
  105. TMP_Text text = child != null ? child.GetComponent<TMP_Text>() : null;
  106. if (child != null && text == null)
  107. Debug.LogWarning("[GameScreen] Panel node is not TMP text: " + path);
  108. return text;
  109. }
  110. static Button PanelButton(GameObject panel, string path)
  111. {
  112. Transform child = PanelChild(panel, path);
  113. return child != null ? UIViewUtility.EnsureButton(child) : null;
  114. }
  115. void BuildPausePanel()
  116. {
  117. pausePanel = InstantiatePanel(pausePanelPrefab, "Prefab/08_pause", "PausePanel");
  118. if (pausePanel == null)
  119. return;
  120. pauseModeInfo = PanelText(pausePanel, "IMG_BG/IMG_PauseModeBadge/TMP_PauseModeInfo");
  121. pauseMovesCount = PanelText(pausePanel, "IMG_BG/IMG_PauseStatsBG/TMP_MovesCount");
  122. pauseFruitsLeftCount = PanelText(pausePanel, "IMG_BG/IMG_PauseStatsBG/TMP_FruitsLeftCount");
  123. pauseServedCount = PanelText(pausePanel, "IMG_BG/IMG_PauseStatsBG/TMP_ServedCount");
  124. UIViewUtility.Bind(PanelButton(pausePanel, "IMG_BG/BTN_ClosePause"), ResumeGame);
  125. UIViewUtility.Bind(PanelButton(pausePanel, "IMG_BG/BTN_Resume"), ResumeGame);
  126. UIViewUtility.Bind(PanelButton(pausePanel, "IMG_BG/BTN_Quit"), HandleBack);
  127. UILocaleScreens.ApplyPausePanel(pausePanel != null ? pausePanel.transform : null);
  128. }
  129. void BuildWinPanel()
  130. {
  131. winPanel = InstantiatePanel(winPanelPrefab, "Prefab/09_win", "WinPanel");
  132. if (winPanel == null)
  133. return;
  134. winModeInfo = PanelText(winPanel, "IMG_BG/IMG_PauseModeBadge/TMP_PauseModeInfo");
  135. winSummary = PanelText(winPanel, "IMG_BG/IMG_StandEmptySummaryBG/TMP_StandEmptySummary");
  136. winLevelBonusValue = PanelText(winPanel, "IMG_BG/IMG_ClearedScoreBG/GRP_SetsServed/TMP_Value");
  137. winMovesBonusValue = PanelText(winPanel, "IMG_BG/IMG_ClearedScoreBG/GRP_FruitLeft/TMP_Value");
  138. winServedValue = PanelText(winPanel, "IMG_BG/IMG_ClearedScoreBG/GRP_MovesLeft/TMP_Value");
  139. winTotalReward = PanelText(winPanel, "IMG_BG/IMG_TotalRewardBG/OBJ_Content/TMP_Reward_Coin");
  140. winTicketBadge = PanelChild(winPanel, "IMG_BG/IMG_DrawTicketBadgeBG")?.gameObject;
  141. winTicketText = PanelText(winPanel, "IMG_BG/IMG_DrawTicketBadgeBG/OBJ_Content/TMP_Reward_Ticket");
  142. winNextLabel = PanelText(winPanel, "IMG_BG/BTN_NextLevel/TMP_Label");
  143. UIViewUtility.Bind(PanelButton(winPanel, "IMG_BG/BTN_ClosePause"), HandleBack);
  144. UIViewUtility.Bind(PanelButton(winPanel, "IMG_BG/BTN_Back"), HandleBack);
  145. UIViewUtility.Bind(PanelButton(winPanel, "IMG_BG/BTN_NextLevel"), HandleResultAction);
  146. UILocaleScreens.ApplyWinPanel(winPanel != null ? winPanel.transform : null);
  147. }
  148. void BuildLosePanel()
  149. {
  150. losePanel = InstantiatePanel(losePanelPrefab, "Prefab/10_lose", "LosePanel");
  151. if (losePanel == null)
  152. return;
  153. loseTimeUpGroup = PanelChild(losePanel, "IMG_BG/GRP_TimeUp")?.gameObject;
  154. loseNoMovesGroup = PanelChild(losePanel, "IMG_BG/GRP_NoMoves")?.gameObject;
  155. loseTimeUpModeInfo = PanelText(losePanel, "IMG_BG/GRP_TimeUp/IMG_PauseModeBadge/TMP_PauseModeInfo");
  156. loseNoMovesModeInfo = PanelText(losePanel, "IMG_BG/GRP_NoMoves/IMG_PauseModeBadge/TMP_PauseModeInfo");
  157. loseMessage = PanelText(losePanel, "IMG_BG/TMP_Message");
  158. loseServedLabel = PanelText(losePanel, "IMG_BG/IMG_ClearedScoreBG/GRP_SetsServed/TMP_Label");
  159. loseServedValue = PanelText(losePanel, "IMG_BG/IMG_ClearedScoreBG/GRP_SetsServed/TMP_Value");
  160. loseFruitLeftLabel = PanelText(losePanel, "IMG_BG/IMG_ClearedScoreBG/GRP_FruitLeft/TMP_Label");
  161. loseFruitLeftValue = PanelText(losePanel, "IMG_BG/IMG_ClearedScoreBG/GRP_FruitLeft/TMP_Value");
  162. loseMovesLeftLabel = PanelText(losePanel, "IMG_BG/IMG_ClearedScoreBG/GRP_MovesLeft/TMP_Label");
  163. loseMovesLeftValue = PanelText(losePanel, "IMG_BG/IMG_ClearedScoreBG/GRP_MovesLeft/TMP_Value");
  164. loseNextLabel = PanelText(losePanel, "IMG_BG/BTN_NextLevel/TMP_Label");
  165. UIViewUtility.Bind(PanelButton(losePanel, "IMG_BG/BTN_ClosePause"), HandleBack);
  166. UIViewUtility.Bind(PanelButton(losePanel, "IMG_BG/BTN_Back"), HandleBack);
  167. UIViewUtility.Bind(PanelButton(losePanel, "IMG_BG/BTN_NextLevel"), HandleResultAction);
  168. }
  169. void HandleResultAction()
  170. {
  171. int next = resultIsWin
  172. ? (levelNumber >= GameDefinitions.MaxLevel ? GameDefinitions.MaxLevel : levelNumber + 1)
  173. : levelNumber;
  174. HidePanels();
  175. NextLevelRequested?.Invoke(next);
  176. }
  177. bool IsResultVisible()
  178. {
  179. return (winPanel != null && winPanel.activeSelf) || (losePanel != null && losePanel.activeSelf);
  180. }
  181. void PauseGame()
  182. {
  183. if (paused || ended)
  184. return;
  185. if (IsResultVisible())
  186. return;
  187. paused = true;
  188. toolMode = "";
  189. ClearSelection();
  190. RefreshPausePanel();
  191. ShowOverlay(pausePanel);
  192. }
  193. void RefreshPausePanel()
  194. {
  195. UILocaleScreens.ApplyPausePanel(pausePanel != null ? pausePanel.transform : null);
  196. if (pauseModeInfo != null)
  197. pauseModeInfo.text = GameLocale.ModeLevelLine(modeKey, levelNumber);
  198. if (pauseMovesCount != null)
  199. pauseMovesCount.text = state.MovesLeft.ToString();
  200. if (pauseFruitsLeftCount != null)
  201. pauseFruitsLeftCount.text = state.RemainingFruits.ToString();
  202. if (pauseServedCount != null)
  203. pauseServedCount.text = state.Served.ToString();
  204. }
  205. void ResumeGame()
  206. {
  207. paused = false;
  208. if (pausePanel != null)
  209. pausePanel.SetActive(false);
  210. }
  211. void HidePanels()
  212. {
  213. paused = false;
  214. if (pausePanel != null)
  215. pausePanel.SetActive(false);
  216. if (winPanel != null)
  217. winPanel.SetActive(false);
  218. if (losePanel != null)
  219. losePanel.SetActive(false);
  220. if (popView != null)
  221. popView.Hide();
  222. }
  223. void ShowOverlay(GameObject overlay)
  224. {
  225. if (overlay == null)
  226. return;
  227. overlay.SetActive(true);
  228. overlay.transform.SetAsLastSibling();
  229. var group = overlay.GetComponent<CanvasGroup>();
  230. if (group == null)
  231. return;
  232. if (!Application.isPlaying)
  233. {
  234. group.alpha = 1f;
  235. return;
  236. }
  237. StartCoroutine(FadeOverlay(group));
  238. }
  239. IEnumerator FadeOverlay(CanvasGroup group)
  240. {
  241. group.alpha = 0f;
  242. float elapsed = 0f;
  243. while (elapsed < 0.18f)
  244. {
  245. elapsed += Time.unscaledDeltaTime;
  246. group.alpha = Mathf.Clamp01(elapsed / 0.18f);
  247. yield return null;
  248. }
  249. group.alpha = 1f;
  250. }
  251. void ShowBanner(string message, float hold, float size)
  252. {
  253. if (bannerText == null || bannerGroup == null)
  254. return;
  255. bannerText.text = message;
  256. bannerText.fontSize = size;
  257. if (!Application.isPlaying)
  258. {
  259. bannerGroup.alpha = 0f;
  260. return;
  261. }
  262. if (bannerRoutine != null)
  263. StopCoroutine(bannerRoutine);
  264. bannerRoutine = StartCoroutine(BannerRoutine(hold));
  265. }
  266. IEnumerator BannerRoutine(float hold)
  267. {
  268. bannerGroup.alpha = 0f;
  269. var rect = bannerText.rectTransform;
  270. rect.localScale = Vector3.one * 0.82f;
  271. float elapsed = 0f;
  272. while (elapsed < 0.14f)
  273. {
  274. elapsed += Time.unscaledDeltaTime;
  275. float t = Mathf.Clamp01(elapsed / 0.14f);
  276. bannerGroup.alpha = t;
  277. rect.localScale = Vector3.one * Mathf.Lerp(0.82f, 1.04f, t);
  278. yield return null;
  279. }
  280. rect.localScale = Vector3.one;
  281. yield return new WaitForSecondsRealtime(Mathf.Max(0.05f, hold));
  282. elapsed = 0f;
  283. while (elapsed < 0.22f)
  284. {
  285. elapsed += Time.unscaledDeltaTime;
  286. bannerGroup.alpha = 1f - Mathf.Clamp01(elapsed / 0.22f);
  287. yield return null;
  288. }
  289. bannerGroup.alpha = 0f;
  290. bannerRoutine = null;
  291. }
  292. void WinLevel()
  293. {
  294. if (ended)
  295. return;
  296. ended = true;
  297. busy = false;
  298. toolMode = "";
  299. ClearSelection();
  300. ApplyWinRewards();
  301. GameAudio.PlaySfx(GameSfx.Win);
  302. if (Application.isPlaying)
  303. StartCoroutine(WinSequence());
  304. else
  305. ShowResult(true, "", "");
  306. }
  307. IEnumerator WinSequence()
  308. {
  309. ShowBanner(L.Get("game.banner.stand_empty"), 1.1f, 54f);
  310. bool reduce = SaveService.Data.settings.reduceMotion;
  311. if (!reduce)
  312. yield return new WaitForSecondsRealtime(0.95f);
  313. ShowResult(true, "", "");
  314. }
  315. void FailLevel(string title, string subtitle)
  316. {
  317. if (ended)
  318. return;
  319. ended = true;
  320. busy = false;
  321. toolMode = "";
  322. ClearSelection();
  323. GameAudio.PlaySfx(GameSfx.Fail);
  324. if (title == FailReasonNoMoves && CanOfferAdRevive())
  325. OfferAdRevive(title, subtitle);
  326. else
  327. ShowResult(false, title, subtitle);
  328. }
  329. void ApplyWinRewards()
  330. {
  331. var save = SaveService.Data;
  332. int previous = save.cleared.Get(modeKey);
  333. bool firstClear = levelNumber > previous;
  334. lastRewardCoins = definition.BaseReward + state.MovesLeft * 8;
  335. lastServedCoins = state.Served * 2;
  336. lastTicket = 0;
  337. if (firstClear && levelNumber == 1)
  338. lastTicket = 1;
  339. else if (firstClear && UnityEngine.Random.value < 0.35f)
  340. lastTicket = 1;
  341. SaveService.AddCoins(lastRewardCoins + lastServedCoins, "ledger.game_clear|" + modeKey + "|" + levelNumber);
  342. save.cleared.Set(modeKey, Mathf.Max(previous, levelNumber));
  343. save.stats.clears += 1;
  344. save.stats.served += state.Served;
  345. save.stats.best = Mathf.Max(save.stats.best, state.Score);
  346. save.tickets += lastTicket;
  347. SaveService.SaveNow();
  348. }
  349. void ShowResult(bool win, string title, string subtitle)
  350. {
  351. if (AdMobEngagement.RecordRoundCompleted())
  352. AdMobService.NotifyNonRewardedAdsUnlocked();
  353. resultIsWin = win;
  354. resultFailTitle = title;
  355. if (pausePanel != null)
  356. pausePanel.SetActive(false);
  357. if (win)
  358. {
  359. if (lastRewardCoins + lastServedCoins > 0)
  360. GameAudio.PlaySfx(GameSfx.Coin);
  361. UpdateWinPanel();
  362. ShowOverlay(winPanel);
  363. if (losePanel != null)
  364. losePanel.SetActive(false);
  365. PlayConfetti();
  366. }
  367. else
  368. {
  369. UpdateLosePanel(title, subtitle);
  370. ShowOverlay(losePanel);
  371. if (winPanel != null)
  372. winPanel.SetActive(false);
  373. }
  374. }
  375. void UpdateWinPanel()
  376. {
  377. UILocaleScreens.ApplyWinPanel(winPanel != null ? winPanel.transform : null);
  378. if (winModeInfo != null)
  379. winModeInfo.text = GameLocale.ModeLevelLine(modeKey, levelNumber);
  380. if (winSummary != null)
  381. winSummary.text = L.F("result.win_summary", state.Served, state.MovesLeft);
  382. if (winLevelBonusValue != null)
  383. winLevelBonusValue.text = "+" + definition.BaseReward;
  384. if (winMovesBonusValue != null)
  385. winMovesBonusValue.text = "+" + (state.MovesLeft * 8);
  386. if (winServedValue != null)
  387. winServedValue.text = state.Served.ToString();
  388. if (winTotalReward != null)
  389. winTotalReward.text = "+" + (lastRewardCoins + lastServedCoins);
  390. if (winTicketBadge != null)
  391. winTicketBadge.SetActive(lastTicket > 0);
  392. if (winTicketText != null)
  393. winTicketText.text = L.F("result.draw_ticket", lastTicket);
  394. if (winNextLabel != null)
  395. winNextLabel.text = levelNumber >= GameDefinitions.MaxLevel
  396. ? L.F("result.replay_max", GameDefinitions.MaxLevel)
  397. : L.Get("result.next_level");
  398. }
  399. void UpdateLosePanel(string title, string subtitle)
  400. {
  401. UILocaleScreens.ApplyLosePanel(losePanel != null ? losePanel.transform : null);
  402. bool timeUp = title == FailReasonTimeUp;
  403. string modeInfo = GameLocale.ModeLevelLine(modeKey, levelNumber);
  404. if (loseTimeUpModeInfo != null)
  405. loseTimeUpModeInfo.text = modeInfo;
  406. if (loseNoMovesModeInfo != null)
  407. loseNoMovesModeInfo.text = modeInfo;
  408. if (loseTimeUpGroup != null)
  409. loseTimeUpGroup.SetActive(timeUp);
  410. if (loseNoMovesGroup != null)
  411. loseNoMovesGroup.SetActive(!timeUp);
  412. if (loseMessage != null)
  413. {
  414. loseMessage.text = timeUp
  415. ? L.Get("result.lose_time_msg")
  416. : L.Get("result.lose_moves_msg");
  417. }
  418. if (loseServedLabel != null)
  419. loseServedLabel.text = L.Get("result.sets_served");
  420. if (loseServedValue != null)
  421. loseServedValue.text = state.Served.ToString();
  422. if (loseFruitLeftLabel != null)
  423. loseFruitLeftLabel.text = L.Get("result.fruit_left");
  424. if (loseFruitLeftValue != null)
  425. loseFruitLeftValue.text = state.RemainingFruits.ToString();
  426. if (loseMovesLeftLabel != null)
  427. loseMovesLeftLabel.text = L.Get("result.moves_left");
  428. if (loseMovesLeftValue != null)
  429. loseMovesLeftValue.text = state.MovesLeft.ToString();
  430. if (loseNextLabel != null)
  431. loseNextLabel.text = L.Get("common.retry");
  432. }
  433. class ConfettiData
  434. {
  435. public RectTransform rect;
  436. public Vector2 velocity;
  437. public float spin;
  438. public Image image;
  439. }
  440. void PlayConfetti()
  441. {
  442. if (winPanel == null || SaveService.Data.settings.reduceMotion)
  443. return;
  444. ClearConfetti();
  445. Color[] colors =
  446. {
  447. new Color(1f, 0.78f, 0.2f, 1f),
  448. new Color(1f, 0.25f, 0.56f, 1f),
  449. new Color(0.36f, 0.9f, 0.52f, 1f),
  450. new Color(0.33f, 0.91f, 0.89f, 1f),
  451. new Color(1f, 0.95f, 0.72f, 1f)
  452. };
  453. var pieces = new List<ConfettiData>();
  454. for (int i = 0; i < 54; i++)
  455. {
  456. var go = new GameObject("Confetti", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image));
  457. go.transform.SetParent(winPanel.transform, false);
  458. var rect = (RectTransform)go.transform;
  459. rect.anchorMin = new Vector2(0.5f, 0.5f);
  460. rect.anchorMax = new Vector2(0.5f, 0.5f);
  461. rect.pivot = new Vector2(0.5f, 0.5f);
  462. rect.anchoredPosition = new Vector2(UnityEngine.Random.Range(-350f, 350f), UnityEngine.Random.Range(420f, 760f));
  463. rect.sizeDelta = new Vector2(UnityEngine.Random.Range(7f, 13f), UnityEngine.Random.Range(12f, 22f));
  464. var image = go.GetComponent<Image>();
  465. image.color = colors[UnityEngine.Random.Range(0, colors.Length)];
  466. image.raycastTarget = false;
  467. pieces.Add(new ConfettiData
  468. {
  469. rect = rect,
  470. image = image,
  471. velocity = new Vector2(UnityEngine.Random.Range(-90f, 90f), UnityEngine.Random.Range(-220f, -520f)),
  472. spin = UnityEngine.Random.Range(-220f, 220f)
  473. });
  474. }
  475. StartCoroutine(ConfettiRoutine(pieces));
  476. }
  477. IEnumerator ConfettiRoutine(List<ConfettiData> pieces)
  478. {
  479. float elapsed = 0f;
  480. while (elapsed < 2.1f)
  481. {
  482. float dt = Time.unscaledDeltaTime;
  483. elapsed += dt;
  484. for (int i = 0; i < pieces.Count; i++)
  485. {
  486. var piece = pieces[i];
  487. if (piece.rect == null)
  488. continue;
  489. piece.velocity.y -= 780f * dt;
  490. piece.rect.anchoredPosition += piece.velocity * dt;
  491. piece.rect.Rotate(0f, 0f, piece.spin * dt);
  492. if (piece.image != null)
  493. {
  494. Color color = piece.image.color;
  495. color.a = Mathf.Clamp01(1f - (elapsed - 1.4f) / 0.7f);
  496. piece.image.color = color;
  497. }
  498. }
  499. yield return null;
  500. }
  501. for (int i = 0; i < pieces.Count; i++)
  502. {
  503. if (pieces[i].rect != null)
  504. Destroy(pieces[i].rect.gameObject);
  505. }
  506. }
  507. void ClearConfetti()
  508. {
  509. if (confetti.Count > 0)
  510. {
  511. for (int i = 0; i < confetti.Count; i++)
  512. {
  513. if (confetti[i] != null)
  514. DestroyObject(confetti[i]);
  515. }
  516. confetti.Clear();
  517. }
  518. if (winPanel == null)
  519. return;
  520. for (int i = winPanel.transform.childCount - 1; i >= 0; i--)
  521. {
  522. Transform child = winPanel.transform.GetChild(i);
  523. if (child.name == "Confetti")
  524. DestroyObject(child.gameObject);
  525. }
  526. }
  527. }