UI_ScrollFlow_Item.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public class UI_ScrollFlow_Item : MonoBehaviour
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. private UI_ScrollFlow parent;
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. [HideInInspector]
  19. public RectTransform rect;
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. public List<Image> imgList = new List<Image>();
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. public List<Text> txtList = new List<Text>();
  28. /// <summary>
  29. ///
  30. /// </summary>
  31. public float v = 0;
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. private Vector3 p, s;
  36. /// <summary>
  37. /// 缩放值
  38. /// </summary>
  39. public float sv;
  40. /// <summary>
  41. ///
  42. /// </summary>
  43. private Color color;
  44. /// <summary>
  45. ///
  46. /// </summary>
  47. public int _index;
  48. /// <summary>
  49. /// 元素列表优化对象
  50. /// </summary>
  51. public UI_ScrollFlow ScrollFlowParent
  52. {
  53. set
  54. {
  55. parent = value;
  56. }
  57. }
  58. /// <summary>
  59. /// 拖拽到指定位置
  60. /// 将子对象,指定到时间轴的某个点上.
  61. /// </summary>
  62. /// <param name="value"></param>
  63. public void Drag(float value)
  64. {
  65. //// Debug.LogError("Item drag::" + value + " curV:::" + v);
  66. v += value;
  67. Refresh();
  68. if (Index == 0 && v > parent.startValue)
  69. {
  70. parent.OnReceiveUpdateState(true, this);
  71. return;
  72. }
  73. if (Index == parent.mDataCount - 1 && v < parent.startValue)
  74. {
  75. parent.OnReceiveUpdateState(true, this);
  76. return;
  77. }
  78. float abs = Mathf.Abs(0.5f - v);
  79. if (abs < 0.05f)
  80. {
  81. parent.OnReceiveUpdateState(true, this);
  82. }
  83. else
  84. {
  85. parent.OnReceiveUpdateState(false, this);
  86. }
  87. }
  88. /////// <summary>
  89. /////// 更改 显示的绘制顺序
  90. /////// </summary>
  91. /////// <param name="v"></param>
  92. ////public int ResetSiblingIndex()
  93. ////{
  94. //// int tempIndex = -1;
  95. //// if (Index == parent.CurSelectElementIndex)
  96. //// {
  97. //// tempIndex = parent.mDataCount - 1;
  98. //// }
  99. //// else
  100. //// {
  101. //// if (Index < parent.CurSelectElementIndex)
  102. //// {
  103. //// tempIndex = Index;
  104. //// }
  105. //// else if (Index > parent.CurSelectElementIndex)
  106. //// {
  107. //// tempIndex = parent.mDataCount - (Index + 1) - parent.CurSelectElementIndex;
  108. //// }
  109. //// else
  110. //// {
  111. //// }
  112. //// }
  113. //// //// Debug.LogError("获得卡牌绘制的顺序!! 卡牌::" + Index + " 绘制顺序::" + tempIndex);
  114. //// return tempIndex;
  115. ////}
  116. /// <summary>
  117. /// 指的是刷新,
  118. /// 该子对象在时间轴上
  119. /// 对应的位置等信息
  120. /// </summary>
  121. public void Refresh()
  122. {
  123. if (rect == null)
  124. {
  125. rect = GetComponent<RectTransform>();
  126. }
  127. p = rect.localPosition;
  128. p.x = parent.GetPositionX(v, _index);
  129. rect.localPosition = p;
  130. for (int i = 0; i < imgList.Count; i++)
  131. {
  132. imgList[i].color = new Color(imgList[i].color.r, imgList[i].color.g, imgList[i].color.b, color.a); ;
  133. }
  134. for (int i = 0; i < txtList.Count; i++)
  135. {
  136. txtList[i].color = new Color(txtList[i].color.r, txtList[i].color.g, txtList[i].color.b, color.a);
  137. }
  138. sv = parent.GetScale(v);
  139. s.x = 1;
  140. s.y = sv;
  141. s.z = 1;
  142. rect.localScale = s;
  143. }
  144. /// <summary>
  145. /// 数据索引
  146. /// </summary>
  147. public int Index
  148. {
  149. get
  150. {
  151. return _index;
  152. }
  153. set
  154. {
  155. _index = value;
  156. if (_index < 0)
  157. {
  158. ResetDrag();
  159. }
  160. if (parent.onInitializeItem != null && _index >= 0)
  161. {
  162. parent.onInitializeItem(gameObject, _index);
  163. }
  164. }
  165. }
  166. /// <summary>
  167. ///
  168. /// </summary>
  169. private void ResetDrag()
  170. {
  171. v = 0;
  172. Refresh();
  173. this.gameObject.SetActive(false);
  174. }
  175. }