StoryM.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using DG.Tweening;
  4. using UnityEngine;
  5. using UnityEngine.Localization.Settings;
  6. using UnityEngine.UI;
  7. public class StoryM : MonoBehaviour {
  8. // 文本
  9. public TMPro.TextMeshProUGUI textStory;
  10. public RectTransform shapeBack;
  11. IEnumerator SetStoryText(string textCh, string textEn = "English", float hideTime = 2f) {
  12. textStory.text = LocalizationSettings.SelectedLocale.LocaleName == "Chinese (Simplified) (zh-CN)" ? textCh : textEn;
  13. LayoutRebuilder.ForceRebuildLayoutImmediate(textStory.rectTransform);
  14. // 动画
  15. DOTween.Kill("StoryFont");
  16. textStory.color = Color.black;
  17. textStory.DOColor(Color.white, 0.2f).SetId("StoryFont");
  18. DOTween.Kill("StoryBackground");
  19. shapeBack.DOScaleX((textStory.rectTransform.rect.width + 40f) / 100f, 0.1f).SetId("StoryBackground");
  20. yield return new WaitForSeconds(hideTime);
  21. }
  22. public void HideStoryText() {
  23. textStory.text = "";
  24. shapeBack.DOScaleX(0, 0.06f);
  25. }
  26. // 刷怪
  27. private Dictionary<Spawner.EnemyType, List<Spawner>> spawners = null;
  28. public void Spawn(Spawner.EnemyType type, int count = 1) {
  29. // 初始化
  30. if (spawners == null) {
  31. spawners = new();
  32. foreach (Spawner spawner in FindObjectsOfType<Spawner>()) {
  33. if (spawners.ContainsKey(spawner.type)) {
  34. spawners[spawner.type].Add(spawner);
  35. } else {
  36. spawners[spawner.type] = new() { spawner };
  37. }
  38. }
  39. }
  40. // 实际刷怪
  41. spawners[type].Shuffle();
  42. for (int i = 0; i < Mathf.Min(6, count); i++) {
  43. spawners[type][i].Spawn();
  44. }
  45. }
  46. void Start() {
  47. StartCoroutine(test());
  48. }
  49. IEnumerator test() {
  50. while (true) {
  51. Spawn(Spawner.EnemyType.Mover);
  52. yield return StartCoroutine(SetStoryText("先刷一个瞎晃悠的", "Eng" , 3f));
  53. Spawn(Spawner.EnemyType.Arrow, 3);
  54. yield return StartCoroutine(SetStoryText("再刷……三个戳脑门的", "", 3f));
  55. Spawn(Spawner.EnemyType.Heavy);
  56. yield return StartCoroutine(SetStoryText("最后是一个贼硬的,完事", "", 3f));
  57. HideStoryText();
  58. yield return new WaitForSeconds(5f);
  59. yield return StartCoroutine(SetStoryText("准备下一轮……", "", 3f));
  60. }
  61. }
  62. }