powerfun-unity/Assets/Scripts/UI/Prefab/Login/QuickLoginScroll.cs

127 lines
3.8 KiB
C#

using Assets.Scripts;
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
public class QuickLoginScroll : MonoBehaviour
{
// Start is called before the first frame update
private ScrollRect scroll;
private Transform content,lightbg;
private float contentSize;
private int count;
[SerializeField] Button L, R;
void Start()
{
scroll = gameObject.GetComponent<ScrollRect>();
content = gameObject.transform.Find("Viewport").Find("Content");
lightbg = transform.parent.Find("Light");
//Initial();
if (L != null)
{
UIManager.AddEvent(L.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick,(b)=>
{
goLeft();
});
//L.onClick.AddListener(goLeft);
}
if (R != null)
{
UIManager.AddEvent(R.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (b) =>
{
goRight();
});
//R.onClick.AddListener(goRight);
}
}
private void doAni()
{
lightbg.GetComponent<Image>().color = Utils.HexToColorHtml("#ffffff00");
lightbg.GetComponent<Image>().gameObject.SetActive(false);
//lightbg.GetComponent<Image>().DOFade(0, 0.15f);
}
public void Initial()
{
count = content.childCount;
if (content.childCount - 2 > 0)
{
contentSize = 1f / (content.childCount - 2);
}
else
{
contentSize = 0;
}
//scroll.horizontalNormalizedPosition = 0;
Canvas.ForceUpdateCanvases();
var i = PlayerPrefs.GetInt("UserInfoIndex");
Debug.Log($"51 {i}");
scroll.horizontalNormalizedPosition = contentSize/2+i*contentSize;
SetColor();
}
void goLeft()
{
if (scroll.horizontalNormalizedPosition <= contentSize) return;
doAni();
if (!start) startPosition = scroll.horizontalNormalizedPosition;
start = true;
scrollValue = -1 * contentSize / 30;
}
void goRight()
{
if ((scroll.horizontalNormalizedPosition + contentSize) >= 1) return;
doAni();
if (!start) startPosition = scroll.horizontalNormalizedPosition;
start = true;
scrollValue = contentSize / 30;
}
private bool start = false, _lock = false;
private float scrollValue = 0, totalScrollValue = 0, startPosition = 0;
void SetColor()
{
int index = (int)System.Math.Round(
(scroll.horizontalNormalizedPosition + (contentSize / 2)) / contentSize,
0);
for (int i = 1; i < content.childCount-1; i++)
{
string c;
if (i == index)
{
lightbg.GetComponent<Image>().gameObject.SetActive(true);
lightbg.GetComponent<Image>().DOFade(1, 0.2f);
content.GetChild(i).GetComponent<QuickLoginUser>().setActive();
//
}
else
{
content.GetChild(i).GetComponent<QuickLoginUser>().setNoActive();
}
}
}
// Update is called once per frame
void Update()
{
if (start)
{
totalScrollValue += scrollValue;
if (System.Math.Abs(totalScrollValue) >= contentSize)
{
scrollValue = 0;
start = false;
scroll.horizontalNormalizedPosition = startPosition + totalScrollValue;
//Debug.Log();
SetColor();
totalScrollValue = 0;
}
else
{
scroll.horizontalNormalizedPosition += scrollValue;
}
}
}
}