powerfun-unity/Assets/BannerController.cs
2022-01-19 13:42:12 +08:00

283 lines
10 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<Recommand> list;
private int currentIndex = 1;
private Dictionary<string, Texture> caches;
void Awake()
{
itemList = new List<CanvasGroup>
{
transform.Find("1").GetComponent<CanvasGroup>(),
transform.Find("2").GetComponent<CanvasGroup>(),
transform.Find("3").GetComponent<CanvasGroup>()
};
caches = new Dictionary<string, Texture>();
standardPositions = new List<Vector3>
{
transform.Find("1").localPosition,transform.Find("2").localPosition,transform.Find("3").localPosition
};
AddTouchEvent();
Debug.Log(standardPositions[0]);
Debug.Log(standardPositions[1]);
Debug.Log(standardPositions[2]);
}
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);
}
int sIndex = 0, eIndex = 2;
private void Start()
{
GetList();
}
async void GetList()
{
var res = await ConfigHelper.mapApi.GetRecommendList();
if (res.result)
{
if (res.data.Count >= 3)
{
sIndex = 0;eIndex = 2;
Initial(res.data);
gameObject.SetActive(true);
}
else
{
gameObject.SetActive(false);
}
}
}
public void Initial(List<Recommand> list)
{
int index = 0;
foreach (CanvasGroup c in itemList)
{
var area = list[index++];
c.GetComponent<RecommendController>().Initial(area, caches);
c.GetComponent<Button>().onClick.RemoveAllListeners();
if (c.alpha != 1)
{
if (c.transform.localPosition.x < 0)
{
c.GetComponent<Button>().onClick.AddListener(Right);
}
else
{
c.GetComponent<Button>().onClick.AddListener(Left);
}
}
else
{
c.GetComponent<Button>().onClick.AddListener(()=>GoDetail(c));
}
}
this.list = list;
}
bool animateLock = false;
private void Right()
{
int center = GetCenterIndex(),
left = GetSideIndex(true),
right = GetSideIndex(false);
if (center == -1 || left == -1 || right == -1 || animateLock) return;
animateLock = true;
if (leftse != null) leftse.Complete(true);
DORight(itemList[left], itemList[center], itemList[right]);
}
void DORight(CanvasGroup left, CanvasGroup center, CanvasGroup right)
{
Sequence se = DOTween.Sequence();
se.Join(center.DOFade(0.5f, 0.3f));
se.Join(center.GetComponent<RectTransform>().DOScale(Vector3.one * 0.8f, 0.3f));
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}");
right.GetComponent<RecommendController>().Initial(area, caches);
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));
se.Play().onComplete = ()=> { animateLock = false; };
rightse = se;
left.transform.SetAsLastSibling();
//事件改变
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);
}
#if UNITY_STANDALONE_WIN
float offset = 110;
#else
float offset = 43;
#endif
Sequence leftse, rightse;
private void Left()
{
int center = GetCenterIndex(),
left = GetSideIndex(true),
right = GetSideIndex(false);
if (center == -1 || left == -1 || right == -1|| animateLock) return;
animateLock = true;
if (rightse != null) rightse.Complete(true);
DOLeft(itemList[left], itemList[center], itemList[right]);
}
void DOLeft(CanvasGroup left, CanvasGroup center, CanvasGroup right)
{
Sequence se = DOTween.Sequence();
se.Join(center.DOFade(0.5f, 0.3f));
se.Join(center.GetComponent<RectTransform>().DOScale(Vector3.one * 0.8f, 0.3f));
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}");
var centerArea = list[currentIndex + 1];
left.GetComponent<RecommendController>().Initial(area, caches);
if (currentIndex < 0) currentIndex = list.Count - 1;
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));
se.Play().onComplete = () => { animateLock = false; };
leftse = se;
right.transform.SetAsLastSibling();
//事件改变
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);
}
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);
}
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 = ()=>
{
c.GetComponent<RecommendController>().DoWithType();
};
}
}
float autoTime = 5;
// Update is called once per frame
void Update()
{
autoTime -= Time.deltaTime;
if (autoTime < 0)
{
Left();
autoTime += 5;
}
}
private void OnDestroy()
{
Debug.Log(248+"毁灭");
TouchKit.removeGestureRecognizer(pan);
caches = null;
Resources.UnloadUnusedAssets();
GC.Collect();
}
}