Files
ZHGD_Web/Assets/Script/Controller/RawImageController.cs

115 lines
2.9 KiB
C#
Raw Permalink Normal View History

2025-07-13 23:16:20 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class RawImageController : MonoBehaviour
{
public string Name;
public List<Texture2D> Images = new List<Texture2D>();
2025-07-16 15:37:39 +08:00
private List<Texture2D> TempImages = new List<Texture2D>();
2025-07-13 23:16:20 +08:00
private string ImagePath = Application.streamingAssetsPath + "\\Data\\测试数据\\智慧党建\\图片\\";
private int CurrentIndex = 0;
private RawImage RawImage;
private string path;
void Start()
{
path = ImagePath + Name;
2025-07-18 17:50:42 +08:00
RawImage = transform.Find("01").GetComponent<RawImage>();
2025-07-13 23:16:20 +08:00
2025-07-15 18:33:21 +08:00
transform.Find("L_Button").GetComponent<Button>().onClick.AddListener(() =>
{
2025-07-13 23:16:20 +08:00
ImageController(-1);
});
2025-07-15 18:33:21 +08:00
transform.Find("R_Button").GetComponent<Button>().onClick.AddListener(() =>
{
2025-07-13 23:16:20 +08:00
ImageController(1);
});
ChartDataUpdateController.Inst.ChartUpdateAction += ImageDataUpate;
ImageDataUpate();
}
2025-07-15 18:33:21 +08:00
private void ImageController(int index)
{
2025-07-16 15:37:39 +08:00
if (Images.Count == 0)
return;
2025-07-13 23:16:20 +08:00
CurrentIndex += index;
int i = CurrentIndex % Images.Count;
if (i < 0)
{
ImageUpdate(Images.Count + i);
}
else if (i > 0)
{
ImageUpdate(i);
}
2025-07-15 18:33:21 +08:00
else
{
2025-07-13 23:16:20 +08:00
ImageUpdate(0);
}
}
2025-07-15 18:33:21 +08:00
private void ImageDataUpate()
{
2025-07-16 15:37:39 +08:00
// string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
WebRequsetTool.Inst.Get(path + "\\Images.txt", (str) =>
2025-07-15 18:33:21 +08:00
{
2025-07-16 15:37:39 +08:00
TempImages.Clear();
string[] files = str.Split("\r\n");
foreach (string file in files)
2025-07-15 18:33:21 +08:00
{
// Images.Add(GetTextureByString(SetImageToString(file)));
2025-07-16 15:37:39 +08:00
WebRequsetTool.Inst.GetTexture(path + "\\" + file, (texture) =>
2025-07-15 18:33:21 +08:00
{
2025-07-16 15:37:39 +08:00
TempImages.Add(texture);
Images = TempImages;
ImageController(0);
2025-07-15 18:33:21 +08:00
});
2025-07-13 23:16:20 +08:00
}
2025-07-16 15:37:39 +08:00
});
2025-07-13 23:16:20 +08:00
}
2025-07-15 18:33:21 +08:00
private void ImageUpdate(int i)
{
2025-07-13 23:16:20 +08:00
RawImage.texture = Images[i];
}
/// <summary>
/// 将图片转化为字符串
/// </summary>
private string SetImageToString(string imgPath)
{
FileStream fs = new FileStream(imgPath, FileMode.Open);
byte[] imgByte = new byte[fs.Length];
fs.Read(imgByte, 0, imgByte.Length);
fs.Close();
return Convert.ToBase64String(imgByte);
}
/// <summary>
/// 将字符串转换为纹理
/// </summary>
private Texture2D GetTextureByString(string textureStr)
{
Texture2D tex = new Texture2D(1, 1);
byte[] arr = Convert.FromBase64String(textureStr);
tex.LoadImage(arr);
tex.Apply();
return tex;
}
2025-07-15 18:33:21 +08:00
2025-07-13 23:16:20 +08:00
void Update()
{
2025-07-15 18:33:21 +08:00
2025-07-13 23:16:20 +08:00
}
}