UiLoopAutoMove.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LuaInterface;
  5. public delegate void LuaAutoMoveCallback(UnityEngine.UI.Text Text);
  6. public class UiLoopAutoMove : MonoBehaviour
  7. {
  8. public GameObject[] mArrayItem;
  9. public RectTransform[] mArrayRect;
  10. public float[] defaultPosArr;
  11. public float fHeight = 40.0f;
  12. private float fAllHeight = 40.0f;
  13. public float fMoveSpeed = 10.0f;
  14. private bool bIsRun = false;
  15. private LuaAutoMoveCallback mLuaOnSelectedCallback = null;
  16. void Start()
  17. {
  18. mArrayRect = new RectTransform[mArrayItem.Length];
  19. defaultPosArr = new float[mArrayItem.Length];
  20. for (int i = 0; i < mArrayItem.Length; i++)
  21. {
  22. mArrayRect[i] = mArrayItem[i].GetComponent<RectTransform>();
  23. defaultPosArr[i] = mArrayRect[i].localPosition.y;
  24. }
  25. }
  26. void Update()
  27. {
  28. if(bIsRun)
  29. {
  30. Move();
  31. }
  32. }
  33. public void Reset() {
  34. for (int i = 0; i < mArrayItem.Length; i++)
  35. {
  36. Vector2 v2MovePos = mArrayRect[i].localPosition;
  37. v2MovePos.y = defaultPosArr[i];
  38. mArrayRect[i].localPosition = v2MovePos;
  39. UnityEngine.UI.Text UiText = mArrayItem[i].GetComponentInChildren<UnityEngine.UI.Text>();
  40. if (mLuaOnSelectedCallback != null)
  41. {
  42. mLuaOnSelectedCallback.DynamicInvoke(UiText);
  43. }
  44. }
  45. }
  46. public void SetText(int nIndex ,string strInfo)
  47. {
  48. if (null != mArrayItem)
  49. {
  50. fAllHeight = fHeight * mArrayItem.Length;
  51. }
  52. if (nIndex > mArrayItem.Length)
  53. return;
  54. UnityEngine.UI.Text UiText = mArrayItem[nIndex].GetComponentInChildren<UnityEngine.UI.Text>();
  55. if (null != UiText)
  56. {
  57. UiText.text = strInfo;
  58. }
  59. }
  60. public void Init(string[] ArrayStrInfo)
  61. {
  62. if (null != mArrayItem)
  63. {
  64. fAllHeight = fHeight * mArrayItem.Length;
  65. }
  66. int nMinLenth = ArrayStrInfo.Length > mArrayItem.Length ? mArrayItem.Length : ArrayStrInfo.Length;
  67. for (int i = 0; i < nMinLenth; i++)
  68. {
  69. UnityEngine.UI.Text UiText = mArrayItem[i].GetComponentInChildren<UnityEngine.UI.Text>();
  70. if (null != UiText)
  71. {
  72. UiText.text = ArrayStrInfo[i];
  73. }
  74. }
  75. }
  76. public void RunMove()
  77. {
  78. bIsRun = true;
  79. }
  80. public void StopMove()
  81. {
  82. bIsRun = false;
  83. }
  84. public void SetChangeInfoCb(LuaAutoMoveCallback cb)
  85. {
  86. mLuaOnSelectedCallback = cb;
  87. }
  88. private void Move()
  89. {
  90. if (null != mArrayItem)
  91. for(int i=0;i< mArrayItem.Length;i++)
  92. {
  93. RectTransform RectTrans = mArrayItem[i].GetComponentInChildren<RectTransform>();
  94. Vector2 v2MovePos = RectTrans.anchoredPosition;
  95. v2MovePos.y += fMoveSpeed * Time.deltaTime;
  96. RectTrans.anchoredPosition = v2MovePos;
  97. }
  98. Refresh();
  99. }
  100. private void Refresh()
  101. {
  102. for (int i = 0; i < mArrayItem.Length; i++)
  103. {
  104. RectTransform RectTrans = mArrayItem[i].GetComponentInChildren<RectTransform>();
  105. Vector2 v2MovePos = RectTrans.anchoredPosition;
  106. if(v2MovePos.y > fHeight * 0.5f)
  107. {
  108. ChangeInfo(i);
  109. }
  110. }
  111. }
  112. private void ChangeInfo(int nIndex)
  113. {
  114. if(null != mArrayItem)
  115. if (mArrayItem.Length > nIndex)
  116. {
  117. //set Pos
  118. int nNext = GetNext(nIndex);
  119. //Debug.Log("Change"+nIndex.ToString());
  120. //Debug.Log("nNext" + nNext.ToString());
  121. RectTransform RectNextTrans = mArrayItem[nNext].GetComponentInChildren<RectTransform>();
  122. RectTransform RectTrans = mArrayItem[nIndex].GetComponentInChildren<RectTransform>();
  123. Vector2 v2MovePos = RectTrans.anchoredPosition;
  124. float fOffset = fHeight;
  125. v2MovePos.y = RectNextTrans.anchoredPosition.y - fOffset;
  126. RectTrans.anchoredPosition = v2MovePos;
  127. UnityEngine.UI.Text UiText = mArrayItem[nIndex].GetComponentInChildren<UnityEngine.UI.Text>();
  128. if (mLuaOnSelectedCallback != null)
  129. {
  130. mLuaOnSelectedCallback.DynamicInvoke(UiText);
  131. }
  132. }
  133. }
  134. private int GetNext(int nIndex)
  135. {
  136. nIndex += 1;
  137. int nNext = ((mArrayItem.Length - 1) + nIndex) % mArrayItem.Length;
  138. nNext -= 1;
  139. if (nNext < 0)
  140. nNext = mArrayItem.Length - 1;
  141. return nNext;
  142. }
  143. }