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;
|
2021-12-28 11:25:09 +08:00
|
|
|
|
using UnityEngine.UI;
|
2021-12-27 09:39:07 +08:00
|
|
|
|
|
|
|
|
|
|
public class BannerController : MonoBehaviour
|
|
|
|
|
|
{
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
|
private List<CanvasGroup> itemList;
|
|
|
|
|
|
private List<Vector3> standardPositions;
|
2022-01-07 15:11:40 +08:00
|
|
|
|
private List<Recommand> list;
|
2021-12-27 09:39:07 +08:00
|
|
|
|
private int currentIndex = 1;
|
2022-01-12 10:29:10 +08:00
|
|
|
|
private Dictionary<string, Texture> caches;
|
2021-12-27 09:39:07 +08:00
|
|
|
|
void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
itemList = new List<CanvasGroup>
|
|
|
|
|
|
{
|
|
|
|
|
|
transform.Find("1").GetComponent<CanvasGroup>(),
|
|
|
|
|
|
transform.Find("2").GetComponent<CanvasGroup>(),
|
|
|
|
|
|
transform.Find("3").GetComponent<CanvasGroup>()
|
|
|
|
|
|
};
|
2022-01-12 10:29:10 +08:00
|
|
|
|
caches = new Dictionary<string, Texture>();
|
2021-12-27 09:39:07 +08:00
|
|
|
|
standardPositions = new List<Vector3>
|
|
|
|
|
|
{
|
|
|
|
|
|
transform.Find("1").localPosition,transform.Find("2").localPosition,transform.Find("3").localPosition
|
|
|
|
|
|
};
|
2021-12-31 14:39:04 +08:00
|
|
|
|
AddTouchEvent();
|
|
|
|
|
|
|
2021-12-28 11:25:09 +08:00
|
|
|
|
|
2021-12-27 09:39:07 +08:00
|
|
|
|
Debug.Log(standardPositions[0]);
|
|
|
|
|
|
Debug.Log(standardPositions[1]);
|
|
|
|
|
|
Debug.Log(standardPositions[2]);
|
|
|
|
|
|
}
|
2021-12-31 14:39:04 +08:00
|
|
|
|
TKPanRecognizer pan;
|
|
|
|
|
|
private void AddTouchEvent()
|
|
|
|
|
|
{
|
|
|
|
|
|
pan = new TKPanRecognizer();
|
|
|
|
|
|
var panList = new List<CanvasGroup>();
|
|
|
|
|
|
pan.gestureRecognizedEvent += (r) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!App.currentPageIsHome) return;
|
|
|
|
|
|
int center = GetCenterIndex(),
|
|
|
|
|
|
left = GetSideIndex(true),
|
|
|
|
|
|
right = GetSideIndex(false);
|
|
|
|
|
|
if (!(center == -1 || left == -1 || right == -1) && panList.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
panList.Add(itemList[left]);
|
|
|
|
|
|
panList.Add(itemList[center]);
|
|
|
|
|
|
panList.Add(itemList[right]);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (panList.Count == 0) return;
|
|
|
|
|
|
var startPoint = r.startTouchLocation();
|
|
|
|
|
|
if (((RectTransform)transform).isPointInTransfrom(startPoint))
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var item in panList)
|
|
|
|
|
|
{
|
|
|
|
|
|
item.GetComponent<Button>().onClick.RemoveAllListeners();
|
|
|
|
|
|
}
|
|
|
|
|
|
var offset = pan.deltaTranslation;
|
|
|
|
|
|
if (panList[1].transform.localPosition.x == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
panList[1].transform.DOScale(0.8f, 0.5f);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (offset.x > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
//-43 0 43
|
|
|
|
|
|
//后面的 如果往左,需要回到-43再往右
|
|
|
|
|
|
panList[0].transform.localPosition += new Vector3(1, 0, 0);
|
|
|
|
|
|
panList[1].transform.localPosition += new Vector3(1, 0, 0);
|
|
|
|
|
|
panList[2].transform.localPosition += new Vector3(-1, 0, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (offset.x < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
panList[0].transform.localPosition += new Vector3(1, 0, 0);
|
|
|
|
|
|
panList[1].transform.localPosition += new Vector3(-1, 0, 0);
|
|
|
|
|
|
//后面的 如果往右,需要回到43再往左
|
|
|
|
|
|
panList[2].transform.localPosition += new Vector3(-1, 0, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
panList.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
pan.gestureCompleteEvent += (r) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!App.currentPageIsHome) return;
|
|
|
|
|
|
if (panList.Count == 0) return;
|
|
|
|
|
|
if (panList[1].transform.localPosition.x < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
DOLeft(panList[0], panList[1], panList[2]);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
DORight(panList[0], panList[1], panList[2]);
|
|
|
|
|
|
}
|
|
|
|
|
|
panList.Clear();
|
|
|
|
|
|
};
|
|
|
|
|
|
TouchKit.addGestureRecognizer(pan);
|
|
|
|
|
|
}
|
2022-01-12 15:31:57 +08:00
|
|
|
|
int sIndex = 0, eIndex = 2;
|
2021-12-28 11:25:09 +08:00
|
|
|
|
private void Start()
|
|
|
|
|
|
{
|
|
|
|
|
|
GetList();
|
|
|
|
|
|
}
|
2021-12-27 09:39:07 +08:00
|
|
|
|
async void GetList()
|
|
|
|
|
|
{
|
2022-01-07 15:11:40 +08:00
|
|
|
|
var res = await ConfigHelper.mapApi.GetRecommendList();
|
2021-12-27 09:39:07 +08:00
|
|
|
|
if (res.result)
|
|
|
|
|
|
{
|
2021-12-29 14:23:37 +08:00
|
|
|
|
if (res.data.Count >= 3)
|
|
|
|
|
|
{
|
2022-01-12 15:31:57 +08:00
|
|
|
|
sIndex = 0;eIndex = 2;
|
2021-12-29 14:23:37 +08:00
|
|
|
|
Initial(res.data);
|
|
|
|
|
|
gameObject.SetActive(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
gameObject.SetActive(false);
|
|
|
|
|
|
}
|
2021-12-27 09:39:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-07 15:11:40 +08:00
|
|
|
|
public void Initial(List<Recommand> list)
|
2021-12-27 09:39:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
foreach (CanvasGroup c in itemList)
|
|
|
|
|
|
{
|
2021-12-28 11:25:09 +08:00
|
|
|
|
var area = list[index++];
|
2022-01-07 15:11:40 +08:00
|
|
|
|
|
2022-01-12 10:29:10 +08:00
|
|
|
|
c.GetComponent<RecommendController>().Initial(area, caches);
|
2021-12-31 14:39:04 +08:00
|
|
|
|
c.GetComponent<Button>().onClick.RemoveAllListeners();
|
2021-12-28 11:25:09 +08:00
|
|
|
|
if (c.alpha != 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (c.transform.localPosition.x < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
c.GetComponent<Button>().onClick.AddListener(Right);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
c.GetComponent<Button>().onClick.AddListener(Left);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-12-31 14:39:04 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
c.GetComponent<Button>().onClick.AddListener(()=>GoDetail(c));
|
|
|
|
|
|
}
|
2021-12-27 09:39:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
this.list = list;
|
|
|
|
|
|
}
|
2022-01-12 10:29:10 +08:00
|
|
|
|
bool animateLock = false;
|
2021-12-27 09:39:07 +08:00
|
|
|
|
private void Right()
|
|
|
|
|
|
{
|
|
|
|
|
|
int center = GetCenterIndex(),
|
2021-12-31 14:39:04 +08:00
|
|
|
|
left = GetSideIndex(true),
|
|
|
|
|
|
right = GetSideIndex(false);
|
2022-01-12 10:29:10 +08:00
|
|
|
|
if (center == -1 || left == -1 || right == -1 || animateLock) return;
|
|
|
|
|
|
animateLock = true;
|
2022-01-18 13:24:19 +08:00
|
|
|
|
if (leftse != null) leftse.Complete(true);
|
2021-12-31 14:39:04 +08:00
|
|
|
|
DORight(itemList[left], itemList[center], itemList[right]);
|
|
|
|
|
|
}
|
|
|
|
|
|
void DORight(CanvasGroup left, CanvasGroup center, CanvasGroup right)
|
|
|
|
|
|
{
|
|
|
|
|
|
Sequence se = DOTween.Sequence();
|
2022-01-05 17:40:12 +08:00
|
|
|
|
se.Join(center.DOFade(0.5f, 0.3f));
|
|
|
|
|
|
se.Join(center.GetComponent<RectTransform>().DOScale(Vector3.one * 0.8f, 0.3f));
|
2022-01-12 15:31:57 +08:00
|
|
|
|
se.Join(center.GetComponent<RectTransform>().DOLocalMoveX(offset, 0.3f));
|
|
|
|
|
|
se.Join(right.GetComponent<RectTransform>().DOLocalMoveX(-1* offset, 0.3f));
|
|
|
|
|
|
var area = list[((sIndex - 1) + list.Count) % list.Count];
|
|
|
|
|
|
sIndex--;
|
|
|
|
|
|
eIndex--;
|
|
|
|
|
|
if (sIndex < 0) sIndex = (sIndex + list.Count) % list.Count;
|
|
|
|
|
|
if (eIndex < 0) eIndex = (eIndex + list.Count) % list.Count;
|
|
|
|
|
|
Debug.Log($"{sIndex},{eIndex}");
|
2022-01-12 10:29:10 +08:00
|
|
|
|
right.GetComponent<RecommendController>().Initial(area, caches);
|
2022-01-05 17:40:12 +08:00
|
|
|
|
se.Join(left.DOFade(1, 0.3f));
|
|
|
|
|
|
se.Join(left.GetComponent<RectTransform>().DOScale(Vector3.one, 0.3f));
|
|
|
|
|
|
se.Join(left.GetComponent<RectTransform>().DOLocalMoveX(0, 0.3f));
|
2022-01-12 10:29:10 +08:00
|
|
|
|
se.Play().onComplete = ()=> { animateLock = false; };
|
2022-01-18 13:24:19 +08:00
|
|
|
|
rightse = se;
|
2021-12-31 14:39:04 +08:00
|
|
|
|
left.transform.SetAsLastSibling();
|
2021-12-28 11:25:09 +08:00
|
|
|
|
//事件改变
|
2021-12-31 14:39:04 +08:00
|
|
|
|
left.GetComponent<Button>().onClick.RemoveAllListeners();
|
|
|
|
|
|
left.GetComponent<Button>().onClick.AddListener(() => GoDetail(left));
|
|
|
|
|
|
center.GetComponent<Button>().onClick.RemoveAllListeners();
|
|
|
|
|
|
center.GetComponent<Button>().onClick.AddListener(Left);
|
|
|
|
|
|
right.GetComponent<Button>().onClick.RemoveAllListeners();
|
|
|
|
|
|
right.GetComponent<Button>().onClick.AddListener(Right);
|
2021-12-27 09:39:07 +08:00
|
|
|
|
}
|
2022-01-12 15:31:57 +08:00
|
|
|
|
#if UNITY_STANDALONE_WIN
|
|
|
|
|
|
float offset = 110;
|
|
|
|
|
|
#else
|
|
|
|
|
|
float offset = 43;
|
|
|
|
|
|
#endif
|
2022-01-18 13:24:19 +08:00
|
|
|
|
Sequence leftse, rightse;
|
2021-12-27 09:39:07 +08:00
|
|
|
|
private void Left()
|
|
|
|
|
|
{
|
|
|
|
|
|
int center = GetCenterIndex(),
|
2021-12-31 14:39:04 +08:00
|
|
|
|
left = GetSideIndex(true),
|
|
|
|
|
|
right = GetSideIndex(false);
|
2022-01-12 10:29:10 +08:00
|
|
|
|
if (center == -1 || left == -1 || right == -1|| animateLock) return;
|
|
|
|
|
|
animateLock = true;
|
2022-01-18 13:24:19 +08:00
|
|
|
|
if (rightse != null) rightse.Complete(true);
|
2021-12-31 14:39:04 +08:00
|
|
|
|
DOLeft(itemList[left], itemList[center], itemList[right]);
|
|
|
|
|
|
}
|
|
|
|
|
|
void DOLeft(CanvasGroup left, CanvasGroup center, CanvasGroup right)
|
|
|
|
|
|
{
|
|
|
|
|
|
Sequence se = DOTween.Sequence();
|
2022-01-05 17:40:12 +08:00
|
|
|
|
se.Join(center.DOFade(0.5f, 0.3f));
|
|
|
|
|
|
se.Join(center.GetComponent<RectTransform>().DOScale(Vector3.one * 0.8f, 0.3f));
|
2022-01-12 15:31:57 +08:00
|
|
|
|
se.Join(center.GetComponent<RectTransform>().DOLocalMoveX(-1* offset, 0.3f));
|
|
|
|
|
|
se.Join(left.GetComponent<RectTransform>().DOLocalMoveX(offset, 0.3f));
|
|
|
|
|
|
var area = list[((eIndex + 1) + list.Count) % list.Count];
|
|
|
|
|
|
sIndex++;
|
|
|
|
|
|
eIndex++;
|
|
|
|
|
|
if (sIndex >= list.Count - 1) sIndex = sIndex % list.Count;
|
|
|
|
|
|
if (eIndex >= list.Count - 1) eIndex = eIndex % list.Count;
|
|
|
|
|
|
Debug.Log($"{sIndex},{eIndex}");
|
2021-12-28 11:25:09 +08:00
|
|
|
|
var centerArea = list[currentIndex + 1];
|
2022-01-12 10:29:10 +08:00
|
|
|
|
left.GetComponent<RecommendController>().Initial(area, caches);
|
2021-12-27 09:39:07 +08:00
|
|
|
|
if (currentIndex < 0) currentIndex = list.Count - 1;
|
2022-01-05 17:40:12 +08:00
|
|
|
|
se.Join(right.DOFade(1, 0.3f));
|
|
|
|
|
|
se.Join(right.GetComponent<RectTransform>().DOScale(Vector3.one, 0.3f));
|
|
|
|
|
|
se.Join(right.GetComponent<RectTransform>().DOLocalMoveX(0, 0.3f));
|
2022-01-12 10:29:10 +08:00
|
|
|
|
se.Play().onComplete = () => { animateLock = false; };
|
2022-01-18 13:24:19 +08:00
|
|
|
|
leftse = se;
|
2021-12-31 14:39:04 +08:00
|
|
|
|
right.transform.SetAsLastSibling();
|
2021-12-28 11:25:09 +08:00
|
|
|
|
//事件改变
|
2021-12-31 14:39:04 +08:00
|
|
|
|
right.GetComponent<Button>().onClick.RemoveAllListeners();
|
|
|
|
|
|
right.GetComponent<Button>().onClick.AddListener(() => GoDetail(right));
|
|
|
|
|
|
center.GetComponent<Button>().onClick.RemoveAllListeners();
|
|
|
|
|
|
center.GetComponent<Button>().onClick.AddListener(Right);
|
|
|
|
|
|
left.GetComponent<Button>().onClick.RemoveAllListeners();
|
|
|
|
|
|
left.GetComponent<Button>().onClick.AddListener(Left);
|
2021-12-27 09:39:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
2021-12-31 14:39:04 +08:00
|
|
|
|
void GoDetail(CanvasGroup c)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (c.transform.localPosition.x == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var se = DOTween.Sequence();
|
|
|
|
|
|
se.Append(c.transform.DOScale(1.05f * Vector3.one, 0.15f));
|
|
|
|
|
|
se.Append(c.transform.DOScale(1f * Vector3.one, 0.15f));
|
|
|
|
|
|
se.Play().onComplete = ()=>
|
|
|
|
|
|
{
|
2022-01-07 15:11:40 +08:00
|
|
|
|
c.GetComponent<RecommendController>().DoWithType();
|
2021-12-31 14:39:04 +08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float autoTime = 5;
|
2021-12-27 09:39:07 +08:00
|
|
|
|
// Update is called once per frame
|
|
|
|
|
|
void Update()
|
|
|
|
|
|
{
|
2021-12-31 14:39:04 +08:00
|
|
|
|
autoTime -= Time.deltaTime;
|
|
|
|
|
|
if (autoTime < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Left();
|
|
|
|
|
|
autoTime += 5;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log(248+"毁灭");
|
|
|
|
|
|
TouchKit.removeGestureRecognizer(pan);
|
2022-01-12 10:29:10 +08:00
|
|
|
|
caches = null;
|
|
|
|
|
|
Resources.UnloadUnusedAssets();
|
|
|
|
|
|
GC.Collect();
|
2021-12-27 09:39:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|