北海美人鱼地下场景演练
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

206 lines
6.6 KiB

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class VideoData
{
public string Url;//对应文件完整路径
public string Mode;//Practice为练习模式,manoeuvre为演习模式
public string RoomId;//房间id
public string RoomName;//房间名
public string Creator;//创建者
public string Name;//输入的文件名
public string RecoedTime;//录制持续时间
}
public class VideoFileCtrl
{
public static readonly VideoFileCtrl Instance = new VideoFileCtrl();
public List<VideoData> PracticeDatas = new List<VideoData>();
public List<VideoData> manoeuvreDatas = new List<VideoData>();
public string RECFilePath;
private List<string> PracticeUrlList = new List<string>();
private List<string> manoeuvreUrlList = new List<string>();
//private void Awake()
//{
// //DontDestroyOnLoad(this);
// //Instance = this;
// Init();
//}
// Start is called before the first frame update
//void Start()
//{
// RECFilePath = Path.Combine(Application.streamingAssetsPath, "RECFile");
// PracticeDatas = GetVideoData("/PracticeVideoList.json");
// manoeuvreDatas = GetVideoData("/manoeuvreVideoList.json");
// for (int i = 0; i < PracticeDatas.Count; i++)
// {
// PracticeUrlList.Add(PracticeDatas[i].Url);
// }
// for (int i = 0; i < manoeuvreDatas.Count; i++)
// {
// manoeuvreUrlList.Add(manoeuvreDatas[i].Url);
// }
//}
public void Init()
{
RECFilePath = Path.Combine(Application.streamingAssetsPath, "RECFile");
PracticeDatas = GetVideoData("/PracticeVideoList.json");
manoeuvreDatas = GetVideoData("/manoeuvreVideoList.json");
for (int i = 0; i < PracticeDatas.Count; i++)
{
PracticeUrlList.Add(PracticeDatas[i].Url);
}
for (int i = 0; i < manoeuvreDatas.Count; i++)
{
manoeuvreUrlList.Add(manoeuvreDatas[i].Url);
}
}
// Update is called once per frame
void Update()
{
}
public void LoadFileStringSync(string path, Action<string> complete)
{
if (!File.Exists(path))
return;
StreamReader sr = new StreamReader(path);
if (sr == null)
return;
string fileString = sr.ReadToEnd();
sr.Close();
sr.Dispose();
if (complete != null)
complete(fileString);
}
private List<VideoData> GetVideoData(string filepath)
{
List<VideoData> data = new List<VideoData>();
LoadFileStringSync(
RECFilePath + filepath,
VideoJsonData =>
{
var tasksData = JsonConvert.DeserializeObject<List<VideoData>>(VideoJsonData);
data = tasksData;
}
);
return data;
}
/// <summary>
/// 新生成视频时添加文件,并修改json
/// </summary>
/// <param name="videoData"></param>
/// <param name="mode"></param>
public void AddVideoDatasToJson(VideoData videoData,string mode)
{
if (mode== "manoeuvre")
{
if (!manoeuvreUrlList.Contains(videoData.Url))
{
manoeuvreUrlList.Add(videoData.Url);
manoeuvreDatas.Add(videoData);
}
string jsondata = JsonConvert.SerializeObject(manoeuvreDatas);
WriteFileToLocal(jsondata,Path.Combine(RECFilePath, "manoeuvreVideoList.json"));
}
if (mode== "Practice")
{
if (!PracticeUrlList.Contains(videoData.Url))
{
PracticeUrlList.Add(videoData.Url);
PracticeDatas.Add(videoData);
}
string jsondata = JsonConvert.SerializeObject(PracticeDatas);
WriteFileToLocal(jsondata, Path.Combine(RECFilePath, "PracticeVideoList.json"));
}
}
public void DelectVideoAndUpdateJson(string url,string mode)
{
if (mode == "manoeuvre")
{
if (manoeuvreUrlList.Contains(url))
{
manoeuvreUrlList.Remove(url);
}
for (int i = 0; i < manoeuvreDatas.Count; i++)
{
if (manoeuvreDatas[i].Url==url)
{
manoeuvreDatas.Remove(manoeuvreDatas[i]);
}
}
if (File.Exists(url))
{
File.Delete(url);
}
string imagepath = url.Replace(".mp4", ".png");
if (File.Exists(imagepath))
{
//删除图片文件
File.Delete(imagepath);
}
string jsondata = JsonConvert.SerializeObject(manoeuvreDatas);
WriteFileToLocal(jsondata, Path.Combine(RECFilePath, "manoeuvreVideoList.json"));
}
if (mode == "Practice")
{
if (PracticeUrlList.Contains(url))
{
PracticeUrlList.Remove(url);
}
for (int i = 0; i < PracticeDatas.Count; i++)
{
if (PracticeDatas[i].Url == url)
{
PracticeDatas.Remove(PracticeDatas[i]);
}
}
if (File.Exists(url))
{
//删除视频文件
File.Delete(url);
}
string imagepath = url.Replace(".mp4", ".png");
if (File.Exists(imagepath))
{
//删除图片文件
File.Delete(imagepath);
}
string jsondata = JsonConvert.SerializeObject(PracticeDatas);
WriteFileToLocal(jsondata, Path.Combine(RECFilePath, "PracticeVideoList.json"));
}
}
private void WriteFileToLocal(string fileJsonData, string filePath)
{
//var fileDirectory = Path.GetDirectoryName(filePath);
if (File.Exists(filePath))
{
File.Delete(filePath);
}
//if (!File.Exists(filePath))
//{
// File.Create(filePath);
//}
//using (var filestream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
//{
// var fileBytes = Encoding.UTF8.GetBytes(fileJsonData);
// filestream.Write(fileBytes, 0, fileBytes.Length);
//}
FileStream file = File.Create(filePath);
byte[] byteArray = Encoding.UTF8.GetBytes(fileJsonData);
file.Write(byteArray, 0, byteArray.Length);
file.Close();
}
}