158 lines
5.8 KiB
C#
158 lines
5.8 KiB
C#
using Assets.Scripts;
|
|
using Assets.Scripts.Apis.Models;
|
|
using DG.Tweening;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class BannerController : MonoBehaviour
|
|
{
|
|
// Start is called before the first frame update
|
|
private List<CanvasGroup> itemList;
|
|
private List<Vector3> standardPositions;
|
|
private List<MapRouteAreaItem> list;
|
|
private int currentIndex = 1;
|
|
void Awake()
|
|
{
|
|
itemList = new List<CanvasGroup>
|
|
{
|
|
transform.Find("1").GetComponent<CanvasGroup>(),
|
|
transform.Find("2").GetComponent<CanvasGroup>(),
|
|
transform.Find("3").GetComponent<CanvasGroup>()
|
|
};
|
|
standardPositions = new List<Vector3>
|
|
{
|
|
transform.Find("1").localPosition,transform.Find("2").localPosition,transform.Find("3").localPosition
|
|
};
|
|
|
|
Debug.Log(standardPositions[0]);
|
|
Debug.Log(standardPositions[1]);
|
|
Debug.Log(standardPositions[2]);
|
|
}
|
|
private void Start()
|
|
{
|
|
GetList();
|
|
}
|
|
async void GetList()
|
|
{
|
|
var res = await ConfigHelper.mapApi.GetRecommendAreaList();
|
|
if (res.result)
|
|
{
|
|
if (res.data.Count >= 3)
|
|
{
|
|
Initial(res.data);
|
|
gameObject.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Initial(List<MapRouteAreaItem> list)
|
|
{
|
|
int index = 0;
|
|
foreach (CanvasGroup c in itemList)
|
|
{
|
|
var area = list[index++];
|
|
c.GetComponent<NewRouteItemController>().Initial(area);
|
|
if (c.alpha != 1)
|
|
{
|
|
c.GetComponent<Button>().onClick.RemoveAllListeners();
|
|
if (c.transform.localPosition.x < 0)
|
|
{
|
|
c.GetComponent<Button>().onClick.AddListener(Right);
|
|
}
|
|
else
|
|
{
|
|
c.GetComponent<Button>().onClick.AddListener(Left);
|
|
}
|
|
}
|
|
}
|
|
currentIndex = 1;
|
|
this.list = list;
|
|
}
|
|
private void Right()
|
|
{
|
|
int center = GetCenterIndex(),
|
|
left = GetSideIndex(true),
|
|
right = GetSideIndex(false);
|
|
if (center == -1 || left == -1 || right == -1) return;
|
|
itemList[center].DOFade(0.5f, 0.5f);
|
|
itemList[center].GetComponent<RectTransform>().DOScale(Vector3.one * 0.8f, 0.5f);
|
|
itemList[center].GetComponent<RectTransform>().DOLocalMoveX(43, 0.5f);
|
|
itemList[right].GetComponent<RectTransform>().DOLocalMoveX(-43, 0.5f);
|
|
var area = list[((currentIndex++) + list.Count) % list.Count];
|
|
itemList[right].GetComponent<NewRouteItemController>().Initial(area);
|
|
if (currentIndex >= list.Count) currentIndex = 0;
|
|
itemList[left].DOFade(1, 0.5f);
|
|
itemList[left].GetComponent<RectTransform>().DOScale(Vector3.one, 0.5f);
|
|
itemList[left].GetComponent<RectTransform>().DOLocalMoveX(0, 0.5f);
|
|
itemList[left].transform.SetAsLastSibling();
|
|
//事件改变
|
|
itemList[left].GetComponent<Button>().onClick.RemoveAllListeners();
|
|
itemList[left].GetComponent<Button>().onClick.AddListener(()=>
|
|
{
|
|
if (itemList[left].transform.localPosition.x == 0)
|
|
{
|
|
UIManager.ShowNewRouteDetailPanel(itemList[left].GetComponent<NewRouteItemController>().Area);
|
|
}
|
|
});
|
|
itemList[center].GetComponent<Button>().onClick.RemoveAllListeners();
|
|
itemList[center].GetComponent<Button>().onClick.AddListener(Left);
|
|
itemList[right].GetComponent<Button>().onClick.RemoveAllListeners();
|
|
itemList[right].GetComponent<Button>().onClick.AddListener(Right);
|
|
}
|
|
|
|
private void Left()
|
|
{
|
|
int center = GetCenterIndex(),
|
|
left = GetSideIndex(true),
|
|
right = GetSideIndex(false);
|
|
if (center == -1 || left == -1 || right == -1) return;
|
|
itemList[center].DOFade(0.5f, 0.5f);
|
|
itemList[center].GetComponent<RectTransform>().DOScale(Vector3.one*0.8f, 0.5f);
|
|
itemList[center].GetComponent<RectTransform>().DOLocalMoveX(-43, 0.5f);
|
|
itemList[left].GetComponent<RectTransform>().DOLocalMoveX(43, 0.5f);
|
|
var area = list[((currentIndex--) + list.Count) % list.Count];
|
|
var centerArea = list[currentIndex + 1];
|
|
itemList[left].GetComponent<NewRouteItemController>().Initial(area);
|
|
if (currentIndex < 0) currentIndex = list.Count - 1;
|
|
itemList[right].DOFade(1, 0.5f);
|
|
itemList[right].GetComponent<RectTransform>().DOScale(Vector3.one, 0.5f);
|
|
itemList[right].GetComponent<RectTransform>().DOLocalMoveX(0, 0.5f);
|
|
itemList[right].transform.SetAsLastSibling();
|
|
//事件改变
|
|
itemList[right].GetComponent<Button>().onClick.RemoveAllListeners();
|
|
itemList[right].GetComponent<Button>().onClick.AddListener(() =>
|
|
{
|
|
if (itemList[right].transform.localPosition.x == 0)
|
|
{
|
|
UIManager.ShowNewRouteDetailPanel(itemList[right].GetComponent<NewRouteItemController>().Area);
|
|
}
|
|
});
|
|
itemList[center].GetComponent<Button>().onClick.RemoveAllListeners();
|
|
itemList[center].GetComponent<Button>().onClick.AddListener(Right);
|
|
itemList[left].GetComponent<Button>().onClick.RemoveAllListeners();
|
|
itemList[left].GetComponent<Button>().onClick.AddListener(Left);
|
|
}
|
|
|
|
int GetCenterIndex()
|
|
{
|
|
return itemList.FindIndex(x => x.transform.localPosition.x == 0);
|
|
}
|
|
int GetSideIndex(bool left)
|
|
{
|
|
return itemList.FindIndex(x => left ? x.transform.localPosition.x < 0 : x.transform.localPosition.x > 0);
|
|
}
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
}
|