204 lines
7.0 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.UI;
public class RaceHomeScript : RaceScript
{
// Start is called before the first frame update
private ScrollRect scroll;
private Transform content,rightContainer,leftContainer,btnGoList,avatar;
private float contentSize;
private int count;
private Button L, R;
//当前鼠标悬浮的卡片
async 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();
});
}
if (R != null)
{
UIManager.AddEvent(R.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (b) =>
{
goRight();
});
}
StartTime();
#if UNITY_EDITOR
if (App.CurrentUser == null) //App.CurrentUser == null
{
await Login();
}
#endif
rightContainer = transform.Find("RightContainer");
leftContainer = transform.Find("LeftContainer");
btnGoList = rightContainer.Find("Container-2/BtnGoList");
UIManager.AddEvent(btnGoList.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, b =>
{
UIManager.ShowRaceListPanel();
});
avatar = transform.Find("Avatar");
if (avatar != null)
{
Utils.DisplayImage(avatar.GetComponent<RawImage>(), App.CurrentUser.WxHeadImg, true);
UIManager.AddEvent(avatar.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, b =>
{
UIManager.ShowUserInfoPanel();
});
}
var r = await ConfigHelper.mapCompetitionApi.GetCompetitionListV2("", 0, 5);
var activityList = await ConfigHelper.mapCompetitionApi.GetCompetitionListV2("", 0, 3, activity: 1);
DisplayMaps(r.data,activityList.data);
Initial();
}
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;
}
for (; i < 5; 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 < 3; i++)
{
var activity = activityList[i];
var banner = Instantiate<GameObject>(bannerItem);
var dot = Instantiate<GameObject>(dotItem);
Utils.DisplayImage(banner.GetComponent<RawImage>(), activity.BannerPreview, true);
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)
{
Debug.Log("banner點擊");
}
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) return;
goMove(-1);
}
void goRight()
{
if (scroll.horizontalNormalizedPosition >= 0.9999) return;
//if ((scroll.horizontalNormalizedPosition + contentSize) >= 1) return;
goMove();
}
void goMove(int i = 1)
{
//doAni();
if (!start) startPosition = scroll.horizontalNormalizedPosition;
start = true;
scrollValue = i * contentSize / 20;
}
private bool start = false;
private float scrollValue = 0, totalScrollValue = 0, startPosition = 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;
}
}
}
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 Update()
{
}
}