307 lines
10 KiB
C#
307 lines
10 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
namespace Toolset.UI
|
|
{
|
|
/// <summary>
|
|
/// 轮播图菜单
|
|
/// </summary>
|
|
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<CarouselItem> MoveEndAction;
|
|
|
|
private RectTransform rect;
|
|
public List<CarouselItem> itemList = new List<CarouselItem>();
|
|
public List<CarouselItem> itemFrontList = new List<CarouselItem>();
|
|
public List<CarouselItem> itemBackList = new List<CarouselItem>();
|
|
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<RectTransform>();
|
|
for (int i = 0; i < rect.childCount; i++)
|
|
{
|
|
Transform trans = rect.GetChild(i);
|
|
CarouselItem item = trans.GetComponent<CarouselItem>();
|
|
if (item != null)
|
|
{
|
|
//item.transform.GetChild(0).GetComponent<Text>().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<CarouselItem>();
|
|
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<AudioSource>();
|
|
a.clip = Resources.Load<AudioClip>("聊天卷轴纽");
|
|
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>().text == "矿石类")
|
|
{
|
|
UI_manage.inst.show3D_unm = 2;
|
|
UI_manage.inst.show3D.image.overrideSprite = Resources.Load<Sprite>("活株");
|
|
}
|
|
else
|
|
{
|
|
UI_manage.inst.show3D_unm = 1;
|
|
UI_manage.inst.show3D.image.overrideSprite = Resources.Load<Sprite>("切片");
|
|
}
|
|
UI_manage.inst.Grid.GetComponent<GridLayoutGroup>().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<Menuitem>().conf_child;
|
|
item.GetComponent<Menuitem>().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>().text == "矿石类")
|
|
{
|
|
UI_manage.inst.show3D_unm = 2;
|
|
UI_manage.inst.show3D.image.overrideSprite = Resources.Load<Sprite>("活株");
|
|
}
|
|
else
|
|
{
|
|
UI_manage.inst.show3D_unm = 1;
|
|
UI_manage.inst.show3D.image.overrideSprite = Resources.Load<Sprite>("切片");
|
|
}
|
|
StartCoroutine(delayed(button));
|
|
}
|
|
public void Btn_LastPage(Button button)
|
|
{
|
|
if (UI_manage.inst.kind.transform.GetChild(0).GetComponent<Text>().text == "矿石类")
|
|
{
|
|
UI_manage.inst.show3D_unm = 2;
|
|
UI_manage.inst.show3D.image.overrideSprite = Resources.Load<Sprite>("活株");
|
|
}
|
|
else
|
|
{
|
|
UI_manage.inst.show3D_unm = 1;
|
|
UI_manage.inst.show3D.image.overrideSprite = Resources.Load<Sprite>("切片");
|
|
}
|
|
StartCoroutine(delayed_last(button));
|
|
}
|
|
IEnumerator delayed(Button button)
|
|
{
|
|
UI_manage.inst.Grid.GetComponent<GridLayoutGroup>().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<GridLayoutGroup>().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;
|
|
|
|
}
|
|
}
|
|
}
|