264 lines
7.2 KiB
C#
264 lines
7.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using static MainCanvasManager;
|
|
|
|
public class MediaViwerPanel : PanelBasic
|
|
{
|
|
/// <summary>
|
|
/// 关闭
|
|
/// </summary>
|
|
public Button close_button;
|
|
/// <summary>
|
|
/// 导入媒体
|
|
/// </summary>
|
|
public Button import_media_button;
|
|
|
|
public Toggle video_files_button;
|
|
public Toggle audio_files_button;
|
|
private TextMeshProUGUI video_label;
|
|
private TextMeshProUGUI audio_label;
|
|
|
|
public Image video_media_panel;
|
|
public Image audio_media_panel;
|
|
|
|
public RectTransform video_media_content;
|
|
public RectTransform audio_media_content;
|
|
|
|
/// <summary>
|
|
/// 预制体
|
|
/// </summary>
|
|
public MediaViewerItem media_viewer_item_prefab;
|
|
/// <summary>
|
|
/// 对象池
|
|
/// </summary>
|
|
public List<MediaViewerItem> media_viwer_item_pool;
|
|
|
|
/// <summary>
|
|
/// 右键菜单
|
|
/// </summary>
|
|
public RectTransform context_menu_image;
|
|
public Button context_menu_preview_button;
|
|
public Button context_menu_delete_button;
|
|
|
|
/// <summary>
|
|
/// 当前选中的
|
|
/// </summary>
|
|
public MediaViewerItem current_media_item;
|
|
|
|
/// <summary>
|
|
/// 文件字典,路径$@$文件名
|
|
/// </summary>
|
|
public static List<MediaViewerItem> file_dic = new List<MediaViewerItem>();
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
close_button.onClick.AddListener(OnClose);
|
|
import_media_button.onClick.AddListener(OnImportMedia);
|
|
video_files_button.onValueChanged.AddListener(OnVideoFiles);
|
|
audio_files_button.onValueChanged.AddListener(OnAudioFiles);
|
|
|
|
context_menu_preview_button.onClick.AddListener(OnPreview);
|
|
context_menu_delete_button.onClick.AddListener(OnDelete);
|
|
|
|
MyInit();
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
file_dic.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增初始化
|
|
/// </summary>
|
|
public void MyInit()
|
|
{
|
|
if (PlayerPrefs.HasKey("media_cache"))
|
|
{
|
|
var lis = PlayerPrefs.GetString("media_cache");
|
|
var lis_sp = lis.Split(">*<");
|
|
for (int i = 0; i < lis_sp.Length; i++)
|
|
{
|
|
if (!string.IsNullOrEmpty(lis_sp[i]))
|
|
{
|
|
var lis_sp_sp = lis_sp[i].Split("/*/");
|
|
Init(lis_sp_sp[0], lis_sp_sp[1], lis_sp_sp[2], true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool Init(string _file_type, string _file_name, string _path, bool _start_init = false)
|
|
{
|
|
bool _add_new_data = false; //是否有新增
|
|
if (media_viewer_item_prefab == null) media_viewer_item_prefab = Resources.Load<MediaViewerItem>("Prefabs/UIItem/MediaViewerItem");
|
|
|
|
if (file_dic.Find(x => x.file_path.Equals(_path)))
|
|
{
|
|
ConsolePanel.ConsoleOutput(_path + " 文件已存在!");
|
|
return _add_new_data = false;
|
|
}
|
|
else
|
|
{
|
|
_add_new_data = true;
|
|
}
|
|
|
|
if (!_start_init)
|
|
{
|
|
var lis = "";
|
|
if (PlayerPrefs.HasKey("media_cache"))
|
|
{
|
|
lis = PlayerPrefs.GetString("media_cache");
|
|
}
|
|
lis += ">*<" + _file_type + "/*/" + _file_name + "/*/" + _path;
|
|
PlayerPrefs.SetString("media_cache", lis);
|
|
}
|
|
|
|
if (_file_type == "mp3")
|
|
{
|
|
var _audio_item = Instantiate(media_viewer_item_prefab, audio_media_content);
|
|
_audio_item.Init(_file_type, _file_name, _path);
|
|
file_dic.Add(_audio_item);
|
|
}
|
|
else if (_file_type == "mp4" || _file_type == "mov" || _file_type == "avi")
|
|
{
|
|
var _video_item = Instantiate(media_viewer_item_prefab, video_media_content);
|
|
_video_item.Init(_file_type, _file_name, _path);
|
|
file_dic.Add(_video_item);
|
|
}
|
|
else if (_file_type == "jpg" || _file_type == "png" || _file_type == "jpeg")
|
|
{
|
|
|
|
}
|
|
|
|
return _add_new_data;
|
|
}
|
|
|
|
private MediaViewerItem Get(RectTransform _content)
|
|
{
|
|
MediaViewerItem __item = null;
|
|
if (media_viwer_item_pool == null)
|
|
{
|
|
media_viwer_item_pool = new List<MediaViewerItem>();
|
|
}
|
|
else
|
|
{
|
|
if (media_viwer_item_pool.Count > 0)
|
|
{
|
|
__item = media_viwer_item_pool[0];
|
|
media_viwer_item_pool.Remove(__item);
|
|
}
|
|
}
|
|
if (__item == null)
|
|
{
|
|
__item = Instantiate(media_viewer_item_prefab, _content);
|
|
}
|
|
else
|
|
{
|
|
__item.gameObject.SetActive(true);
|
|
__item.transform.SetParent(_content);
|
|
}
|
|
return __item;
|
|
}
|
|
|
|
private void Recycle(MediaViewerItem _item)
|
|
{
|
|
_item.gameObject.SetActive(false);
|
|
_item.transform.SetParent(transform);
|
|
media_viwer_item_pool.Add(_item);
|
|
|
|
var p = PlayerPrefs.GetString("media_cache");
|
|
var p_sp = p.Split(">*<");
|
|
for (int i = 0; i < p_sp.Length; i++)
|
|
{
|
|
if (!string.IsNullOrEmpty(p_sp[i]))
|
|
{
|
|
if (p_sp[i].Equals(_item.perfer_str))
|
|
{
|
|
p_sp[i] = "";
|
|
}
|
|
}
|
|
}
|
|
p = "";
|
|
for (int i = 0; i < p_sp.Length; i++)
|
|
{
|
|
if (!string.IsNullOrEmpty(p_sp[i]))
|
|
{
|
|
p += ">*<" + p_sp[i];
|
|
}
|
|
}
|
|
PlayerPrefs.SetString("media_cache", p);
|
|
}
|
|
|
|
public void OnClose()
|
|
{
|
|
OnPopupEnd();
|
|
}
|
|
|
|
public void OnImportMedia()
|
|
{
|
|
menu_panel.OnImportMedia();
|
|
}
|
|
|
|
public void OnVideoFiles(bool _ison)
|
|
{
|
|
video_media_panel.gameObject.SetActive(_ison);
|
|
video_label.color = new Color(1, 1, 1, _ison ? 1 : 0.5f);
|
|
}
|
|
|
|
public void OnAudioFiles(bool _ison)
|
|
{
|
|
audio_media_panel.gameObject.SetActive(_ison);
|
|
audio_label.color = new Color(1, 1, 1, _ison ? 1 : 0.5f);
|
|
}
|
|
|
|
public void OnPreview()
|
|
{
|
|
if (current_media_item.file_type.Equals("mp3") || current_media_item.file_type.Equals("wav"))
|
|
{
|
|
MediaPlayer.Instance.AudioLoad(current_media_item.file_name_text.text, (result,_file_name) =>
|
|
{
|
|
if (result)
|
|
{
|
|
media_preview_panel.OnPopup();
|
|
media_preview_panel.PreviewAudio(_file_name);
|
|
}
|
|
});
|
|
}
|
|
else if (current_media_item.file_type.Equals("mp4") || current_media_item.file_type.Equals("avi"))
|
|
{
|
|
media_preview_panel.OnPopup();
|
|
media_preview_panel.PreviewVideo(current_media_item.file_name_text.text, current_media_item.file_path);
|
|
}
|
|
}
|
|
|
|
|
|
public void OnDelete()
|
|
{
|
|
confirm_panel.OnPopup("删除确认", "是否确认删除" + current_media_item.file_name_text.text + "?", "提示", (_delete) =>
|
|
{
|
|
if (_delete)
|
|
{
|
|
Recycle(current_media_item);
|
|
context_menu_image.gameObject.SetActive(false);
|
|
file_dic.Remove(current_media_item);
|
|
current_media_item = null;
|
|
}
|
|
else
|
|
{
|
|
|
|
}
|
|
});
|
|
}
|
|
}
|