ModalView.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Events;
  6. public class ModalView : MonoBehaviour
  7. {
  8. public string text;
  9. public string textKey;
  10. public object[] textFormatArgs = {};
  11. public UnityAction onAgree;
  12. public string onAgreeTextKey;
  13. public UnityAction onReject;
  14. public string onRejectTextKey;
  15. public static ModalView Show()
  16. {
  17. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/ModalView"));
  18. ModalView v = o.AddComponent<ModalView>();
  19. return v;
  20. }
  21. void Start()
  22. {
  23. if (textKey != null) {
  24. TextAutoLanguage2 t2 = transform.Find("Frame/Text").gameObject.AddComponent<TextAutoLanguage2>();
  25. t2.textFormatArgs = textFormatArgs;
  26. t2.SetTextKey(textKey);
  27. } else {
  28. transform.Find("Frame/Text").GetComponent<Text>().text = text;
  29. }
  30. Transform btnReject = transform.Find("Frame/BtnReject");
  31. btnReject.GetComponent<Button>().onClick.AddListener(() => {
  32. onReject?.Invoke();
  33. Destroy(gameObject);
  34. });
  35. Transform btnAgree = transform.Find("Frame/BtnAgree");
  36. btnAgree.GetComponent<Button>().onClick.AddListener(() => {
  37. onAgree?.Invoke();
  38. Destroy(gameObject);
  39. });
  40. if (!string.IsNullOrEmpty(onAgreeTextKey)) {
  41. btnAgree.GetComponentInChildren<TextAutoLanguage2>().SetTextKey(onAgreeTextKey);
  42. }
  43. if (!string.IsNullOrEmpty(onRejectTextKey)) {
  44. btnReject.GetComponentInChildren<TextAutoLanguage2>().SetTextKey(onRejectTextKey);
  45. }
  46. }
  47. }