139 lines
3.7 KiB
C#
139 lines
3.7 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
/// <summary>
|
|
/// 附件项UI组件
|
|
/// </summary>
|
|
public class AttachmentItem : MonoBehaviour
|
|
{
|
|
[Header("UI组件")]
|
|
public Image iconImage; // 文件图标
|
|
public TextMeshProUGUI fileNameText; // 文件名文本
|
|
public TextMeshProUGUI fileSizeText; // 文件大小文本
|
|
public Button removeButton; // 删除按钮
|
|
public Button downloadButton; // 下载/打开按钮
|
|
|
|
public string filePath; // 文件路径
|
|
private Action<AttachmentItem> onRemove; // 删除回调
|
|
|
|
/// <summary>
|
|
/// 初始化附件项
|
|
/// </summary>
|
|
/// <param name="filePath">文件路径</param>
|
|
/// <param name="fileIcon">文件图标</param>
|
|
/// <param name="onRemoveCallback">删除回调</param>
|
|
public void Initialize(string filePath, Sprite fileIcon, Action<AttachmentItem> onRemoveCallback = null)
|
|
{
|
|
this.filePath = filePath;
|
|
this.onRemove = onRemoveCallback;
|
|
|
|
// 设置文件图标
|
|
if (iconImage != null)
|
|
{
|
|
iconImage.sprite = fileIcon;
|
|
iconImage.preserveAspect = true;
|
|
}
|
|
|
|
// 设置文件名
|
|
if (fileNameText != null)
|
|
{
|
|
fileNameText.text = System.IO.Path.GetFileName(filePath);
|
|
}
|
|
|
|
// 设置文件大小
|
|
if (fileSizeText != null)
|
|
{
|
|
long fileSize = new System.IO.FileInfo(filePath).Length;
|
|
fileSizeText.text = FormatFileSize(fileSize);
|
|
}
|
|
|
|
// 设置删除按钮事件
|
|
if (removeButton != null)
|
|
{
|
|
removeButton.onClick.RemoveAllListeners();
|
|
removeButton.onClick.AddListener(OnRemoveClicked);
|
|
}
|
|
|
|
// 设置下载/打开按钮事件
|
|
if (downloadButton != null)
|
|
{
|
|
downloadButton.onClick.RemoveAllListeners();
|
|
downloadButton.onClick.AddListener(OnOpenFileClicked);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除按钮点击事件
|
|
/// </summary>
|
|
private void OnRemoveClicked()
|
|
{
|
|
if (onRemove != null)
|
|
{
|
|
onRemove(this);
|
|
}
|
|
else
|
|
{
|
|
// 如果没有外部回调,则直接销毁自身
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开文件按钮点击事件
|
|
/// </summary>
|
|
private void OnOpenFileClicked()
|
|
{
|
|
if (!string.IsNullOrEmpty(filePath) && System.IO.File.Exists(filePath))
|
|
{
|
|
// 在不同平台上打开文件
|
|
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
|
|
try
|
|
{
|
|
System.Diagnostics.Process.Start(filePath);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"无法打开文件: {e.Message}");
|
|
}
|
|
#elif UNITY_ANDROID || UNITY_IOS
|
|
// 移动平台需要特殊处理
|
|
Debug.Log("移动平台打开文件功能待实现");
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("文件不存在: " + filePath);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 格式化文件大小
|
|
/// </summary>
|
|
/// <param name="bytes">字节数</param>
|
|
/// <returns>格式化的文件大小字符串</returns>
|
|
private string FormatFileSize(long bytes)
|
|
{
|
|
string[] sizes = { "B", "KB", "MB", "GB" };
|
|
int order = 0;
|
|
double len = bytes;
|
|
|
|
while (len >= 1024 && order < sizes.Length - 1)
|
|
{
|
|
order++;
|
|
len = len / 1024;
|
|
}
|
|
|
|
return $"{len:F2} {sizes[order]}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取文件路径
|
|
/// </summary>
|
|
/// <returns>文件路径</returns>
|
|
public string GetFilePath()
|
|
{
|
|
return filePath;
|
|
}
|
|
} |