PageComp.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. // using DG.Tweening;
  3. using Unity.VisualScripting;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace Plugins.CxShine.page
  7. {
  8. public class PageComp : MonoBehaviour
  9. {
  10. public delegate void onPageClosingListener();
  11. public GameObject playAnimBackGround;
  12. public string pageName;
  13. private bool _isShowAnim;
  14. private Action _onClose;
  15. private onPageClosingListener _onPageClosing;
  16. public Action OnPause;
  17. public Action OnResume;
  18. // Start is called before the first frame update
  19. private void Start()
  20. {
  21. if (playAnimBackGround == null)
  22. // 如果播放动画的背景go没有设置,就是自己
  23. playAnimBackGround = gameObject;
  24. //Debug.Log("启动页面" + pageName);
  25. _initClickEvents();
  26. if (_isShowAnim) _playOpenAnim();
  27. }
  28. // Update is called once per frame
  29. private void Update()
  30. {
  31. }
  32. public void setOnPageCloseListener(Action action)
  33. {
  34. _onClose = action;
  35. }
  36. private void _initClickEvents()
  37. {
  38. // find all 'ClosePageButton' tag add closePageEvent
  39. var trans = gameObject.GetComponentsInChildren<Transform>();
  40. foreach (var t in trans)
  41. if ("ClosePageButton".Equals(t.gameObject.tag))
  42. t.gameObject.GetOrAddComponent<Button>().onClick.AddListener(A__ClosePage);
  43. }
  44. private void _playOpenAnim()
  45. {
  46. var to = playAnimBackGround.transform.localScale;
  47. // transform.SetPositionX(a.x - 50);
  48. playAnimBackGround.transform.localScale = new Vector3(to.x * 0.3f, to.y * 0.3f, to.z);
  49. // transform.SetLocalScaleX(to.x * 0.3f);
  50. // transform.SetLocalScaleY(to.y * 0.3f);
  51. // transform.DOMove(from, 0.5f).SetEase(Ease.Linear);
  52. // playAnimBackGround.transform.DOScale(new Vector3(to.x, to.y, to.z), 0.2f).SetEase(Ease.InSine);
  53. }
  54. public void A__ClosePage()
  55. {
  56. //Debug.Log("关闭页面" + pageName);
  57. if (_onPageClosing != null) _onPageClosing();
  58. _onClose?.Invoke();
  59. if (_isShowAnim)
  60. {
  61. // var to = playAnimBackGround.transform.localScale;
  62. // var t = playAnimBackGround.transform.DOScale(new Vector3(to.x * 0.3f, to.y * 0.3f, to.z), 0.2f)
  63. // .SetEase(Ease.InSine);
  64. // t.onComplete = onComplete;
  65. }
  66. else
  67. {
  68. onComplete();
  69. }
  70. }
  71. // public void setOnPageClosingListener(onPageClosingListener l)
  72. // {
  73. // _onPageClosing = l;
  74. // }
  75. // delegate TweenCallback onPageCloseAnimationFinish;
  76. private void onComplete()
  77. {
  78. var o = gameObject;
  79. if (o != null)
  80. {
  81. o.transform.localPosition = new Vector3(10000, 0, 0);
  82. Destroy(gameObject);
  83. }
  84. // Destroy(o, 0.1f);
  85. }
  86. public void showAnim(bool isShowAnim)
  87. {
  88. _isShowAnim = isShowAnim;
  89. }
  90. }
  91. }