powerfun-unity/Assets/Scripts/UI/Prefab/Login/QuickLoginScroll.cs
2021-09-06 17:50:01 +08:00

241 lines
7.2 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;
float startPos, endPos;
void Start()
{
scroll = gameObject.GetComponent<ScrollRect>();
#if UNITY_IOS || UNITY_ANDROID
UIManager.AddEvent(scroll.gameObject, UnityEngine.EventSystems.EventTriggerType.BeginDrag, b =>
{
startPos = scroll.horizontalNormalizedPosition;
doAni();
Debug.Log("开始滑动" + startPos);
});
UIManager.AddEvent(scroll.gameObject, UnityEngine.EventSystems.EventTriggerType.EndDrag, b =>
{
Debug.Log($"拽动结束{startPos},{endPos}");
endPos = scroll.horizontalNormalizedPosition;
goMovess();
});
#endif
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(bool flag = false)
{
int index = GetIndex();
if (flag)
{
lightbg.GetComponent<Image>().gameObject.SetActive(true);
lightbg.GetComponent<Image>().DOFade(1, 0.2f);
content.GetChild(index).GetComponent<QuickLoginUser>().setActive();
}
else
{
lightbg.GetComponent<Image>().color = Utils.HexToColorHtml("#ffffff00");
lightbg.GetComponent<Image>().gameObject.SetActive(false);
content.GetChild(index).GetComponent<QuickLoginUser>().setNoActive();
//lightbg.GetComponent<Image>().DOFade(0, 0.15f);
}
}
public void Initial()
{
count = content.childCount;
#if !(UNITY_ANDROID || UNITY_IOS)
if (content.childCount - 2 > 0)
{
contentSize = 1f / (content.childCount - 2);
}
else
{
contentSize = 0;
}
#else
if (content.childCount - 3 > 0)
{
contentSize = 1f / (content.childCount - 3);
//Debug.Log(contentSize);
}
else
{
contentSize = 0;
}
#endif
//scroll.horizontalNormalizedPosition = 0;
Canvas.ForceUpdateCanvases();
var i = PlayerPrefs.GetInt("UserInfoIndex");
Debug.Log($"51 {i}");
#if !(UNITY_ANDROID || UNITY_IOS)
scroll.horizontalNormalizedPosition = contentSize/2+i*contentSize;
#else
scroll.horizontalNormalizedPosition = i * contentSize;
#endif
SetColor();
}
void goMovess()
{
int i = (int)System.Math.Round((scroll.horizontalNormalizedPosition - startPos) / contentSize);
//Debug.Log("左滑开始" + );
if (i == 0)
{
scroll.horizontalNormalizedPosition = startPos;
doAni(true);
return;
}
goMove(i);
}
#if UNITY_ANDROID || UNITY_IOS
void goLeft()
{
startPos = scroll.horizontalNormalizedPosition;
if (scroll.horizontalNormalizedPosition <= 0 + 0.0001)
{
return;
}
goMove(-1);
}
void goRight()
{
startPos = scroll.horizontalNormalizedPosition;
if (scroll.horizontalNormalizedPosition + 0.0001 >= 1)
{
return;
}
goMove();
}
#else
void goLeft()
{
if (scroll.horizontalNormalizedPosition <= contentSize) return;
goMove(-1);
}
void goRight()
{
if ((scroll.horizontalNormalizedPosition + contentSize) >= 1) return;
goMove();
}
#endif
void goMove(int i = 1)
{
Debug.Log("移动系数" + i);
doAni();
//#if !(UNITY_ANDROID || UNITY_IOS)
// if (!start) startPosition = scroll.horizontalNormalizedPosition;
//#else
// int ind = GetIndex(startPos);
// if (!start) startPosition = ind*contentSize;
//#endif
if (!start) startPosition = scroll.horizontalNormalizedPosition;
scrollIndex = i;
start = true;
#if !(UNITY_ANDROID || UNITY_IOS)
scrollValue = i * contentSize / 10;
#else
scrollValue = ((GetIndex(startPos) + i) * contentSize - startPos) / 10;
#endif
}
private bool start = false;
private float scrollValue = 0, totalScrollValue = 0, startPosition = 0;
private int scrollIndex = 0;
int GetIndex(float? startPos = null)
{
#if !(UNITY_ANDROID || UNITY_IOS)
return (int)System.Math.Round(
(scroll.horizontalNormalizedPosition + (contentSize / 2)) / contentSize,
0);
#else
if (contentSize == 0)
{
return 1;
}
return (int)System.Math.Round
((startPos ?? scroll.horizontalNormalizedPosition + contentSize) / contentSize, 0);
#endif
}
void SetColor(int? rIndex = null)
{
int index = rIndex ?? 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++)
{
string c;
if (i == index)
{
doAni(true);
//
}
else
{
content.GetChild(i).GetComponent<QuickLoginUser>().setNoActive();
if (i < index)
{
content.GetChild(i).GetComponent<QuickLoginUser>().setNoActiveFunc(goLeft);
}
else
{
content.GetChild(i).GetComponent<QuickLoginUser>().setNoActiveFunc(goRight);
}
}
}
}
// Update is called once per frame
void FixedUpdate()
{
if (start)
{
scroll.horizontalNormalizedPosition += scrollValue;
totalScrollValue += scrollValue;
if (System.Math.Abs(totalScrollValue) >= contentSize)
{
scrollValue = 0;
start = false;
#if !(UNITY_ANDROID || UNITY_IOS)
scroll.horizontalNormalizedPosition = startPosition + (totalScrollValue<0?-1:1)*contentSize;
#else
scroll.horizontalNormalizedPosition = (GetIndex(startPos) + scrollIndex) * contentSize;
#endif
//Debug.Log();
SetColor();
totalScrollValue = 0;
}
}
}
}