ShanxiKnowledgeBase/SXElectricalInspection/Assets/Adam/Scripts/SystemController.cs

85 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
//============================================================
//支持中文文件使用UTF-8编码
//@author Adam
//@create 20240516
//@company LG
//
//@description:
//============================================================
public class SystemController : MonoBehaviour
{
public Button leftBtn;
public Button rightBtn;
public RawImage tips;
public Text tipText;
public int index = 0;
public int imageCount;
public Texture2D[] texturesTips;
// Use this for initialization
private void Start()
{
texturesTips = LoadSprite("Tips");
leftBtn.onClick.AddListener(OnLeft);
rightBtn.onClick.AddListener(OnRight);
imageCount = texturesTips.Length;
tips.texture = texturesTips[index];
tipText.text = texturesTips[index].name;
}
public void OnRight()
{
index++;
if (index >= imageCount)
{
index = imageCount - 1;
}
tipText.text = texturesTips[index].name;
tips.texture = texturesTips[index];
}
public void OnLeft()
{
index--;
if (index < 0)
{
index = 0;
}
tipText.text = texturesTips[index].name;
tips.texture = texturesTips[index];
}
public Texture2D[] LoadSprite(string name)
{
string fileName_1 = Path.Combine(Application.streamingAssetsPath, name);
Debug.Log(fileName_1);
if (!Directory.Exists(fileName_1))
{
throw new FileNotFoundException("Could not find file: " + fileName_1);
}
string[] filePaths = Directory.GetFiles(fileName_1, "*.*", SearchOption.AllDirectories)
.Where(s => s.EndsWith(".jpg") || s.EndsWith(".png"))
.ToArray();
Texture2D[] sprites = new Texture2D[filePaths.Length];
for (int i = 0; i < filePaths.Length; i++)
{
byte[] bytes = File.ReadAllBytes(filePaths[i]);
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(bytes);
string tipName = filePaths[i].Substring(filePaths[i].LastIndexOf(@"\") + 1, filePaths[i].LastIndexOf('.') - filePaths[i].LastIndexOf('\\') - 1);
string[] tempNames = tipName.Split("_");
texture.name = tempNames[2];
sprites[i] = texture;
}
return sprites;
}
}