SceneBubbleWindow.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class SceneBubbleWindow : MonoBehaviour
  6. {
  7. public string[] text;
  8. public bool isAuto = false;
  9. GameObject bg;
  10. GameObject frame;
  11. Text content;
  12. float liveTime = 3.0f;
  13. float curTime = 0;
  14. bool isEnable = false;
  15. YLBattle.SpawnPoint.BubbleType bubbleType = YLBattle.SpawnPoint.BubbleType.Area;
  16. int nowIndex = 0;
  17. bool isShow = false;
  18. private void Awake()
  19. {
  20. return;
  21. if(text == null)
  22. {
  23. text = new string[1];
  24. }
  25. if(text.Length == 0)
  26. {
  27. return;
  28. }
  29. ResourceHelper.Instance.LoadAssetBundle("SceneBubbleWindow", ab =>
  30. {
  31. if (ab != null)
  32. {
  33. GameObject bubble = (GameObject)Instantiate(ab.LoadAsset("SceneBubbleWindow"));
  34. bubble.transform.SetParent(this.transform);
  35. bubble.transform.localPosition = Vector3.zero;
  36. bubble.transform.SetLocalPositionY(2.0f);
  37. TransformRotation rtCmpt = bubble.AddComponent<TransformRotation>();
  38. bg = bubble.transform.Find("Canvas/Text/bg").gameObject;
  39. frame = bubble.transform.Find("frame").gameObject;
  40. content = bubble.transform.Find("Canvas/Text").GetComponent<Text>();
  41. content.text = text[nowIndex];
  42. curTime = 0;
  43. Hide();
  44. }
  45. });
  46. }
  47. // Start is called before the first frame update
  48. void Start()
  49. {
  50. }
  51. // Update is called once per frame
  52. void Update()
  53. {
  54. if(!isEnable)
  55. {
  56. return;
  57. }
  58. curTime += Time.deltaTime;
  59. if(curTime > liveTime)
  60. {
  61. //GameObject.Destroy(this.gameObject);
  62. if(isShow)
  63. {
  64. Hide();
  65. }
  66. else
  67. {
  68. Show();
  69. }
  70. curTime = 0;
  71. }
  72. }
  73. public void SetContent(string str)
  74. {
  75. if(text == null)
  76. {
  77. text = new string[1];
  78. }
  79. text[0] = str;
  80. }
  81. public void SetEnableType(YLBattle.SpawnPoint.BubbleType bt)
  82. {
  83. bubbleType = bt;
  84. if(bubbleType == YLBattle.SpawnPoint.BubbleType.Create)
  85. {
  86. isEnable = false;
  87. }
  88. }
  89. public void Show()
  90. {
  91. this.isEnable = true;
  92. if(bg == null)
  93. {
  94. return;
  95. }
  96. this.bg.SetActive(true);
  97. this.frame.SetActive(true);
  98. this.content.gameObject.SetActive(true);
  99. this.content.text = text[nowIndex];
  100. ++nowIndex;
  101. if(nowIndex >= text.Length)
  102. {
  103. nowIndex = 0;
  104. }
  105. this.isShow = true;
  106. }
  107. public void Hide()
  108. {
  109. if (bg == null)
  110. {
  111. return;
  112. }
  113. this.bg.SetActive(false);
  114. this.frame.SetActive(false);
  115. this.content.gameObject.SetActive(false);
  116. this.isShow = false;
  117. }
  118. public void OnTriggerEnter(Collider other)
  119. {
  120. if(isAuto && other.gameObject.layer == LayerMask.NameToLayer("Player"))
  121. {
  122. SetContent(this.text[nowIndex]);
  123. Show();
  124. }
  125. }
  126. }