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

162 lines
4.8 KiB
C#
Raw Normal View History

2021-04-21 16:16:14 +08:00
using Assets.Scripts;
using DG.Tweening;
using System.Collections;
2021-03-26 16:36:31 +08:00
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
2021-04-06 15:30:36 +08:00
public class QuickLoginScroll : MonoBehaviour
2021-03-26 16:36:31 +08:00
{
// Start is called before the first frame update
private ScrollRect scroll;
2021-04-21 16:16:14 +08:00
private Transform content,lightbg;
2021-03-26 16:36:31 +08:00
private float contentSize;
private int count;
2021-03-26 16:36:31 +08:00
[SerializeField] Button L, R;
void Start()
{
scroll = gameObject.GetComponent<ScrollRect>();
2021-08-24 17:50:14 +08:00
//scroll.onValueChanged.AddListener((a) =>
//{
// Debug.Log(a);
//});
2021-03-26 16:36:31 +08:00
content = gameObject.transform.Find("Viewport").Find("Content");
2021-04-21 16:16:14 +08:00
lightbg = transform.parent.Find("Light");
2021-04-06 15:30:36 +08:00
//Initial();
if (L != null)
2021-03-26 16:36:31 +08:00
{
2021-04-19 15:44:11 +08:00
UIManager.AddEvent(L.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick,(b)=>
{
goLeft();
});
//L.onClick.AddListener(goLeft);
2021-03-26 16:36:31 +08:00
}
if (R != null)
{
2021-04-19 15:44:11 +08:00
UIManager.AddEvent(R.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (b) =>
{
goRight();
});
//R.onClick.AddListener(goRight);
2021-03-26 16:36:31 +08:00
}
}
2021-04-21 16:16:14 +08:00
private void doAni()
{
lightbg.GetComponent<Image>().color = Utils.HexToColorHtml("#ffffff00");
lightbg.GetComponent<Image>().gameObject.SetActive(false);
2021-08-24 17:50:14 +08:00
int index = GetIndex();
content.GetChild(index).GetComponent<QuickLoginUser>().setNoActive();
2021-04-21 16:16:14 +08:00
//lightbg.GetComponent<Image>().DOFade(0, 0.15f);
}
2021-04-06 15:30:36 +08:00
public void Initial()
{
count = content.childCount;
2021-08-24 17:50:14 +08:00
#if !(UNITY_ANDROID || UNITY_IOS)
2021-04-06 15:30:36 +08:00
if (content.childCount - 2 > 0)
{
contentSize = 1f / (content.childCount - 2);
}
else
{
contentSize = 0;
}
2021-08-24 17:50:14 +08:00
#else
if (content.childCount - 3 > 0)
{
contentSize = 1f / (content.childCount - 3);
}
else
{
contentSize = 0;
}
#endif
2021-04-06 15:30:36 +08:00
//scroll.horizontalNormalizedPosition = 0;
Canvas.ForceUpdateCanvases();
2021-04-21 11:19:31 +08:00
var i = PlayerPrefs.GetInt("UserInfoIndex");
Debug.Log($"51 {i}");
2021-08-24 17:50:14 +08:00
#if !(UNITY_ANDROID || UNITY_IOS)
2021-04-21 11:19:31 +08:00
scroll.horizontalNormalizedPosition = contentSize/2+i*contentSize;
2021-08-24 17:50:14 +08:00
#else
scroll.horizontalNormalizedPosition = i * contentSize;
Debug.Log(scroll.horizontalNormalizedPosition);
#endif
2021-04-06 15:30:36 +08:00
SetColor();
}
2021-03-26 16:36:31 +08:00
void goLeft()
{
if (scroll.horizontalNormalizedPosition <= contentSize) return;
goMove(-1);
2021-03-26 16:36:31 +08:00
}
void goRight()
{
2021-08-24 17:50:14 +08:00
Debug.Log(scroll.horizontalNormalizedPosition);
if (scroll.horizontalNormalizedPosition+0.0001 >= 1) return;
goMove();
}
void goMove(int i = 1)
{
2021-04-21 16:16:14 +08:00
doAni();
if (!start) startPosition = scroll.horizontalNormalizedPosition;
2021-03-26 16:36:31 +08:00
start = true;
2021-08-24 17:50:14 +08:00
scrollValue = i*contentSize / 10;
2021-03-26 16:36:31 +08:00
}
private bool start = false;
private float scrollValue = 0, totalScrollValue = 0, startPosition = 0;
2021-08-24 17:50:14 +08:00
int GetIndex()
{
2021-08-24 17:50:14 +08:00
#if !(UNITY_ANDROID || UNITY_IOS)
return (int)System.Math.Round(
(scroll.horizontalNormalizedPosition + (contentSize / 2)) / contentSize,
0);
2021-08-24 17:50:14 +08:00
#else
return (int)System.Math.Round(
(scroll.horizontalNormalizedPosition+ contentSize) / contentSize,
0);
#endif
}
void SetColor()
{
int index = GetIndex();
//lightbg.GetComponent<Image>().gameObject.SetActive(true);
//lightbg.GetComponent<Image>().DOFade(1, 0.2f);
//content.GetChild(index).GetComponent<QuickLoginUser>().setActive();
for (int i = 1; i < content.childCount - 1; i++)
{
2021-04-06 15:30:36 +08:00
string c;
if (i == index)
{
2021-04-21 16:16:14 +08:00
lightbg.GetComponent<Image>().gameObject.SetActive(true);
lightbg.GetComponent<Image>().DOFade(1, 0.2f);
2021-04-06 15:30:36 +08:00
content.GetChild(i).GetComponent<QuickLoginUser>().setActive();
2021-04-21 16:16:14 +08:00
//
2021-04-06 15:30:36 +08:00
}
else
{
2021-04-06 15:30:36 +08:00
content.GetChild(i).GetComponent<QuickLoginUser>().setNoActive();
}
}
}
2021-03-26 16:36:31 +08:00
// Update is called once per frame
2021-08-24 17:50:14 +08:00
void FixedUpdate()
2021-03-26 16:36:31 +08:00
{
if (start)
{
2021-04-23 15:36:18 +08:00
scroll.horizontalNormalizedPosition += scrollValue;
2021-03-26 16:36:31 +08:00
totalScrollValue += scrollValue;
if (System.Math.Abs(totalScrollValue) >= contentSize)
{
scrollValue = 0;
start = false;
2021-04-23 15:36:18 +08:00
scroll.horizontalNormalizedPosition = startPosition + (totalScrollValue<0?-1:1)*contentSize;
//Debug.Log();
SetColor();
2021-03-26 16:36:31 +08:00
totalScrollValue = 0;
}
}
}
}