今天是上班第一天,其他就也就不多说了,直接进入正题吧。今天要写的是Unity3D的UGUI的Scroll View的滑动居中,这个功能在NGUI里有现成的代码,这里的代码和NGUI的类似,思路都是一样的,据说做UGUI的就是那批做NGUI的人做的,闲话不多说了下面直接上代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections.Generic; using System; /// <summary> /// UGUI ScrollRect 滑动元素居中 /// </summary> public class UICenterOnChild : MonoBehaviour, IBeginDragHandler, IEndDragHandler { public float scrollSpeed = 8f; public Transform UIGrid; private ScrollRect scrollRect; private float[] pageArray; private float targetPagePosition = 0f; private bool isDrag = false; private int pageCount; private int currentPage = 0; private List<Transform> items = new List<Transform>(); // Use this for initialization void Awake() { scrollRect = GetComponent<ScrollRect>(); } void Start() { InitPageArray(); } /// <summary> /// 初始化获取元素总个数 /// </summary> void InitPageArray() { foreach (Transform item in UIGrid) { if (item.gameObject.activeSelf && !items.Contains(item)) { items.Add(item); } } pageCount = items.Count; pageArray = new float[pageCount]; for (int i = 0; i < pageCount; i++) { pageArray[i] = (1f / (pageCount - 1)) * i; } } // Update is called once per frame void Update() { if (!isDrag) { if (scrollRect.horizontal) { scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition, targetPagePosition, scrollSpeed * Time.deltaTime); } else if (scrollRect.vertical) { scrollRect.verticalNormalizedPosition = Mathf.Lerp(scrollRect.verticalNormalizedPosition, targetPagePosition, scrollSpeed * Time.deltaTime); } } } public void OnBeginDrag(PointerEventData eventData) { isDrag = true; } public void OnEndDrag(PointerEventData eventData) { isDrag = false; float pos = scrollRect.horizontal ? scrollRect.horizontalNormalizedPosition : scrollRect.verticalNormalizedPosition; int index = 0; float offset = Math.Abs(pageArray[index] - pos); for (int i = 1; i < pageArray.Length; i++) { float _offset = Math.Abs(pageArray[i] - pos); if (_offset < offset) { index = i; offset = _offset; } } targetPagePosition = pageArray[index]; currentPage = index; } /// <summary> /// 向左移动一个元素 /// </summary> public void ToLeft() { if (currentPage > 0) { currentPage = currentPage - 1; targetPagePosition = pageArray[currentPage]; } } /// <summary> /// 向右移动一个元素 /// </summary> public void ToRight() { if (currentPage < pageCount - 1) { currentPage = currentPage + 1; targetPagePosition = pageArray[currentPage]; } } /// <summary> /// 获取当前页码 /// </summary> /// <returns></returns> public int GetCurrentPageIndex() { return currentPage; } /// <summary> /// 设置当前页码 /// </summary> /// <param name="index"></param> public void SetCurrentPageIndex(int index) { currentPage = index; targetPagePosition = pageArray[currentPage]; } /// <summary> /// 获取总页数 /// </summary> /// <returns></returns> public int GetTotalPages() { return pageCount; } } |
把上面的脚本放到Scroll View上面,把Content拖到脚本上的 UIGrid上面,如下图:文章源自大腿Plus-https://www.zhaoshijun.com/archives/102
自己也可以拓展里面的代码,最后的几个方法就是我自己写的,为了方便外面获取信息用的,大家有更好的方法也可以留言,不懂的地方,看到回复我一定会解答的。文章源自大腿Plus-https://www.zhaoshijun.com/archives/102 文章源自大腿Plus-https://www.zhaoshijun.com/archives/102
我的微信
微信扫一扫
shijun_z
我的QQ
QQ扫一扫
846207670
1F
可以可以,等你更新。。。