165 lines
4.0 KiB
C#
165 lines
4.0 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Video;
|
|
using TMPro;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
|
|
public class ContentViewer : MonoBehaviour
|
|
{
|
|
[Header("父物体")]
|
|
public GameObject textRoot;
|
|
public GameObject imageRoot;
|
|
public GameObject videoRoot;
|
|
|
|
[Header("展示组件")]
|
|
public TMP_Text textDisplay;
|
|
public Image imageDisplay;
|
|
public VideoPlayer videoPlayer;
|
|
public RawImage videoRawImage;
|
|
|
|
[Header("切换按钮")]
|
|
public Button prevButton;
|
|
public Button nextButton;
|
|
|
|
public Button close;
|
|
|
|
private List<string> files = new List<string>();
|
|
private int index = 0;
|
|
|
|
void Awake()
|
|
{
|
|
HideAll();
|
|
|
|
prevButton.onClick.AddListener(Prev);
|
|
nextButton.onClick.AddListener(Next);
|
|
close.onClick.AddListener(OnClose);
|
|
}
|
|
|
|
|
|
void OnClose()
|
|
{
|
|
Destroy(transform.parent.gameObject);
|
|
}
|
|
|
|
void HideAll()
|
|
{
|
|
textRoot.SetActive(false);
|
|
imageRoot.SetActive(false);
|
|
videoRoot.SetActive(false);
|
|
}
|
|
|
|
// ----- 加载文件夹,并按文本→图片→视频排序 -----
|
|
public void LoadFolder(string folderPath)
|
|
{
|
|
if (!Directory.Exists(folderPath))
|
|
{
|
|
Debug.LogError("路径不存在: " + folderPath);
|
|
return;
|
|
}
|
|
|
|
files.Clear();
|
|
|
|
files.AddRange(Directory.GetFiles(folderPath, "*.txt"));
|
|
files.AddRange(Directory.GetFiles(folderPath, "*.png"));
|
|
files.AddRange(Directory.GetFiles(folderPath, "*.jpg"));
|
|
files.AddRange(Directory.GetFiles(folderPath, "*.jpeg"));
|
|
files.AddRange(Directory.GetFiles(folderPath, "*.mp4"));
|
|
files.AddRange(Directory.GetFiles(folderPath, "*.mov"));
|
|
|
|
if (files.Count == 0)
|
|
{
|
|
Debug.LogWarning("文件夹无可用文件: " + folderPath);
|
|
return;
|
|
}
|
|
|
|
// 排序规则
|
|
files.Sort((a, b) =>
|
|
{
|
|
int typeA = GetTypeOrder(a);
|
|
int typeB = GetTypeOrder(b);
|
|
|
|
if (typeA != typeB)
|
|
return typeA.CompareTo(typeB);
|
|
|
|
return Path.GetFileName(a).CompareTo(Path.GetFileName(b));
|
|
});
|
|
|
|
index = 0;
|
|
ShowCurrent();
|
|
UpdateButtonState();
|
|
}
|
|
|
|
private int GetTypeOrder(string path)
|
|
{
|
|
string ext = Path.GetExtension(path).ToLower();
|
|
if (ext == ".txt") return 0;
|
|
if (ext == ".png" || ext == ".jpg" || ext == ".jpeg") return 1;
|
|
if (ext == ".mp4" || ext == ".mov") return 2;
|
|
return 99;
|
|
}
|
|
|
|
void ShowCurrent()
|
|
{
|
|
HideAll();
|
|
|
|
string path = files[index];
|
|
string ext = Path.GetExtension(path).ToLower();
|
|
|
|
if (ext == ".txt")
|
|
{
|
|
textDisplay.text = File.ReadAllText(path);
|
|
textRoot.SetActive(true);
|
|
}
|
|
else if (ext == ".png" || ext == ".jpg" || ext == ".jpeg")
|
|
{
|
|
byte[] data = File.ReadAllBytes(path);
|
|
Texture2D tex = new Texture2D(2, 2);
|
|
tex.LoadImage(data);
|
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(.5f, .5f));
|
|
|
|
imageDisplay.sprite = sprite;
|
|
imageRoot.SetActive(true);
|
|
}
|
|
else if (ext == ".mp4" || ext == ".mov")
|
|
{
|
|
videoPlayer.Stop();
|
|
videoPlayer.url = path;
|
|
|
|
RenderTexture rt = new RenderTexture(1920, 1080, 16);
|
|
videoPlayer.targetTexture = rt;
|
|
videoRawImage.texture = rt;
|
|
|
|
videoRoot.SetActive(true);
|
|
videoPlayer.Play();
|
|
}
|
|
|
|
UpdateButtonState();
|
|
}
|
|
|
|
// ----- 更新按钮状态 -----
|
|
void UpdateButtonState()
|
|
{
|
|
prevButton.interactable = index > 0;
|
|
nextButton.interactable = index < files.Count - 1;
|
|
}
|
|
|
|
public void Next()
|
|
{
|
|
if (index < files.Count - 1)
|
|
{
|
|
index++;
|
|
ShowCurrent();
|
|
}
|
|
}
|
|
|
|
public void Prev()
|
|
{
|
|
if (index > 0)
|
|
{
|
|
index--;
|
|
ShowCurrent();
|
|
}
|
|
}
|
|
}
|