赞
踩
图片可以外部修改,但是目前这个包图片数量是固定的6个,可以再inspector面板中修改图片的个数,对应文字的个数和StreamingAssets中的图片个数也应该修改,
按左右两边的按钮也可以左右滑动,一次只能滑动一个图片,
适用于做一个项目的目录
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- using DG.Tweening;
-
- public class SliderCanMoveScrollView : MonoBehaviour, IBeginDragHandler, IEndDragHandler
- {
- //content的长度计算方法:(cellLength + spacing)*(num-1)
- private RectTransform contentTrans;
- private float beginMousePositionX;
- private float endMousePositionX;
- private ScrollRect scrollRect;
-
- public int cellLength;
- public int spacing;
- public int leftOffset;
- private float moveOneItemLength;
-
- private Vector3 currentContentLocalPos;//上一次的位置
- private Vector3 contentInitPos;//Content初始位置
- private Vector2 contentTransSize;//Content初始大小
-
- public int totalItemNum;
- private int currentIndex;
-
- public Text pageText;
-
- public bool needSendMessage;
-
- private void Awake()
- {
- scrollRect = GetComponent<ScrollRect>();
- contentTrans = scrollRect.content;
- moveOneItemLength = cellLength + spacing;
- currentContentLocalPos = contentTrans.localPosition;
- contentTransSize = contentTrans.sizeDelta;
- contentInitPos = contentTrans.localPosition;
- currentIndex = 1;
- if (pageText != null)
- {
- pageText.text = currentIndex.ToString() + "/" + totalItemNum;
- }
- }
-
- public void Init()
- {
- currentIndex = 1;
-
- if (contentTrans != null)
- {
- contentTrans.localPosition = contentInitPos;
- currentContentLocalPos = contentInitPos;
- if (pageText != null)
- {
- pageText.text = currentIndex.ToString() + "/" + totalItemNum;
- }
- }
- }
-
- /// <summary>
- /// 通过拖拽与松开来达成翻页效果
- /// </summary>
- /// <param name="eventData"></param>
-
- public void OnEndDrag(PointerEventData eventData)
- {
- endMousePositionX = Input.mousePosition.x;
- float offSetX = 0;
- float moveDistance = 0;//当次需要滑动的距离
- offSetX = beginMousePositionX - endMousePositionX;
-
- if (offSetX > 0)//右滑
- {
- if (currentIndex >= totalItemNum)
- {
- return;
- }
- if (needSendMessage)
- {
- UpdatePanel(true);
- }
-
- moveDistance = -moveOneItemLength;
- currentIndex++;
- }
- else//左滑
- {
- if (currentIndex <= 1)
- {
- return;
- }
- if (needSendMessage)
- {
- UpdatePanel(false);
- }
- moveDistance = moveOneItemLength;
- currentIndex--;
- }
- if (pageText != null)
- {
- pageText.text = currentIndex.ToString() + "/" + totalItemNum;
- }
- DOTween.To(() => contentTrans.localPosition, lerpValue => contentTrans.localPosition = lerpValue, currentContentLocalPos + new Vector3(moveDistance, 0, 0), 0.5f).SetEase(Ease.OutQuint);
- currentContentLocalPos += new Vector3(moveDistance, 0, 0);
- }
-
- /// <summary>
- /// 按钮来控制翻书效果
- /// </summary>
-
- public void ToNextPage()
- {
- float moveDistance = 0;
- if (currentIndex >= totalItemNum)
- {
- return;
- }
-
- moveDistance = -moveOneItemLength;
- currentIndex++;
- if (pageText != null)
- {
- pageText.text = currentIndex.ToString() + "/" + totalItemNum;
- }
- if (needSendMessage)
- {
- UpdatePanel(true);
- }
- DOTween.To(() => contentTrans.localPosition, lerpValue => contentTrans.localPosition = lerpValue, currentContentLocalPos + new Vector3(moveDistance, 0, 0), 0.5f).SetEase(Ease.OutQuint);
- currentContentLocalPos += new Vector3(moveDistance, 0, 0);
- }
-
- public void ToLastPage()
- {
- float moveDistance = 0;
- if (currentIndex <= 1)
- {
- return;
- }
-
- moveDistance = moveOneItemLength;
- currentIndex--;
- if (pageText != null)
- {
- pageText.text = currentIndex.ToString() + "/" + totalItemNum;
- }
- if (needSendMessage)
- {
- UpdatePanel(false);
- }
- DOTween.To(() => contentTrans.localPosition, lerpValue => contentTrans.localPosition = lerpValue, currentContentLocalPos + new Vector3(moveDistance, 0, 0), 0.5f).SetEase(Ease.OutQuint);
- currentContentLocalPos += new Vector3(moveDistance, 0, 0);
- }
-
- public void OnBeginDrag(PointerEventData eventData)
- {
- beginMousePositionX = Input.mousePosition.x;
- }
-
- //设置Content的大小
- public void SetContentLength(int itemNum)
- {
- contentTrans.sizeDelta = new Vector2(contentTrans.sizeDelta.x + (cellLength + spacing) * (itemNum - 1), contentTrans.sizeDelta.y);
- totalItemNum = itemNum;
- }
-
- //初始化Content的大小
- public void InitScrollLength()
- {
- contentTrans.sizeDelta = contentTransSize;
- }
-
- //发送翻页信息的方法
- public void UpdatePanel(bool toNext)
- {
- if (toNext)
- {
- gameObject.SendMessageUpwards("ToNextLevel");
- }
- else
- {
- gameObject.SendMessageUpwards("ToLastLevel");
- }
- }
- }
(其中content长度的计算方法也再代码中有)
图片外部导入
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Text.RegularExpressions;
- using UnityEngine;
- using UnityEngine.UI;
- /// <summary>
- /// 图片外部加载
- /// </summary>
- public class ImageManage : MonoBehaviour
- {
- List<Sprite> imageList = new List<Sprite>(); //加载的图片的列表
- [SerializeField] List<GameObject> images = new List<GameObject>(); //图片列表
- [SerializeField] List<Text> texts = new List<Text>(); //图片名称列表
-
-
- void Start()
- {
- LoadAllPic();
- for (int i = 0; i < imageList.Count; i++)
- {
- //将图片赋值到预制体上
- images[i].GetComponent<Image>().sprite = imageList[i];
- }
-
- }
- string[] dirs;
- //外部加载图片
- void LoadAllPic()
- {
- string imgtype = "*.JPG|*.PNG";
- string[] ImageType = imgtype.Split('|');
- List<string> filePathList = new List<string>();
- string[] dirs1 = Directory.GetFiles(Application.streamingAssetsPath + "/image/", ImageType[0]);
- List<string> dirs1List = new List<string>(dirs1);
- string[] dirs2 = Directory.GetFiles(Application.streamingAssetsPath + "/image/", ImageType[1]);
- //当两种格式都存在时,将第二个数组中的内容加入到第一个数组中去
- if (ImageType[0] != null && ImageType[1] != null)
- {
- for (int m = 0; m < dirs2.Length; m++)
- {
- string dir2 = dirs2[m];
- dirs1List.Add(dir2);
- }
- dirs = dirs1List.ToArray();
- }
- //当只有jpg格式时,将数组内容赋值
- else if (ImageType[0] != null && ImageType[1] == null)
- dirs = dirs1;
- //当只有png格式时,将数组内容赋值
- else if (ImageType[0] == null && ImageType[1] != null)
- dirs = dirs2;
-
- for (int j = 0; j < dirs.Length; j++)
- {
- Texture2D tx = new Texture2D(100, 100);
- tx.LoadImage(getImageByte(dirs[j]));
- Sprite sprite = Sprite.Create(tx, new Rect(0, 0, tx.width, tx.height), new Vector2(-500, -500));
-
- //获取sprite的名称
- sprite.name = Path.GetFileNameWithoutExtension(dirs[j]);
- //将名称用“-”分割
- string[] names = sprite.name.Split('-');
- //获取数字后面的名字并赋值
- texts[j].text = names[1];
- //将获取的图片加到图片列表中
- imageList.Add(sprite);
- }
-
- }
-
- private static byte[] getImageByte(string imagePath)
- {
- FileStream files = new FileStream(imagePath, FileMode.Open);
- byte[] imgByte = new byte[files.Length];
- files.Read(imgByte, 0, imgByte.Length);
- files.Close();
- return imgByte;
- }
- }
先新建一个ScrollRect,将代码挂载再上面,然后再content中添加Grid Layout Group组件,设置左偏移量,图片大小和间隔,将这些数值设置好之后,填入SliderCanMoveScrollView 脚本中,content中Right的计算方法(每个图片的长度+间隔长度)*(图片数量-1);
例如:图片长度是900,间隔是700,数量是5个,那么content的Right值为:(900+700)*4
两个button手动添加按钮监听就可以了。
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- using DG.Tweening;
-
- public class SlideCanCoverScrollView : MonoBehaviour, IBeginDragHandler, IEndDragHandler
- {
-
- private float contentLength;//容器长度
- private float beginMousePostionX;
- private float endMousePositionX;
- private ScrollRect scrollRect;
- private float lastProportion;//上一个位置比例
-
- public int cellLength;//每个单元格长度
- public int spacing;//间隙
- public int leftOffset;//左偏移量
- private float upperLimit;//上限值
- private float lowerLimit;//下限值
- private float firstItemLength;//移动第一个单元格的距离
- private float oneItemLength;//滑动一个单元格需要的距离
- private float oneItemProportion;//滑动一个单元格所占比例
-
- public int totalItemNum;//共有几个单元格
- private int currentIndex;//当前单元格索引
-
- public Text pageText;
-
- private void Awake()
- {
- scrollRect = GetComponent<ScrollRect>();
- contentLength = scrollRect.content.rect.xMax - 2 * leftOffset - cellLength;
- firstItemLength = cellLength / 2 + leftOffset;
- oneItemLength = cellLength + spacing;
- oneItemProportion = oneItemLength / (contentLength+ oneItemLength);
- upperLimit = 1 - firstItemLength / contentLength;
- lowerLimit = firstItemLength / contentLength;
- currentIndex = 0;
- scrollRect.horizontalNormalizedPosition = 0;
- if (pageText != null)
- {
- pageText.text = currentIndex.ToString() + "/" + totalItemNum;
- }
- }
-
- public void Init()
- {
- lastProportion = 0;
- currentIndex = 0;
- if (scrollRect != null)
- {
- scrollRect.horizontalNormalizedPosition = 0;
- pageText.text = currentIndex.ToString() + "/" + totalItemNum;
- }
- }
-
- public void OnEndDrag(PointerEventData eventData)
- {
- float offSetX = 0;
- endMousePositionX = Input.mousePosition.x;
- offSetX = (beginMousePostionX - endMousePositionX) * 2;
- //Debug.Log("offSetX:" + offSetX);
- //Debug.Log("firstItemLength:" + firstItemLength);
- if (Mathf.Abs(offSetX) > firstItemLength)//执行滑动动作的前提是要大于第一个需要滑动的距离
- {
- if (offSetX > 0)//右滑
- {
- if (currentIndex >= totalItemNum)
- {
- return;
- }
- int moveCount =
- (int)((offSetX - firstItemLength) / oneItemLength) + 1;//当次可以移动的格子数目
- currentIndex += moveCount;
- if (currentIndex >= totalItemNum)
- {
- currentIndex = totalItemNum;
- }
- //当次需要移动的比例:上一次已经存在的单元格位置
- //的比例加上这一次需要去移动的比例
- lastProportion += oneItemProportion;
- Debug.Log(lastProportion);
- if (lastProportion >= upperLimit)
- {
- lastProportion = 1;
- }
- }
- else //左滑
- {
- if (currentIndex <= 1)
- {
- return;
- }
- int moveCount =
- (int)((offSetX + firstItemLength) / oneItemLength) - 1;//当次可以移动的格子数目
- currentIndex += moveCount;
- if (currentIndex <= 1)
- {
- currentIndex = 1;
- }
- //当次需要移动的比例:上一次已经存在的单元格位置
- //的比例加上这一次需要去移动的比例
- lastProportion += oneItemProportion * moveCount;
- if (lastProportion <= lowerLimit)
- {
- lastProportion = 0;
- }
- }
- if (pageText != null)
- {
- pageText.text = currentIndex.ToString() + "/" + totalItemNum;
- }
-
- }
-
- DOTween.To(() => scrollRect.horizontalNormalizedPosition, lerpValue => scrollRect.horizontalNormalizedPosition = lerpValue, lastProportion, 0.5f).SetEase(Ease.OutQuint);
- //GameManager.Instance.audioSourceManager.PlayPagingAudioClip();
- }
-
- public void OnBeginDrag(PointerEventData eventData)
- {
- beginMousePostionX = Input.mousePosition.x;
- }
- }
三:一次滑动一个的资源链接
图片轮播,可以用于目录,展示一个个目录中的内容,应用场景有:保卫萝卜的选项卡,时间线的展示,人物的展示等等_unity3d - Unity3D模型_U3D_免费下载-爱给网
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。