using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace Toolset.UI { /// /// 轮播图菜单 /// public class CarouselMenu : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler { public float width = 1000; public float maxScale = 1; public float startValue = 0.1f; public float addValue = 0.2f; public float minValue = 0.1f; public float maxValue = 0.9f; public float rollSpeed = 1; public AnimationCurve positionCurve; public AnimationCurve scaleCurve; public AnimationCurve alphaCurve; public Action MoveEndAction; private RectTransform rect; public List itemList = new List(); public List itemFrontList = new List(); public List itemBackList = new List(); bool isRoll = false; public CarouselItem current; public Vector2 startPoint; public Vector2 addVect; BaseConf conf; private void Start() { //Refresh(); MoveEndAction += SelectItem; } private void Update() { if (isRoll) { currentV = Time.deltaTime * rollSpeed * vk; vt = vTotal + currentV; if (vk > 0 && vt >= addV) { isRoll = false; currentV = addV - vTotal; } if (vk < 0 && vt <= addV) { isRoll = false; currentV = addV - vTotal; } for (int i = 0; i < itemList.Count; i++) { itemList[i].CalculateTrans(currentV); if (itemList[i].v - 0.5f < 0.05f) { current = itemList[i]; } } Check(currentV); vTotal = vt; if (!isRoll) { MoveEndAction?.Invoke(current); } } //if(Input.GetKeyDown(KeyCode.A)) //{ // itemList.Clear(); //} } public void Refresh() { rect = GetComponent(); for (int i = 0; i < rect.childCount; i++) { Transform trans = rect.GetChild(i); CarouselItem item = trans.GetComponent(); if (item != null) { //item.transform.GetChild(0).GetComponent().text = i.ToString(); itemList.Add(item); item.Init(this); item.CalculateTrans(startValue + i * addValue); if (item.v - 0.5f < 0.05f) { current = itemList[i]; } } } if (rect.childCount < 3) { maxValue = startValue + 3 * addValue; } else { maxValue = startValue + (rect.childCount - 1) * addValue; } } public float GetAlpha(float v) { return alphaCurve.Evaluate(v); } public float GetPosition(float v) { return positionCurve.Evaluate(v) * width; } public float GetScale(float v) { return scaleCurve.Evaluate(v) * maxScale; } public void OnBeginDrag(PointerEventData eventData) { startPoint = eventData.position; addVect = Vector2.zero; isRoll = false; } public void OnDrag(PointerEventData eventData) { addVect = eventData.position - startPoint; float v = eventData.delta.y * 1.0f / width; for (int i = 0; i < itemList.Count; i++) { itemList[i].CalculateTrans(v); } Check(v); } public void OnEndDrag(PointerEventData eventData) { float k = 0, vTemp; for (int i = 0; i < itemList.Count; i++) { if (itemList[i].v >= minValue) { vTemp = (itemList[i].v - minValue) % addValue; if (addVect.x > 0) { k = addValue - vTemp; } else { k = vTemp * -1; } break; } } addVect = Vector2.zero; AnimToEnd(k); } public void OnPointerClick(PointerEventData eventData) { if (addVect.sqrMagnitude <= 1) { CarouselItem item = eventData.pointerPressRaycast.gameObject.GetComponent(); if (item != null) { float k = item.v; k = 0.5f - k; AnimToEnd(k); //if (k > 0.5f) //{ // Btn_NextPage(UI_manage.inst.menuitem_Down_bt); //} //else if(k < 0.5f) //{ // Btn_LastPage(UI_manage.inst.menuitem_UP_bt); //} } } print(1111111111); } public float addV = 0, vk = 0, currentV = 0, vTotal = 0, vt = 0; private void AnimToEnd(float k) { AudioSource a = Camera.main.GetComponent(); a.clip = Resources.Load("聊天卷轴纽"); a.Play(); addV = k; if (k > 0) vk = 1; else if (k < 0) vk = -1; else return; vTotal = 0; isRoll = true; } private void Check(float _v) { if (UI_manage.inst.kind.transform.GetChild(0).GetComponent().text == "矿石类") { UI_manage.inst.show3D_unm = 2; UI_manage.inst.show3D.image.overrideSprite = Resources.Load("活株"); } else { UI_manage.inst.show3D_unm = 1; UI_manage.inst.show3D.image.overrideSprite = Resources.Load("切片"); } UI_manage.inst.Grid.GetComponent().enabled = false; UI_manage.inst.Grid.transform.localPosition = new Vector3(-4, -716.45f, 0); if (_v < 0)//向左滑动-下 { for (int i = 0; i < itemList.Count; i++) { if (itemList[i].v < (minValue - addValue / 2)) { itemBackList.Add(itemList[i]); } } if (itemBackList.Count > 0) { for (int i = 0; i < itemBackList.Count; i++) { itemBackList[i].v = itemList[itemList.Count - 1].v + addValue; itemList.Remove(itemBackList[i]); itemList.Add(itemBackList[i]); } itemBackList.Clear(); } } else if (_v > 0)//向右滑动-上 { for (int i = itemList.Count - 1; i > 0; i--) { if (itemList[i].v > maxValue) { itemFrontList.Add(itemList[i]); } } if (itemFrontList.Count > 0) { for (int i = 0; i < itemFrontList.Count; i++) { itemFrontList[i].v = itemList[0].v - addValue; itemList.Remove(itemFrontList[i]); itemList.Insert(0, itemFrontList[i]); } itemFrontList.Clear(); } } } public void SelectItem(CarouselItem item) { conf = item.GetComponent().conf_child; item.GetComponent().OnPointerClick(conf); UI_manage.inst.name_current.text = conf.name_CN; print(item.name); } void OnDestroy() { MoveEndAction -= SelectItem; } public void Btn_NextPage(Button button) { if (UI_manage.inst.kind.transform.GetChild(0).GetComponent().text == "矿石类") { UI_manage.inst.show3D_unm = 2; UI_manage.inst.show3D.image.overrideSprite = Resources.Load("活株"); } else { UI_manage.inst.show3D_unm = 1; UI_manage.inst.show3D.image.overrideSprite = Resources.Load("切片"); } StartCoroutine(delayed(button)); } public void Btn_LastPage(Button button) { if (UI_manage.inst.kind.transform.GetChild(0).GetComponent().text == "矿石类") { UI_manage.inst.show3D_unm = 2; UI_manage.inst.show3D.image.overrideSprite = Resources.Load("活株"); } else { UI_manage.inst.show3D_unm = 1; UI_manage.inst.show3D.image.overrideSprite = Resources.Load("切片"); } StartCoroutine(delayed_last(button)); } IEnumerator delayed(Button button) { UI_manage.inst.Grid.GetComponent().enabled = false; UI_manage.inst.Grid.transform.localPosition = new Vector3(-4, -716.45f, 0); button.interactable = false; AnimToEnd(-0.4f); yield return new WaitForSeconds(0.4f); button.interactable = true; }IEnumerator delayed_last(Button button) { UI_manage.inst.Grid.GetComponent().enabled = false; UI_manage.inst.Grid.transform.localPosition = new Vector3(-4, -716.45f, 0); button.interactable = false; AnimToEnd(0.4f); yield return new WaitForSeconds(0.4f); button.interactable = true; } } }