GameBehavior.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using UnityEngine.UI;
  6. using UnityEngine.Events;
  7. using UnityEngine.EventSystems;
  8. /// <summary>
  9. /// 游戏中所有UI脚本必须显示继承
  10. /// </summary>
  11. public class GameBehavior : MonoBehaviour
  12. {
  13. /// <summary>
  14. /// OnEnable 后 进入回调
  15. /// </summary>
  16. public System.Action<GameObject> enterAc = null;
  17. /// <summary>
  18. /// 参数
  19. /// </summary>
  20. protected object param = null;
  21. /// <summary>
  22. /// 设置参数
  23. /// </summary>
  24. /// <param name="parameter">参数</param>
  25. public void SetParameter(object parameter)
  26. {
  27. param = parameter;
  28. }
  29. /// <summary>
  30. /// 获取参数
  31. /// </summary>
  32. /// <returns>返回参数</returns>
  33. public object GetParameter()
  34. {
  35. return this.param;
  36. }
  37. public virtual void registListeners()
  38. {
  39. //LogHelper.Log("registListeners");
  40. }
  41. public virtual void distroyListeners()
  42. {
  43. //LogHelper.Log("distroyListeners");
  44. }
  45. public virtual void OnDestroy()
  46. {
  47. distroyListeners();
  48. }
  49. public virtual void OnEnable()
  50. {
  51. //BaseRefresh();
  52. }
  53. public virtual void OnDisable()
  54. {
  55. distroyListeners();
  56. }
  57. public virtual void BaseRefresh()
  58. {
  59. registListeners();
  60. if (enterAc != null)
  61. {
  62. enterAc(this.gameObject);
  63. }
  64. }
  65. /// <summary>
  66. /// 查节点
  67. /// </summary>
  68. /// <typeparam name="T">泛型</typeparam>
  69. /// <param name="go">查询对象</param>
  70. /// <returns>查询结果</returns>
  71. static public T FindInParents<T>(GameObject go) where T : Component
  72. {
  73. if (go == null)
  74. {
  75. return null;
  76. }
  77. var comp = go.GetComponent<T>();
  78. if (comp != null)
  79. {
  80. return comp;
  81. }
  82. Transform t = go.transform.parent;
  83. while (t != null && comp == null)
  84. {
  85. comp = t.gameObject.GetComponent<T>();
  86. t = t.parent;
  87. }
  88. return comp;
  89. }
  90. }