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

223 lines
6.7 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;
2021-08-27 10:12:50 +08:00
float startPos, endPos;
2021-03-26 16:36:31 +08:00
void Start()
{
scroll = gameObject.GetComponent<ScrollRect>();
2021-09-02 14:18:26 +08:00
#if UNITY_IOS || UNITY_ANDROID
2021-08-27 10:12:50 +08:00
UIManager.AddEvent(scroll.gameObject, UnityEngine.EventSystems.EventTriggerType.BeginDrag, b =>
{
startPos = scroll.horizontalNormalizedPosition;
2021-09-02 14:18:26 +08:00
Debug.Log("开始滑动"+startPos);
2021-08-27 10:12:50 +08:00
});
UIManager.AddEvent(scroll.gameObject, UnityEngine.EventSystems.EventTriggerType.EndDrag, b =>
{
2021-09-02 14:18:26 +08:00
Debug.Log($"拽动结束{startPos},{endPos}");
2021-08-27 10:12:50 +08:00
endPos = scroll.horizontalNormalizedPosition;
2021-09-02 14:18:26 +08:00
goMovess();
2021-08-27 10:12:50 +08:00
});
2021-09-02 14:18:26 +08:00
#endif
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)=>
{
2021-08-27 10:12:50 +08:00
startPos = scroll.horizontalNormalizedPosition;
2021-04-19 15:44:11 +08:00
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) =>
{
2021-08-27 10:12:50 +08:00
startPos = scroll.horizontalNormalizedPosition;
2021-04-19 15:44:11 +08:00
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);
2021-09-02 14:18:26 +08:00
//Debug.Log(contentSize);
2021-08-24 17:50:14 +08:00
}
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;
#endif
2021-04-06 15:30:36 +08:00
SetColor();
}
2021-09-02 14:18:26 +08:00
void goMovess()
{
int i = (int)System.Math.Round((scroll.horizontalNormalizedPosition - startPos) / contentSize);
//Debug.Log("左滑开始" + );
if (i == 0)
{
scroll.horizontalNormalizedPosition = startPos;
return;
}
goMove(i);
}
#if UNITY_ANDROID || UNITY_IOS
2021-03-26 16:36:31 +08:00
void goLeft()
{
2021-09-02 14:18:26 +08:00
if (scroll.horizontalNormalizedPosition <= 0 + 0.0001)
{
return;
}
goMove(-1);
2021-03-26 16:36:31 +08:00
}
void goRight()
{
2021-09-02 14:18:26 +08:00
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();
}
2021-09-02 14:18:26 +08:00
#endif
void goMove(int i = 1)
{
2021-09-02 14:18:26 +08:00
Debug.Log("移动系数" + i);
2021-04-21 16:16:14 +08:00
doAni();
2021-08-27 10:12:50 +08:00
//#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;
2021-09-02 14:18:26 +08:00
scrollIndex = i;
2021-03-26 16:36:31 +08:00
start = true;
2021-08-27 10:12:50 +08:00
#if !(UNITY_ANDROID || UNITY_IOS)
scrollValue = i * contentSize / 10;
#else
scrollValue = ((GetIndex(startPos) + i) * contentSize - startPos) / 10;
#endif
2021-03-26 16:36:31 +08:00
}
private bool start = false;
private float scrollValue = 0, totalScrollValue = 0, startPosition = 0;
2021-09-02 14:18:26 +08:00
private int scrollIndex = 0;
2021-08-27 10:12:50 +08:00
int GetIndex(float? startPos = null)
{
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
2021-08-25 14:32:49 +08:00
if (contentSize == 0)
{
return 1;
}
2021-08-27 10:12:50 +08:00
return (int)System.Math.Round
((startPos ?? scroll.horizontalNormalizedPosition + contentSize) / contentSize, 0);
2021-08-24 17:50:14 +08:00
#endif
}
2021-09-02 14:18:26 +08:00
void SetColor(int? rIndex = null)
2021-08-24 17:50:14 +08:00
{
2021-09-02 14:18:26 +08:00
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++)
{
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-08-27 10:12:50 +08:00
#if !(UNITY_ANDROID || UNITY_IOS)
2021-04-23 15:36:18 +08:00
scroll.horizontalNormalizedPosition = startPosition + (totalScrollValue<0?-1:1)*contentSize;
2021-08-27 10:12:50 +08:00
#else
2021-09-02 14:18:26 +08:00
scroll.horizontalNormalizedPosition = (GetIndex(startPos) + scrollIndex) * contentSize;
2021-08-27 10:12:50 +08:00
#endif
//Debug.Log();
SetColor();
2021-03-26 16:36:31 +08:00
totalScrollValue = 0;
}
}
}
}