ToastPanelController.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace TapSDK.UI
  4. {
  5. public class ToastPanelOpenParam : IOpenPanelParameter
  6. {
  7. public float popupTime;
  8. public string text;
  9. public ToastPanelOpenParam(string text, float popupTime)
  10. {
  11. this.text = text;
  12. this.popupTime = popupTime;
  13. }
  14. }
  15. public class ToastPanelController : BasePanelController
  16. {
  17. public Text text;
  18. public RectTransform background;
  19. public float fixVal;
  20. public string show;
  21. protected override void BindComponents()
  22. {
  23. base.BindComponents();
  24. text = transform.Find("Root/Text").GetComponent<Text>();
  25. background = transform.Find("Root/BGM") as RectTransform;
  26. }
  27. protected override void OnLoadSuccess()
  28. {
  29. base.OnLoadSuccess();
  30. ToastPanelOpenParam param = this.openParam as ToastPanelOpenParam;
  31. if (param != null)
  32. {
  33. text.text = param.text;
  34. var totalLength = CalculateLengthOfText();
  35. var x = totalLength;
  36. var y = background.sizeDelta.y;
  37. background.sizeDelta = new Vector2(x, y);
  38. this.Invoke("Close", param.popupTime);
  39. }
  40. }
  41. private float CalculateLengthOfText()
  42. {
  43. var width = text.preferredWidth + fixVal;
  44. width = Mathf.Max(200, width);
  45. width = Mathf.Min(Screen.width, width);
  46. return width;
  47. }
  48. }
  49. }