ModalView.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 bool willDestroyAfterClick = true;
  16. public static ModalView Show()
  17. {
  18. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/ModalView"));
  19. ModalView v = o.AddComponent<ModalView>();
  20. return v;
  21. }
  22. void Start()
  23. {
  24. if (textKey != null) {
  25. TextAutoLanguage2 t2 = transform.Find("Frame/Text").gameObject.AddComponent<TextAutoLanguage2>();
  26. t2.textFormatArgs = textFormatArgs;
  27. t2.SetTextKey(textKey);
  28. } else {
  29. transform.Find("Frame/Text").GetComponent<Text>().text = text;
  30. }
  31. Transform btnReject = transform.Find("Frame/BtnReject");
  32. btnReject.GetComponent<Button>().onClick.AddListener(() => {
  33. onReject?.Invoke();
  34. if (willDestroyAfterClick) Destroy(gameObject);
  35. });
  36. Transform btnAgree = transform.Find("Frame/BtnAgree");
  37. btnAgree.GetComponent<Button>().onClick.AddListener(() => {
  38. onAgree?.Invoke();
  39. if (willDestroyAfterClick) Destroy(gameObject);
  40. });
  41. if (!string.IsNullOrEmpty(onAgreeTextKey)) {
  42. btnAgree.GetComponentInChildren<TextAutoLanguage2>().SetTextKey(onAgreeTextKey);
  43. }
  44. if (!string.IsNullOrEmpty(onRejectTextKey)) {
  45. btnReject.GetComponentInChildren<TextAutoLanguage2>().SetTextKey(onRejectTextKey);
  46. }
  47. }
  48. }