SceneCmptBubbleWindow.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class SceneCmptBubbleWindow : 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. private void Awake()
  16. {
  17. bg = this.transform.Find("bg").gameObject;
  18. frame = this.transform.Find("frame").gameObject;
  19. content = this.transform.Find("Canvas/Text").GetComponent<Text>();
  20. content.text = text;
  21. curTime = 0;
  22. Hide();
  23. }
  24. // Start is called before the first frame update
  25. void Start()
  26. {
  27. }
  28. // Update is called once per frame
  29. void Update()
  30. {
  31. if(!isEnable)
  32. {
  33. return;
  34. }
  35. curTime += Time.deltaTime;
  36. if(curTime > liveTime)
  37. {
  38. GameObject.Destroy(this.gameObject);
  39. }
  40. }
  41. public void SetContent(string str)
  42. {
  43. text = str;
  44. content.text = text;
  45. isEnable = true;
  46. Show();
  47. }
  48. public void Show()
  49. {
  50. this.bg.SetActive(true);
  51. this.frame.SetActive(true);
  52. this.content.gameObject.SetActive(true);
  53. }
  54. public void Hide()
  55. {
  56. this.bg.SetActive(false);
  57. this.frame.SetActive(false);
  58. this.content.gameObject.SetActive(false);
  59. }
  60. public void OnTriggerEnter(Collider other)
  61. {
  62. if(isAuto && other.gameObject.layer == LayerMask.GetMask("Player"))
  63. {
  64. SetContent(this.text);
  65. }
  66. }
  67. }