当前位置:网站首页>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;
}
}
边栏推荐
- Chapter 9 of OpenGL super classic (version 7): fragment processing and frame buffering
- Unreal 4 learning notes - set player birth point
- The difference between SVG and canvas
- Unity project hosting platform plasticscm (learn to use 2)
- 中文版PyCharm改为英文版PyCharm
- Solution to 293 problems in the week of Li Kou
- Unity project hosting platform plasticscm (learn to use 1)
- A collection of errors encountered in machine learning with unity
- Unity + hololens2 performance test
- Database base (Study & review for self use)
猜你喜欢
Force buckle 27 Removing Elements
Unity lens making
Database base (Study & review for self use)
Win10 vs2015 compiling curaengine
How does unity use mapbox to implement real maps in games?
Unity + hololens2 performance test
Parkour demo
Autowired注解警告的解决办法
Basic operations of Oracle data
Easyrecovery data recovery software recovers my photo and video data two years ago
随机推荐
Unit screenshot saved on the phone
Oculus quest2 development: (I) basic environment construction and guide package
Revit二次开发---未打开项目使用面板功能
Autowired注解警告的解决办法
Nestjs配置静态资源,模板引擎以及Post示例
Records of some problems encountered during unity development (continuously updated)
Unity3d packaging and publishing APK process
Some books you should not miss when you are new to the workplace
Unity camera control
Unity packaging and publishing webgl error reason exception: failed building webgl player
【VCS+Verdi联合仿真】~ 以计数器为例
Special folders in unity3d and their meanings
PBR material: basic principle and simple fabrication
Pytorchcnn image recognition and classification model training framework
Unity lens making
力扣27. 移除元素
产生 BUG 测试人员需要自己去分析原因吗?
Unreal 4 learning notes - set player birth point
Operation file file class method
Pycharm database tool