【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一下,能锻炼一下手感,大佬略过哈。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜梦说开发(VR)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值