您的位置:首页 > 汽车 > 新车 > 【Unity】手写图片轮播

【Unity】手写图片轮播

2024/10/22 7:57:12 来源:https://blog.csdn.net/CSDN_6954/article/details/142182010  浏览:    关键词:【Unity】手写图片轮播

最近项目空闲,回顾了一下之前写的杂七杂八的软件,比较多,而且比较杂乱,代码能看明白,但是非常不科学,不符合逻辑,然后我就有点无奈,虽说是做了很多年的老程序的,但是遇到现在公司的这种小软件,基本都是硬写,没有什么框架,没有什么标准,写出来能实现功能就行。

然后就是复盘一下自己写的一些小杂货,看看有没有什么可以积累的内容,忽然发现有一个东西可以积累下来,就是手写的图片轮播模块。

有两个版本的,一个是Resources下加载的,一个是StreamingAssets下加载的,但是感觉大差不差。

先是Resources加载的:

using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using System;public class PicChange : MonoSingleton<PicChange>
{public Button lastBtn;public Button nextBtn;public RawImage picImg;public Text picName;//先获取加载所有图片路径  然后点击哪个按钮进入哪一组图片内void Start(){Debug.Log("Start:"+ index);lastBtn.onClick.AddListener(lastPic);nextBtn.onClick.AddListener(nextPic);}Texture2D[] sprites;/// <summary>/// 确定是哪组图片在进行切换/// </summary>/// <param name="index"></param>public void SetPath(string endPath){sprites = Resources.LoadAll<Texture2D>(endPath);InitPic(0);}int index = 0;/// <summary>/// 初始化切换图片模块/// </summary>/// <param name="index"></param>private void InitPic(int index=0){try{Texture2D pic = sprites[index];//Debug.Log(index);//Debug.Log(pic);picImg.texture = pic;picImg.SetNativeSize();picName.text = sprites[index].name;}catch (Exception o){Debug.Log("异常信息"+o.Message);}  }/// <summary>/// 上一张图片/// </summary>private void lastPic() //0 1 2 3 4 5{index = index-- <= 0 ? sprites.Length - 1 : index--; //如果index--小于0 则sprites.Length - 1为长度 否则为index--Debug.Log(index);InitPic(index);}/// <summary>/// 下一张图片/// </summary>private void nextPic(){index = index++ >= sprites.Length - 1 ? 0 : index++; //如果index++大于长度 则index为0 否则为index++Debug.Log(index);InitPic(index);}
}

接下来就是StreamingAssets下加载的,这个也是翻看了一些大佬的博客借鉴了一下:

using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.IO;/// <summary>
/// 基于streamingAssets文件夹下图片动态切换及加载.
/// </summary>
public class PicChangeStreamingAssets : MonoSingleton<PicChange>
{public Button lastBtn;public Button nextBtn;public RawImage picImg;public Text picName;//先获取加载所有图片路径  然后点击哪个按钮进入哪一组图片内void Start(){lastBtn.onClick.AddListener(lastPic);nextBtn.onClick.AddListener(nextPic);//SetPath("1"); //获取图片地址}/// <summary>/// 确定是哪组图片在进行切换/// </summary>/// <param name="index"></param>public void SetPath(string endPath){Load(endPath);InitPic();}// 储存获取到的图片  List<Texture2D> allTex2d ;#region  图片加载模块/// <summary>/// 读取StremingAssets文件夹指定的文件夹目录下的所有图片进行加载/// </summary>/// <param name="path"> StremingAssets文件夹下的文件夹名字 </param>void Load(string path){allTex2d = new List<Texture2D>();List<string> filePaths = new List<string>();string[] dirs = null;string imgtype = "*.BMP|*.JPG|*.GIF|*.PNG";string[] ImageType = imgtype.Split('|');for (int i = 0; i < ImageType.Length; i++){//获取Application.dataPath文件夹下所有的图片路径  dirs = Directory.GetFiles((Application.streamingAssetsPath + "/" + path), ImageType[i]);for (int j = 0; j < dirs.Length; j++){filePaths.Add(dirs[j]);}}Debug.Log("图片数量:" + dirs.Length);for (int i = 0; i < filePaths.Count; i++){Texture2D tx = new Texture2D(187, 287);tx.LoadImage(GetImageByte(filePaths[i]));///获取图片名字,并去除.png 后缀tx.name = filePaths[i].Substring(filePaths[i].LastIndexOf(@"\") + 1, filePaths[i].LastIndexOf('.') - filePaths[i].LastIndexOf('\\') - 1);allTex2d.Add(tx);Debug.Log("Texture2D Name:" + tx.name + ".png");}}/// <summary>  /// 根据图片路径返回图片的字节流byte[]  /// </summary>  /// <param name="imagePath">图片路径</param>  /// <returns>返回的字节流</returns>  public 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;}/// <summary>/// 将Texture2d转换为Sprite/// </summary>/// <param name="tex">参数是texture2d纹理</param>/// <returns></returns>private Sprite TextureToSprite(Texture2D tex){Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));return sprite;}#endregion#region  图片切换模块int index = 0;/// <summary>/// 初始化切换图片模块/// </summary>/// <param name="index"></param>private void InitPic(int index = 0){try{Texture2D pic = allTex2d[index];Debug.Log(index);Debug.Log(pic);picImg.texture = pic;picImg.SetNativeSize();picName.text = allTex2d[index].name;}catch (Exception o){Debug.Log("异常信息" + o.Message);}}/// <summary>/// 上一张图片/// </summary>private void lastPic(){index = index-- < 0 ? allTex2d.Count - 1 : index--; //如果index--小于0 则index为长度 否则为index--InitPic(index);}/// <summary>/// 下一张图片/// </summary>private void nextPic(){index = index++ >= allTex2d.Count - 1 ? 0 : index++; //如果index++大于长度 则index为0 否则为index++InitPic(index);}#endregion
}

Unity中的层级如下:

脚本直接挂在这个上面就行。

实际效果如上图,有些内容是保密的哈,打了个马赛克,大家可以自己慢慢看,如果需求强烈的话,可以私聊我要一下资源包。

这个比较简单,建议大家最好是自己diy一下,能锻炼一下手感,大佬略过哈。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com