FlowerCard.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Events;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine.EventSystems;
  7. public class FlowerCard : MonoBehaviour
  8. {
  9. #region 变量
  10. public int SelectIndex;
  11. public int ScrollSelectIndex;
  12. public bool SelectLock;
  13. public float LeftBorder;
  14. public float RightBorder;
  15. public Transform SeleItem;
  16. public Transform ScrollSeleItem;
  17. public List<Transform> ItemList;
  18. public List<FlowerInfo> InfoList;
  19. public NewScrollRect ScrollRect;
  20. #endregion
  21. public void RegistValue()
  22. {
  23. InfoList = new List<FlowerInfo>();
  24. ItemList = new List<Transform>();
  25. SelectIndex = -1;
  26. ScrollRect = ManaReso.Get<NewScrollRect>("H_Scrr");
  27. ScrollRect.DragEvent += OnDrag;
  28. ScrollRect.EndDragEvent += OnEndDrag;
  29. MoveVec moveVec = ScrollRect.content.CreateMoveVec();
  30. moveVec.OnStart += () =>
  31. {
  32. SelectLock = true;
  33. };
  34. moveVec.OnFinish += () =>
  35. {
  36. SelectLock = false;
  37. };
  38. }
  39. public void Add(FlowerInfo flowerInfo)
  40. {
  41. Transform tra = ManaReso.GetFlowerCardItem();
  42. tra.CreateTweenScale(new Vector3(1, 1, 1), new Vector3(1.25f, 1.25f, 1), 0.25f, true, true, Curve.EaseOutQuad);
  43. Image image = tra.transform.GetChild(0).GetComponent<Image>();
  44. image.sprite = flowerInfo.Sprite;
  45. Vector2 newSize = flowerInfo.Sprite.rect.size;
  46. newSize.x *= 0.2f;
  47. newSize.y *= 0.2f;
  48. image.rectTransform.sizeDelta = newSize;
  49. Button button = tra.GetComponent<Button>();
  50. button.onClick.AddListener
  51. (
  52. () =>
  53. {
  54. Select(tra);
  55. }
  56. );
  57. InfoList.Add(flowerInfo);
  58. ItemList.Add(tra);
  59. }
  60. public void Select(int index)
  61. {
  62. if (SelectLock)
  63. {
  64. return;
  65. }
  66. if (SelectIndex != index)
  67. {
  68. if (SeleItem != null)
  69. {
  70. SeleItem.TweenBacScale();
  71. }
  72. SelectIndex = index;
  73. SeleItem = ItemList[index];
  74. SeleItem.TweenForScale();
  75. SeleItem.transform.SetAsLastSibling();
  76. ManaGarden.SetFlowerCard(InfoList[index].Id);
  77. Vector3 delta = ScrollRect.transform.position - SeleItem.transform.position;
  78. delta.y = 0;
  79. delta.z = 0;
  80. ScrollRect.content.MoveVec(ScrollRect.content.transform.position + delta, 0.25f, Curve.EaseOutQuad);
  81. }
  82. }
  83. public void Select(Transform item)
  84. {
  85. Select(ItemList.IndexOf(item));
  86. }
  87. public void Close()
  88. {
  89. if (ItemList == null)
  90. {
  91. return;
  92. }
  93. if (SeleItem != null)
  94. {
  95. SeleItem.TweenBacScale();
  96. SeleItem = null;
  97. }
  98. SelectIndex = -1;
  99. if (ItemList.Count > 0)
  100. {
  101. Vector3 delta = ScrollRect.transform.position - ItemList[0].transform.position;
  102. delta.y = 0;
  103. delta.z = 0;
  104. ScrollRect.content.MoveVec(ScrollRect.content.transform.position + delta, 0.25f, Curve.EaseOutQuad);
  105. }
  106. for (int i = 0; i < ItemList.Count; i++)
  107. {
  108. ManaReso.SaveUI(ItemList[i]);
  109. }
  110. InfoList = new List<FlowerInfo>();
  111. ItemList = new List<Transform>();
  112. ManaReso.Get<ContentSizeFitter>("H_Grid").enabled = true;
  113. ManaReso.Get<HorizontalLayoutGroup>("H_Grid").enabled = true;
  114. }
  115. public void PrevItem()
  116. {
  117. if (SelectIndex > 0)
  118. {
  119. Select(SelectIndex - 1);
  120. }
  121. }
  122. public void NextItem()
  123. {
  124. if (SelectIndex < ItemList.Count - 1)
  125. {
  126. Select(SelectIndex + 1);
  127. }
  128. }
  129. public void OnDrag(PointerEventData eventData)
  130. {
  131. if (ItemList.Count >= 3)
  132. {
  133. ScrollRect.horizontal = true;
  134. if (ScrollRect.content.position.x < LeftBorder)
  135. {
  136. ScrollRect.content.SetX(LeftBorder);
  137. }
  138. else if (ScrollRect.content.position.x > RightBorder)
  139. {
  140. ScrollRect.content.SetX(RightBorder);
  141. }
  142. }
  143. else
  144. {
  145. ScrollRect.horizontal = false;
  146. }
  147. float minX = 999999999;
  148. for (int i = 0; i < ItemList.Count; i++)
  149. {
  150. float x = Mathf.Abs(ItemList[i].localPosition.x - ScrollRect.content.InverseTransformPoint(ScrollRect.transform.position).x);
  151. if (x < minX)
  152. {
  153. minX = x;
  154. ScrollSeleItem = ItemList[i];
  155. ScrollSelectIndex = i;
  156. }
  157. }
  158. if (ScrollSelectIndex != SelectIndex)
  159. {
  160. SeleItem.TweenBacScale();
  161. ScrollSeleItem.TweenForScale();
  162. ScrollSeleItem.SetAsLastSibling();
  163. ManaGarden.SetFlowerCard(InfoList[ScrollSelectIndex].Id);
  164. SeleItem = ScrollSeleItem;
  165. SelectIndex = ScrollSelectIndex;
  166. }
  167. }
  168. public void OnEndDrag(PointerEventData eventData)
  169. {
  170. //float minX = 999999999;
  171. //for (int i = 0; i < ItemList.Count; i++)
  172. //{
  173. // float x = Mathf.Abs(ItemList[i].localPosition.x - ScrollRect.content.InverseTransformPoint(ScrollRect.transform.position).x);
  174. // if (x < minX)
  175. // {
  176. // minX = x;
  177. // ScrollSelectIndex = i;
  178. // }
  179. //}
  180. //Select(ScrollSelectIndex);
  181. }
  182. }