ModalConfirmView.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Events;
  6. public class ModalConfirmView : MonoBehaviour
  7. {
  8. public string text;
  9. public string textKey;
  10. public object[] textFormatArgs = {};
  11. public UnityAction onConfirm;
  12. public string onConfirmTextKey;
  13. public bool willDestroyAfterClick = true;
  14. public static ModalConfirmView Show()
  15. {
  16. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/ModalConfirmView"));
  17. ModalConfirmView v = o.AddComponent<ModalConfirmView>();
  18. return v;
  19. }
  20. void Start()
  21. {
  22. if (textKey != null) {
  23. TextAutoLanguage2 t2 = transform.Find("Frame/Text").gameObject.AddComponent<TextAutoLanguage2>();
  24. t2.textFormatArgs = textFormatArgs;
  25. t2.SetTextKey(textKey);
  26. } else {
  27. transform.Find("Frame/Text").GetComponent<Text>().text = text;
  28. }
  29. Transform btnConfirm = transform.Find("Frame/BtnConfirm");
  30. btnConfirm.GetComponent<Button>().onClick.AddListener(() => {
  31. onConfirm?.Invoke();
  32. if (willDestroyAfterClick) Destroy(gameObject);
  33. });
  34. if (!string.IsNullOrEmpty(onConfirmTextKey)) {
  35. btnConfirm.GetComponentInChildren<TextAutoLanguage2>().SetTextKey(onConfirmTextKey);
  36. }
  37. }
  38. }