UIComponent.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. [SerializeField]
  5. public enum UILayer{
  6. none = -1,
  7. lower = 0,
  8. mid = 1,
  9. up = 2
  10. }
  11. public class UIComponent : MonoBehaviour {
  12. [SerializeField]
  13. public UILayer uiLayer = UILayer.none;
  14. public bool isFull = false;
  15. public bool ignoreRayCast = false;
  16. public float zAdd = 0;
  17. public int requestOrder = 0;
  18. public Animator startAnimator;
  19. public Animation startAnimation;
  20. public SystemDelegate.VoidDelegate focusDel;
  21. public SystemDelegate.VoidDelegate postAnimDel;
  22. public bool mIsInit = false;
  23. public bool isPreLoad = false;
  24. public bool isFinishAnim = false;
  25. public float z{
  26. get{
  27. if (gameObject.GetComponent<RectTransform>()!=null ){
  28. return gameObject.GetComponent<RectTransform>().localPosition.z;
  29. }
  30. return transform.localPosition.z;
  31. }
  32. set{
  33. // if (gameObject.GetComponent<RectTransform>()!=null ){
  34. // RectTransform rectTrans = gameObject.GetComponent<RectTransform>();
  35. // Vector3 pos = rectTrans.localPosition;
  36. // pos.z = value;
  37. // rectTrans.localPosition = pos;
  38. // return;
  39. // }
  40. Vector3 posTrans = transform.localPosition;
  41. posTrans.z = value;
  42. transform.localPosition = posTrans;
  43. }
  44. }
  45. public int curOrder{
  46. get{
  47. Canvas canvas = gameObject.GetComponent<Canvas>();
  48. if (canvas!=null){
  49. return canvas.sortingOrder;
  50. }
  51. return 0;
  52. }
  53. set{
  54. Canvas canvas = gameObject.GetComponent<Canvas>();
  55. if (canvas==null ){
  56. canvas = gameObject.AddComponent<Canvas>();
  57. if(!ignoreRayCast)
  58. Tools.GetOrCreateComponent<GraphicRaycaster>(gameObject);
  59. }
  60. canvas.overrideSorting = true;
  61. canvas.sortingOrder = value;
  62. Canvas[] canvases = gameObject.GetComponentsInChildren<Canvas>(true );
  63. foreach(Canvas preCanvas in canvases ){
  64. if (preCanvas.gameObject == gameObject){
  65. continue;
  66. }
  67. preCanvas.sortingOrder += canvas.sortingOrder;
  68. //Debug.LogWarning(preCanvas.sortingOrder + "add once in canvas order " + preCanvas.gameObject.name );
  69. }
  70. }
  71. }
  72. public void FindButton()
  73. {
  74. Button[] childs = gameObject.GetComponentsInChildren<Button>(true);
  75. foreach (Button child in childs)
  76. {
  77. AddaudioSource(child);
  78. }
  79. }
  80. public void AddaudioSource(Button btn)
  81. {
  82. ButtonAudio btnAudio = btn.gameObject.GetComponent<ButtonAudio>();
  83. if (btnAudio == null)
  84. btnAudio = btn.gameObject.AddComponent<ButtonAudio>();
  85. }
  86. public void SetInit( ){
  87. mIsInit = true;
  88. }
  89. public void OnResume(){
  90. if (focusDel!=null ){
  91. focusDel();
  92. }
  93. }
  94. public void OnDestroy()
  95. {
  96. if (isPreLoad){
  97. return;
  98. }
  99. UIManager.GetInstance().DestroyUI(this );
  100. }
  101. // Use this for initialization
  102. void Start() {
  103. if (!mIsInit ){
  104. UIManager.GetInstance().AddUI(this );
  105. }
  106. StartCoroutine(DelayStartAnim() );
  107. FindButton();
  108. }
  109. public void CloseAnim(){
  110. if (startAnimation != null ){
  111. DestroyImmediate(startAnimation);
  112. startAnimation = null;
  113. }
  114. if (startAnimator != null ){
  115. DestroyImmediate(startAnimator);
  116. startAnimator = null;
  117. }
  118. }
  119. IEnumerator DelayStartAnim(){
  120. float time = Time.time;
  121. while(startAnimation!=null&&startAnimation.isPlaying ){
  122. yield return 1;
  123. }
  124. while(startAnimator!=null&&Tools.IsAnimatorPlay(startAnimator )){
  125. yield return 1;
  126. }
  127. yield return 1;
  128. isFinishAnim = true;
  129. //Debug.LogWarning("call post anim " + (Time.time - time ));
  130. if (postAnimDel!=null ){
  131. postAnimDel();
  132. }
  133. }
  134. // Update is called once per frame
  135. void Update() {
  136. }
  137. }