缺点是,在Unity中,自己要手动设置一下图片的位置。图片间的间隔固定。在Update中判断超出距离,就重新设置位置
代码如下
public class DragColorPar : Singleton<DragColorPar>,IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerDownHandler
{
public RectTransform[] ChildItem;
public Camera UICamera;
public float StarY = 0;
public float EndY = 0;
public RectTransform UpBoundary;
public RectTransform DownBoundary;
public Vector3[] FitLocalPos;
public bool isAutoMove = false;//自动拖动
public float InPosX;
public float OutPosX;
private bool drag = false;
public void OnPointerDown(PointerEventData eventData)
{
SetPauseAutoMove();
}
public void OnBeginDrag(PointerEventData eventData)
{
//CancelInvoke("BeginAutoMove");
//PauseAutoMove();
StarY = UICamera.ScreenToWorldPoint(new Vector3(eventData.position.x, eventData.position.y, 0)).y;
if (Input.touchCount <= 1)
drag = true;
}
public void OnDrag(PointerEventData eventData)
{
if (!drag)
return;
EndY = UICamera.ScreenToWorldPoint(new Vector3(eventData.position.x, eventData.position.y, 0)).y;
for (int i = 0; i < ChildItem.Length; i++)
{
var pos = ChildItem[i].position;
pos.y += (EndY - StarY);
ChildItem[i].position = pos;
}
StarY = EndY;
}
public void OnEndDrag(PointerEventData eventData)
{
if (!drag)
return;
drag = false;
// DebugBuild.Log(this.transform.GetChild(0).GetChild(2).localPosition + "LLLLLLLLLLLLLL");
EndFit();
}
/// <summary>
/// 结束滑动的时候,子物体对准自己的位置,始终只显示3个
/// </summary>
public void EndFit()
{
for (int i = 0; i < FitLocalPos.Length; i++)
{
this.transform.GetChild(0).GetChild(i).DOLocalMove(FitLocalPos[i], 0.25f);
}
//CancelInvoke("BeginAutoMove");
//Invoke("BeginAutoMove", 1f);//1秒之后,自动拖动
}
// Use this for initialization
void Start()
{
FitLocalPos = new Vector3[this.transform.GetChild(0).childCount];
for (int i = 0; i < FitLocalPos.Length; i++)
{
FitLocalPos[i] = this.transform.GetChild(0).GetChild(i).localPosition;
}
OutPosX = 160f;
InPosX = -120f;
}
void SetLoopPos()
{
for (int i = 0; i < ChildItem.Length; i++)
{
if (ChildItem[i].transform.position.y > UpBoundary.position.y)
{
ChildItem[i].localPosition = this.transform.GetChild(0).GetChild(this.transform.GetChild(0).childCount - 1).localPosition + new Vector3(0, -130f, 0);//这里的130.是你摆放的图片和图片的间隔
ChildItem[i].transform.SetAsLastSibling();
}
else if (ChildItem[i].transform.position.y < DownBoundary.position.y)
{
ChildItem[i].transform.localPosition = this.transform.GetChild(0).GetChild(0).localPosition + new Vector3(0, 130f, 0);
ChildItem[i].transform.SetAsFirstSibling();
}
}
}
public void AutoRotate(DMoveDirection dmd, float speed = 50f)
{
for (int i = 0; i < ChildItem.Length; i++)
{
var pos = ChildItem[i].localPosition;
pos.y += Time.deltaTime * speed * (int)dmd;
ChildItem[i].localPosition = pos;
}
}
public void SetAutoMove()
{
CancelInvoke("BeginAutoMove");
Invoke("BeginAutoMove", 1f);
}
public void SetPauseAutoMove()
{
CancelInvoke("BeginAutoMove");
PauseAutoMove();
}
private void BeginAutoMove()
{
isAutoMove = true;
}
private void PauseAutoMove()
{
isAutoMove = false;
}
// Update is called once per frame
void Update()
{
if (isAutoMove)
{
AutoRotate(DMoveDirection.上);
}
SetLoopPos();
}
}
[图片上传中...(无限拖动.png-22d4c5-1511517591189-0)]