powerfun-unity/Assets/BannerController.cs

111 lines
4.0 KiB
C#
Raw Normal View History

2021-12-27 09:39:07 +08:00
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;
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()
{
UIManager.AddEvent(transform.Find("L").gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, b =>
{
Left();
});
UIManager.AddEvent(transform.Find("R").gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, b =>
{
Right();
});
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
};
GetList();
Debug.Log(standardPositions[0]);
Debug.Log(standardPositions[1]);
Debug.Log(standardPositions[2]);
}
async void GetList()
{
var res = await ConfigHelper.mapApi.GetRecommendAreaList();
if (res.result)
{
Initial(res.data);
}
}
public void Initial(List<MapRouteAreaItem> list)
{
int index = 0;
foreach (CanvasGroup c in itemList)
{
c.GetComponent<NewRouteItemController>().Initial(list[index++]);
}
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);
itemList[right].GetComponent<NewRouteItemController>().Initial(list[((currentIndex++) + list.Count)% list.Count]);
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();
}
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);
itemList[left].GetComponent<NewRouteItemController>().Initial(list[((currentIndex--) + list.Count) % list.Count]);
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();
}
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()
{
}
}