123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- using System.Collections;
- using System.Collections.Generic;
- public class FlowerCard : MonoBehaviour
- {
- #region 变量
- public int SelectIndex;
- public int ScrollSelectIndex;
- public bool SelectLock;
- public float LeftBorder;
- public float RightBorder;
- public Transform SeleItem;
- public Transform ScrollSeleItem;
- public List<Transform> ItemList;
- public List<FlowerInfo> InfoList;
- public ScrollRectPlus ScrollRect;
- #endregion
- public void RegistValue()
- {
- InfoList = new List<FlowerInfo>();
- ItemList = new List<Transform>();
- SelectIndex = -1;
- ScrollRect = ManaReso.Get<ScrollRectPlus>("H_Scrr");
- ScrollRect.DragEvent += OnDrag;
- ScrollRect.EndDragEvent += OnEndDrag;
- MoveVec moveVec = ScrollRect.content.CreateMoveVec();
- moveVec.OnStart += () =>
- {
- SelectLock = true;
- };
- moveVec.OnFinish += () =>
- {
- SelectLock = false;
- };
- }
- public void Add(FlowerInfo flowerInfo)
- {
- Transform tra = ManaReso.GetFlowerItemH();
- tra.CreateTweenScale(new Vector3(1, 1, 1), new Vector3(1.25f, 1.25f, 1), 0.25f, true, true, Curve.EaseOutQuad);
-
- Image image = tra.transform.GetChild(0).GetComponent<Image>();
- image.sprite = flowerInfo.Sprite;
- Vector2 newSize = flowerInfo.Sprite.rect.size;
- newSize.x *= 0.2f;
- newSize.y *= 0.2f;
- image.rectTransform.sizeDelta = newSize;
- Button button = tra.GetComponent<Button>();
- button.onClick.AddListener
- (
- () =>
- {
- Select(tra);
- }
- );
-
- InfoList.Add(flowerInfo);
- ItemList.Add(tra);
- }
- public void Close()
- {
- if (ItemList.Valid() == false)
- {
- return;
- }
- SeleItem.TweenBacScale();
- SeleItem = null;
- SelectIndex = -1;
- for (int i = 0; i < ItemList.Count; i++)
- {
- ManaReso.SaveUI(ItemList[i]);
- }
- Vector3 delta = ScrollRect.transform.position - ItemList[0].transform.position;
- delta.y = 0;
- delta.z = 0;
- ScrollRect.content.MoveVec(ScrollRect.content.transform.position + delta, 0.25f, Curve.EaseOutQuad);
- InfoList = new List<FlowerInfo>();
- ItemList = new List<Transform>();
- ManaReso.Get<ContentSizeFitter>("H_Grid").enabled = true;
- ManaReso.Get<HorizontalLayoutGroup>("H_Grid").enabled = true;
- }
- public void Select(int index)
- {
- if (SelectLock)
- {
- return;
- }
-
- if (SelectIndex != index)
- {
- if (SeleItem != null)
- {
- SeleItem.TweenBacScale();
- }
-
- SelectIndex = index;
- SeleItem = ItemList[index];
- SeleItem.TweenForScale();
- SeleItem.transform.SetAsLastSibling();
- ManaGarden.SetFlowerCard(InfoList[index]);
- Vector3 delta = ScrollRect.transform.position - SeleItem.transform.position;
- delta.y = 0;
- delta.z = 0;
- ScrollRect.content.MoveVec(ScrollRect.content.transform.position + delta, 0.25f, Curve.EaseOutQuad);
- }
- }
- public void Select(Transform item)
- {
- Select(ItemList.IndexOf(item));
- }
- public void PrevItem()
- {
- if (SelectIndex > 0)
- {
- Select(SelectIndex - 1);
- }
- }
- public void NextItem()
- {
- if (SelectIndex < ItemList.Count - 1)
- {
- Select(SelectIndex + 1);
- }
- }
- private void OnDrag(PointerEventData eventData)
- {
- if (ItemList.Count > 2)
- {
- ScrollRect.horizontal = true;
- if (ScrollRect.content.position.x < LeftBorder)
- {
- ScrollRect.content.SetX(LeftBorder);
- }
- else if (ScrollRect.content.position.x > RightBorder)
- {
- ScrollRect.content.SetX(RightBorder);
- }
- }
- else
- {
- ScrollRect.horizontal = false;
- }
- float minX = 999999999;
- for (int i = 0; i < ItemList.Count; i++)
- {
- float x = Mathf.Abs(ItemList[i].localPosition.x - ScrollRect.content.InverseTransformPoint(ScrollRect.transform.position).x);
- if (x < minX)
- {
- minX = x;
- ScrollSeleItem = ItemList[i];
- ScrollSelectIndex = i;
- }
- }
- if (ScrollSelectIndex != SelectIndex)
- {
- SeleItem.TweenBacScale();
- ScrollSeleItem.TweenForScale();
- ScrollSeleItem.SetAsLastSibling();
- SeleItem = ScrollSeleItem;
- SelectIndex = ScrollSelectIndex;
- ManaGarden.SetFlowerCard(InfoList[ScrollSelectIndex]);
- }
- }
- private void OnEndDrag(PointerEventData eventData)
- {
- Vector3 delta = ScrollRect.transform.position - SeleItem.transform.position;
- delta.y = 0;
- delta.z = 0;
- ScrollRect.content.MoveVec(ScrollRect.content.transform.position + delta, 0.25f, Curve.EaseOutQuad);
- }
- }
|