115 lines
2.9 KiB
C#
115 lines
2.9 KiB
C#
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>();
|
|
private List<Texture2D> TempImages = new List<Texture2D>();
|
|
|
|
private string ImagePath = Application.streamingAssetsPath + "\\Data\\测试数据\\智慧党建\\图片\\";
|
|
private int CurrentIndex = 0;
|
|
private RawImage RawImage;
|
|
private string path;
|
|
|
|
void Start()
|
|
{
|
|
path = ImagePath + Name;
|
|
RawImage = transform.GetChild(0).GetComponent<RawImage>();
|
|
|
|
transform.Find("L_Button").GetComponent<Button>().onClick.AddListener(() =>
|
|
{
|
|
ImageController(-1);
|
|
});
|
|
transform.Find("R_Button").GetComponent<Button>().onClick.AddListener(() =>
|
|
{
|
|
ImageController(1);
|
|
});
|
|
|
|
ChartDataUpdateController.Inst.ChartUpdateAction += ImageDataUpate;
|
|
ImageDataUpate();
|
|
}
|
|
|
|
private void ImageController(int index)
|
|
{
|
|
if (Images.Count == 0)
|
|
return;
|
|
CurrentIndex += index;
|
|
int i = CurrentIndex % Images.Count;
|
|
|
|
if (i < 0)
|
|
{
|
|
ImageUpdate(Images.Count + i);
|
|
}
|
|
else if (i > 0)
|
|
{
|
|
ImageUpdate(i);
|
|
}
|
|
else
|
|
{
|
|
ImageUpdate(0);
|
|
}
|
|
}
|
|
|
|
private void ImageDataUpate()
|
|
{
|
|
// string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
|
|
WebRequsetTool.Inst.Get(path + "\\Images.txt", (str) =>
|
|
{
|
|
TempImages.Clear();
|
|
string[] files = str.Split("\r\n");
|
|
|
|
foreach (string file in files)
|
|
{
|
|
// Images.Add(GetTextureByString(SetImageToString(file)));
|
|
WebRequsetTool.Inst.GetTexture(path + "\\" + file, (texture) =>
|
|
{
|
|
TempImages.Add(texture);
|
|
Images = TempImages;
|
|
ImageController(0);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
private void ImageUpdate(int i)
|
|
{
|
|
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;
|
|
}
|
|
|
|
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
}
|