当前位置:网站首页>Unity scroll view element drag and drop to automatically adsorb centering and card effect
Unity scroll view element drag and drop to automatically adsorb centering and card effect
2022-06-30 05:07:00 【zjh_ three hundred and sixty-eight】
utilize Scroll View Control drag and drop to automatically absorb the centering effect

1、 Create a new one Scrool View Control , Remove two Scroolbar, Because there's no need for ;
And then in Viewport Next build a CenterPoint Empty object follows Content Use the same level as the center to compare ;
Finally, give Content Additive elements .

2、 Cancel vertical drag , The removed Vertical Checked by ;Movement Type Set to Unrestricted, Here's the picture .

3、yiyi Corresponding reference object ; add to Event Trigger Components , Add again Begin Drag and End Drag event , Then call the script code .

4、 The code is as follows
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testttt : MonoBehaviour {
public RectTransform viewport;
public RectTransform[] elements;
public RectTransform centerPoint;
public RectTransform content;
private Vector2 newPos;
private int distanceBetweenEles;
private float[] distanceToCenter;
private int minEleNum;
private bool isDragging = false;
// Use this for initialization
void Start () {
int elelength = elements.Length;
distanceToCenter = new float[elelength];
// Get the element spacing distance
distanceBetweenEles = (int)Mathf.Abs(elements[1].anchoredPosition.x - elements[0].anchoredPosition.x);
}
void Update()
{
// When the current position is close to the calculated adsorption position, the minimum position will not be calculated
if (Vector2.Distance(content.anchoredPosition, newPos)>5f)
{
for (int i = 0; i < elements.Length; i++)
{
// Get the distance from each element to the center point
distanceToCenter[i] = Mathf.Abs(centerPoint.transform.position.x - elements[i].transform.position.x);
}
// Get the closest distance
float minDist = Mathf.Min(distanceToCenter);
for (int i = 0; i < elements.Length; i++)
{
// Find the element index with the minimum distance
if (minDist == distanceToCenter[i])
{
minEleNum = i;
}
}
}
if(!isDragging)
{
// Currently, there is no drag , Automatic adsorption centering
newPos = new Vector2(minEleNum * -distanceBetweenEles, viewport.anchoredPosition.y);
content.anchoredPosition = Vector2.Lerp(content.anchoredPosition, newPos, Time.deltaTime * 5f);
}
}
public void StartDrag()
{
isDragging = true;
}
public void EndDrag()
{
isDragging = false;
}
}
边栏推荐
- 003-JS-DOM-Attr-innerText
- Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor
- Passing values between classes using delegates and events
- Unreal 4 learning notes - set player birth point
- Records of problems encountered in unity + hololens development
- 力扣349. 两个数组的交集
- Leetcode 180 Consecutive numbers (2022.06.29)
- Four methods of unity ugui button binding events
- 炒美原油的国际交易平台如何能保障资金安全呢?
- 力扣59. 螺旋矩阵 II
猜你喜欢

Exploration of unity webgl

力扣704. 二分查找

Have a heart beating Valentine's day in Singapore

Autowired注解警告的解决办法

Some problems encountered in unity steamvr

Deep learning ----- different methods to realize inception-10

Unity packaging and publishing webgl error reason exception: failed building webgl player

力扣977. 有序数组的平方
Sourcetree usage

Unity project hosting platform plasticscm (learn to use 1)
随机推荐
pycharm 数据库工具
产生 BUG 测试人员需要自己去分析原因吗?
虚析构和纯虚析构
Redis cluster concept
Have a heart beating Valentine's day in Singapore
[vcs+verdi joint simulation] ~ take the counter as an example
OpenGL draws model on QT platform to solve the problem of initializing VAO and VBO
Error about the new version of UE4: unavigationsystemv1:: simplemovetoactor has been deprecated
Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor
LXC 和 LXD 容器总结
Database base (Study & review for self use)
Unity application class and data file path
Unity3d packaging and publishing APK process
z-index属性在什么情况下会失效?
C # equipment synthesis
Tensorflow2 of ubantu18.04 X installation
力扣589:N 叉树的前序遍历
一条命令运行rancher
Unity dotween plug-in description
QT connecting external libraries