2023-04-03 11:04:31 +08:00

118 lines
4.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace BF
{
public class CellObj
{
public GameObject gameObject;
public int index; //从0开始 实际index
public int objectIndex; //从0开始 cell递增 cell唯一索引
}
public class ScrollRectCenter : ScrollRect
{
protected override void Awake()
{
content.localPosition = Vector3.zero;
content.pivot = horizontal ? new Vector2(0, 0.5f) : new Vector2(0.5f, 1);
}
private ScrollRectCenterController cachescrollRectCenterController;
public ScrollRectCenterController scrollRectCenterController
{
get
{
if (null == cachescrollRectCenterController)
{
cachescrollRectCenterController = GetComponent<ScrollRectCenterController>();
}
return cachescrollRectCenterController;
}
}
private Vector3 currentVelocity = Vector3.zero;
protected override void LateUpdate()
{
if (!content)
return;
if (scrollRectCenterController.moveToImmediately)
{
if (vertical)
{
Vector3 vector3Tager = new Vector3(content.localPosition.x, scrollRectCenterController.targetPosition, content.localPosition.z);
content.localPosition = vector3Tager;
}
else if (horizontal)
{
Vector3 vector3Tager = new Vector3(scrollRectCenterController.targetPosition, content.localPosition.y, content.localPosition.z);
content.localPosition = vector3Tager;
}
scrollRectCenterController.moveToImmediately = false;
if (vertical)
{
OnUpdatePositionVer();
}
else if (horizontal)
{
OnUpdatePositionHor();
}
}
//更新cell size
if (vertical)
{
if (Mathf.Abs(content.localPosition.y - scrollRectCenterController.targetPosition) > 1)
{
OnUpdatePositionVer();
}
else
{
return;
}
}
else if (horizontal)
{
if (Mathf.Abs(content.localPosition.x - scrollRectCenterController.targetPosition) > 1)
{
OnUpdatePositionHor();
}
else
{
return;
}
}
// 非drag时 平滑移动至中心点
if (!scrollRectCenterController.isDrag)
{
if (vertical)
{
Vector3 vector3Tager = new Vector3(content.localPosition.x, scrollRectCenterController.targetPosition, content.localPosition.z);
content.localPosition = Vector3.SmoothDamp(content.localPosition, vector3Tager,
ref currentVelocity, 0.05f, Mathf.Infinity, Time.unscaledDeltaTime);
}
else if (horizontal)
{
Vector3 vector3Tager = new Vector3(scrollRectCenterController.targetPosition, content.localPosition.y, content.localPosition.z);
content.localPosition = Vector3.SmoothDamp(content.localPosition, vector3Tager,
ref currentVelocity, 0.05f, Mathf.Infinity, Time.unscaledDeltaTime);
}
}
}
private void OnUpdatePositionHor()
{
scrollRectCenterController.OnUpdatePositionHor();
}
private void OnUpdatePositionVer()
{
scrollRectCenterController.OnUpdatePositionVer();
}
}
}