225 lines
7.4 KiB
C#
225 lines
7.4 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using SK.Framework;
|
||
|
||
#if UNITY_EDITOR
|
||
using UnityEditor;
|
||
#endif
|
||
|
||
/// <summary>
|
||
/// 文件上传管理器
|
||
/// </summary>
|
||
public class FileUploadManager : MonoSingleton<FileUploadManager>
|
||
{
|
||
[Header("文件类型图标")]
|
||
public Sprite defaultFileIcon; // 默认文件图标
|
||
public Sprite imageFileIcon; // 图片文件图标
|
||
public Sprite pdfFileIcon; // PDF文件图标
|
||
public Sprite wordFileIcon; // Word文档图标
|
||
public Sprite excelFileIcon; // Excel文档图标
|
||
public Sprite powerpointFileIcon; // PowerPoint文档图标
|
||
public Sprite textFileIcon; // 文本文档图标
|
||
public Sprite videoFileIcon; // 视频文件图标
|
||
public Sprite audioFileIcon; // 音频文件图标
|
||
public Sprite zipFileIcon; // 压缩文件图标
|
||
|
||
// 支持的图片格式扩展名
|
||
private readonly string[] imageExtensions = { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tga", ".psd", ".exr" };
|
||
|
||
// 支持的文档格式扩展名
|
||
private readonly string[] documentExtensions = { ".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".txt" };
|
||
|
||
// 支持的媒体格式扩展名
|
||
private readonly string[] mediaExtensions = { ".mp4", ".avi", ".mov", ".wmv", ".flv", ".mp3", ".wav", ".ogg" };
|
||
|
||
// 支持的压缩格式扩展名
|
||
private readonly string[] archiveExtensions = { ".zip", ".rar", ".7z", ".tar", ".gz" };
|
||
private void OnEnable()
|
||
{
|
||
defaultFileIcon = Resources.Load<Sprite>("Icon/defaultFileIcon");
|
||
imageFileIcon = Resources.Load<Sprite>("Icon/imageFileIcon");
|
||
pdfFileIcon = Resources.Load<Sprite>("Icon/powerpointFileIcon");
|
||
wordFileIcon = Resources.Load<Sprite>("Icon/wordFileIcon");
|
||
excelFileIcon = Resources.Load<Sprite>("Icon/excelFileIcon");
|
||
powerpointFileIcon = Resources.Load<Sprite>("Icon/powerpointFile");
|
||
textFileIcon = Resources.Load<Sprite>("Icon/textFileIcon");
|
||
videoFileIcon = Resources.Load<Sprite>("Icon/videoFileIcon");
|
||
audioFileIcon = Resources.Load<Sprite>("Icon/audioFileIcon");
|
||
zipFileIcon = Resources.Load<Sprite>("Icon/zipFileIcon");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 选择文件并上传
|
||
/// </summary>
|
||
/// <param name="onFileSelected">文件选择回调</param>
|
||
/// <param name="filter">文件过滤器,例如 "Image Files;*.png;*.jpg;*.jpeg"</param>
|
||
public void SelectAndUploadFile(Action<string> onFileSelected, string filter = "")
|
||
{
|
||
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
|
||
string path = EditorUtility.OpenFilePanel("选择文件", "", filter);
|
||
if (!string.IsNullOrEmpty(path))
|
||
{
|
||
onFileSelected?.Invoke(path);
|
||
}
|
||
#elif UNITY_WEBGL
|
||
// WebGL平台使用JS插件进行文件上传
|
||
StartCoroutine(UploadFileWebGL(onFileSelected));
|
||
#else
|
||
// 其他平台可能需要特定的文件选择器实现
|
||
Debug.LogWarning("当前平台不支持文件选择功能");
|
||
#endif
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取文件图标
|
||
/// </summary>
|
||
/// <param name="filePath">文件路径</param>
|
||
/// <returns>对应的文件图标</returns>
|
||
public Sprite GetFileIcon(string filePath)
|
||
{
|
||
if (string.IsNullOrEmpty(filePath))
|
||
return defaultFileIcon;
|
||
|
||
string extension = Path.GetExtension(filePath).ToLower();
|
||
|
||
// 检查是否为图片格式
|
||
foreach (string ext in imageExtensions)
|
||
{
|
||
if (extension == ext)
|
||
return imageFileIcon ?? defaultFileIcon;
|
||
}
|
||
|
||
// 检查是否为文档格式
|
||
foreach (string ext in documentExtensions)
|
||
{
|
||
if (extension == ext)
|
||
{
|
||
switch (extension)
|
||
{
|
||
case ".pdf":
|
||
return pdfFileIcon ?? defaultFileIcon;
|
||
case ".doc":
|
||
case ".docx":
|
||
return wordFileIcon ?? defaultFileIcon;
|
||
case ".xls":
|
||
case ".xlsx":
|
||
return excelFileIcon ?? defaultFileIcon;
|
||
case ".ppt":
|
||
case ".pptx":
|
||
return powerpointFileIcon ?? defaultFileIcon;
|
||
case ".txt":
|
||
return textFileIcon ?? defaultFileIcon;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 检查是否为媒体格式
|
||
foreach (string ext in mediaExtensions)
|
||
{
|
||
if (extension == ext)
|
||
{
|
||
switch (extension)
|
||
{
|
||
case ".mp4":
|
||
case ".avi":
|
||
case ".mov":
|
||
case ".wmv":
|
||
case ".flv":
|
||
return videoFileIcon ?? defaultFileIcon;
|
||
case ".mp3":
|
||
case ".wav":
|
||
case ".ogg":
|
||
return audioFileIcon ?? defaultFileIcon;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 检查是否为压缩格式
|
||
foreach (string ext in archiveExtensions)
|
||
{
|
||
if (extension == ext)
|
||
{
|
||
return zipFileIcon ?? defaultFileIcon;
|
||
}
|
||
}
|
||
|
||
// 默认文件图标
|
||
return defaultFileIcon;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取文件名(不含路径)
|
||
/// </summary>
|
||
/// <param name="filePath">文件路径</param>
|
||
/// <returns>文件名</returns>
|
||
public string GetFileName(string filePath)
|
||
{
|
||
if (string.IsNullOrEmpty(filePath))
|
||
return "";
|
||
|
||
return Path.GetFileName(filePath);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取文件大小(字节)
|
||
/// </summary>
|
||
/// <param name="filePath">文件路径</param>
|
||
/// <returns>文件大小</returns>
|
||
public long GetFileSize(string filePath)
|
||
{
|
||
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
|
||
return 0;
|
||
|
||
FileInfo fileInfo = new FileInfo(filePath);
|
||
return fileInfo.Length;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取格式化的文件大小字符串
|
||
/// </summary>
|
||
/// <param name="filePath">文件路径</param>
|
||
/// <returns>格式化的文件大小</returns>
|
||
public string GetFormattedFileSize(string filePath)
|
||
{
|
||
long size = GetFileSize(filePath);
|
||
return FormatFileSize(size);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 格式化文件大小
|
||
/// </summary>
|
||
/// <param name="bytes">字节数</param>
|
||
/// <returns>格式化的文件大小字符串</returns>
|
||
public 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]}";
|
||
}
|
||
|
||
#if UNITY_WEBGL
|
||
/// <summary>
|
||
/// WebGL平台文件上传协程
|
||
/// </summary>
|
||
/// <param name="onFileSelected">文件选择回调</param>
|
||
private IEnumerator UploadFileWebGL(Action<string> onFileSelected)
|
||
{
|
||
// WebGL平台需要使用JavaScript插件来实现文件上传
|
||
// 这里只是一个示例,实际实现需要配合前端JS代码
|
||
Debug.Log("WebGL平台文件上传功能待实现");
|
||
yield return null;
|
||
}
|
||
#endif
|
||
} |