2021-07-29 15:45:44 +08:00

297 lines
10 KiB
C#

using Assets.Scripts;
using Assets.Scripts.Apis.Models;
using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class RaceHomeScript : RaceScript
{
// Start is called before the first frame update
private ScrollRect scroll;
private Transform content,rightContainer,leftContainer,btnGoList;
private float contentSize;
private int count;
private Button L, R;
//当前鼠标悬浮的卡片
public override void Show()
{
base.Show();
handleAvatar();
GetData();
if (btnGoList != null)
{
btnGoList.Find("Panel").GetComponent<CanvasGroup>().alpha = 0;
}
}
void Start()
{
scroll = transform.Find("LeftContainer/Swiper").GetComponent<ScrollRect>();
content = scroll.content;
//Initial();
if (scroll != null)
{
L = scroll.transform.Find("Left").GetComponent<Button>();
R = scroll.transform.Find("Right").GetComponent<Button>();
}
if (L != null)
{
UIManager.AddEvent(L.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (b) =>
{
goLeft();
});
UIManager.AddEvent(L.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerEnter, (b) =>
{
L.transform.DOScale(1.1f, 0.2f);
});
UIManager.AddEvent(L.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerExit, (b) =>
{
L.transform.DOScale(1f, 0.2f);
});
}
if (R != null)
{
UIManager.AddEvent(R.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (b) =>
{
goRight();
});
UIManager.AddEvent(R.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerEnter, (b) =>
{
R.transform.DOScale(1.1f, 0.2f);
});
UIManager.AddEvent(R.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerExit, (b) =>
{
R.transform.DOScale(1f, 0.2f);
});
}
StartTime();
rightContainer = transform.Find("RightContainer");
leftContainer = transform.Find("LeftContainer");
btnGoList = rightContainer.Find("Container-2/BtnGoList");
if (btnGoList != null)
{
UIManager.AddEvent(btnGoList.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, b =>
{
UIManager.ShowRaceListPanel();
});
}
}
bool isEnter = false;
async void GetData()
{
var r = await ConfigHelper.mapCompetitionApi.GetCompetitionListV2("", 0, 7, order: 0);
var activityList = await ConfigHelper.mapCompetitionApi.GetCompetitionListV2("", 0, 3, activity: 1, order: 2);
DisplayMaps(r.data, activityList.data);
Initial();
if (!isEnter)
{
Utils.StartTimeUnLimit(10, () =>
{
goRight();
});
isEnter = true;
}
}
private void DisplayMaps(List<MapCompetition> list, List<MapCompetition> activityList)
{
int i = 0;
GameObject bigGame = Resources.Load<GameObject>("UI/Prefab/Race/ItemBig"),
smallGame = Resources.Load<GameObject>("UI/Prefab/Race/ItemSmall"),
bannerItem = Resources.Load<GameObject>("UI/Prefab/Race/Banner/BannerItem"),
dotItem = Resources.Load<GameObject>("UI/Prefab/Race/Banner/DotItem");
//for (i = 0; i < 2; i++)
//{
// var game = Instantiate<GameObject>(bigGame);
// game.GetComponent<RaceItemScript>().Initial(list[i], transform);
// game.transform.SetParent(rightContainer.Find("Container-1"));
// game.transform.localScale = Vector3.one;
//}
foreach (Transform t in rightContainer.Find("Container-2"))
{
if (t.name != "BtnGoList")
{
t.gameObject.Destroy();
}
}
scroll.transform.Find("DotList").DestroyChildren();
scroll.content.DestroyChildren();
for (; i < list.Count; i++)
{
var game = Instantiate<GameObject>(smallGame);
game.GetComponent<RaceItemScript>().Initial(list[i], transform);
game.transform.SetParent(rightContainer.Find("Container-2"));
game.transform.localScale = Vector3.one;
}
for (i = 0; i < activityList.Count; i++)
{
var activity = activityList[i];
var banner = Instantiate<GameObject>(bannerItem);
var dot = Instantiate<GameObject>(dotItem);
Utils.DisplayImage(banner.GetComponent<RawImage>(), activity.BannerPreview, true);
//if (i == 0)
//{
// banner.GetComponent<RawImage>().color = Utils.HexToColorHtml("#ff0000");
//}
//else if (i == 1)
//{
// banner.GetComponent<RawImage>().color = Utils.HexToColorHtml("#00ff00");
//}
//else
//{
// banner.GetComponent<RawImage>().color = Utils.HexToColorHtml("#0000ff");
//}
UIManager.AddEvent(banner.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, b =>
{
OnBannerClick(activity);
});
banner.transform.SetParent(scroll.content);
dot.GetComponent<Image>().color =
i == 0 ? Utils.HexToColorHtml("#f93086") : Utils.HexToColorHtml("#ffffff");
dot.transform.SetParent(scroll.transform.Find("DotList"));
banner.transform.localScale = Vector3.one;
dot.transform.localScale = Vector3.one;
//scroll.content
}
rightContainer.Find("Container-2/BtnGoList").SetAsLastSibling();
}
void OnBannerClick(MapCompetition map)
{
App.RouteIdParam = map.RouteId;
App.CompetionId = map.Id;
SceneManager.LoadScene("Ride");
}
private async Task Login()
{
var result = await ConfigHelper.userApi.Login("13115011550", "laozhong", "");
App.CurrentUser = result.data;
}
int scrollIndex = 0;
public void Initial()
{
//if (content.childCount - 2 > 0)
//{
// contentSize = 1f / (content.childCount - 2);
//}
//else
//{
// contentSize = 0;
//}
contentSize = 1f / (content.childCount - 1);
scroll.horizontalNormalizedPosition = 0;
}
void goLeft()
{
Debug.Log($"{scroll.horizontalNormalizedPosition}");
if (scroll.horizontalNormalizedPosition <= 0.0001) goMoveEnd(1- scroll.horizontalNormalizedPosition);
else goMove(-1);
}
void goRight()
{
if (scroll.horizontalNormalizedPosition >= 0.9999) goMoveEnd(0 - scroll.horizontalNormalizedPosition);
//if ((scroll.horizontalNormalizedPosition + contentSize) >= 1) return;
else goMove();
}
void goMove(int i = 1)
{
//doAni();
if (startMoveEnd) return;
if (!start) startPosition = scroll.horizontalNormalizedPosition;
start = true;
scrollValue = i * contentSize / 20;
}
void goMoveEnd(float pos)
{
//doAni();
if (start) return;
if (!startMoveEnd) startPosition = scroll.horizontalNormalizedPosition;
totalScrollValue = 0;
moveEndValue = pos;
scrollValue = pos / 20;
startMoveEnd = true;
}
private bool start = false;
private float scrollValue = 0, totalScrollValue = 0, startPosition = 0;
bool startMoveEnd = false;
float moveEndValue = 0;
// Update is called once per frame
private void FixedUpdate()
{
if (start)
{
scroll.horizontalNormalizedPosition += scrollValue;
totalScrollValue += scrollValue;
if (System.Math.Abs(totalScrollValue) >= contentSize)
{
scrollValue = 0;
start = false;
scroll.horizontalNormalizedPosition = startPosition + (totalScrollValue < 0 ? -1 : 1) * contentSize;
handleDotList(totalScrollValue);
Debug.Log(scrollIndex);
//Debug.Log();
totalScrollValue = 0;
}
}
if (startMoveEnd)
{
scroll.horizontalNormalizedPosition += scrollValue;
totalScrollValue += scrollValue;
if (System.Math.Abs(totalScrollValue) >= System.Math.Abs(moveEndValue))
{
scrollValue = 0;
startMoveEnd = false;
scroll.horizontalNormalizedPosition = startPosition + moveEndValue;
handleDotListMoveEnd(totalScrollValue);
Debug.Log(scrollIndex);
//Debug.Log();
totalScrollValue = 0;
}
}
}
private void handleDotList(float totalScrollValue)
{
scrollIndex = ((scrollIndex + content.childCount) + (totalScrollValue < 0 ? -1 : 1)) % content.childCount;
var dotList = scroll.transform.Find("DotList");
for (int i = 0; i < dotList.childCount; i++)
{
if (i == scrollIndex)
{
dotList.GetChild(i).GetComponent<Image>().color = Utils.HexToColorHtml("#f93086");
}
else
{
dotList.GetChild(i).GetComponent<Image>().color = Utils.HexToColorHtml("#ffffff");
}
}
}
void handleDotListMoveEnd(float totalScrollValue)
{
var dotList = scroll.transform.Find("DotList");
scrollIndex = totalScrollValue < 0 ? 0 : dotList.childCount - 1;
for (int i = 0; i < dotList.childCount; i++)
{
if (i == scrollIndex)
{
dotList.GetChild(i).GetComponent<Image>().color = Utils.HexToColorHtml("#f93086");
}
else
{
dotList.GetChild(i).GetComponent<Image>().color = Utils.HexToColorHtml("#ffffff");
}
}
}
void Update()
{
}
}