TyeUnityToolsFile.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class TyeUnityToolsFile : MonoBehaviour
{
public static string yeGetPath()
{
string eStrPath = $"{Application.persistentDataPath}/Tye/";
TyeUnityToolsFile.yePrint("Root路径", eStrPath);
return eStrPath;
}
public static void yeTyeFileBase() {
string ePathDir = $"{Application.persistentDataPath}/Tye/";
string ePathFile = $"{Application.persistentDataPath}/Tye/Tye.png";
// 判断是否存在文件夹/文件:
bool isHasDir = Directory.Exists(ePathDir); // 是否存在 此文件夹;
bool isHasFile = File.Exists(ePathFile); // 是否存在 此文件;
// 判断是否是文件夹/文件:
if (ePathDir is DirectoryInfo) {
TyeUnityToolsFile.yePrint("判断", "是文件 夹");
}
if (ePathDir is FileInfo)
{
TyeUnityToolsFile.yePrint("判断", "是文件");
}
yePrint("获取文件的扩展名", Path.GetExtension(ePathFile));
}
///-- 删除文件夹: 【必须先删除文件夹中的所有子文件和子文件夹, 形成空文件夹,才能删除本文件夹】
void yeDeleteDir(string ePath)
{
try
{
DirectoryInfo dir = new DirectoryInfo(ePath);
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
foreach (FileSystemInfo i in fileinfo)
{
if (i is DirectoryInfo)
{ //判断是否文件夹
DirectoryInfo subdir = new DirectoryInfo(i.FullName);
subdir.Delete(true); //删除子目录和文件
}
else
{
File.Delete(i.FullName); //删除指定文件
}
}
}
catch (Exception e)
{
//throw;
TyeUnityToolsFile.yePrint("删除文件夹,异常", $"{e}");
}
Directory.Delete(ePath);
}
///
public void yeDeleteFile(string ePathFile)
{
try
{
bool isHasFile = File.Exists(ePathFile); // 是否存在 此文件;
if (isHasFile)
{
File.Delete(ePathFile); //删除指定文件
}
}
catch (Exception e)
{
TyeUnityToolsFile.yePrint("删除文件,异常", $"{e}");
}
}
///-- 创建文件夹:
public void yeCreateDirectory(string ePathDir = "")
{
if (string.IsNullOrEmpty(ePathDir)) { //路径字符为空;
return;
}
if (Directory.Exists(ePathDir)) { // 文件夹 已经存在;
return;
}
Directory.CreateDirectory(ePathDir); // 创建文件夹;
}
void yeCreateFile(string path, string name, string info)
{
//文件流信息
StreamWriter sw;
string url = path + "/" + name;
FileInfo t = new FileInfo(url);
if (!t.Exists)
{
//如果此文件不存在则创建
sw = t.CreateText();
}
else
{
//方式一:如果此文件存在则打开,接着录入
sw = t.AppendText();
//方式二:如果需要覆盖该文件。删除该文件数据然后重新录入
//t.Delete();
//sw = t.CreateText();
}
sw.WriteLine(info); //以行的形式写入信息
sw.Close(); //关闭流
sw.Dispose(); //销毁流
Debug.LogFormat($"---write data:path:{url} , data: {info}");
}
//--- 图片保存在本地:
//yeSaveImgToLocally(eTexture2D, ePathSave + $"{yeGetVideoCurrentTimeFrames()}.png");
public static void yeSaveImgToLocally(Texture2D eTexture2D, string ePathSave)
{
if (null == ePathSave){
ePathSave = Application.dataPath + "/Tye.png";
}
Byte[] eArrBytes = eTexture2D.EncodeToPNG(); //编码;
File.WriteAllBytes(ePathSave, eArrBytes); //直接放在了Assets目录下,存在地址自行修改
}
public static IEnumerator yeSaveByteArrToLocally(string ePathSave, Byte[] eArrBytes) {
// 等待 将文件写入到本地 的完成:
yield return File.WriteAllBytesAsync(ePathSave, eArrBytes);
yePrint("文件写入本地","已完成");
}
public static void yePrint(string eStrMark, string eStrInfo) {
Debug.Log($"---打印,{eStrMark}:{eStrInfo}--");
}
static void yeSaveImg()
{
//string pos_file_name = frameDirName + pose + ".png";
encode to png
camera.RenderToCubemap(cubemap, 63, Camera.MonoOrStereoscopicEye.Mono);
cubemap.ConvertToEquirect(equirect, Camera.MonoOrStereoscopicEye.Mono);
//Texture2D ori_tex = new Texture2D(width, height, TextureFormat.BGRA32, false);
//ori_tex.ReadPixels(new Rect(0, 0, ori_tex.width, ori_tex.height), 0, 0, false);
//ori_tex.Apply();
//byte[] tex_bytes = ori_tex.EncodeToPNG();
//System.IO.File.WriteAllBytes(frameDirName + pose + ".png", tex_bytes);
//Destroy(ori_tex);
//tex_bytes = null;
}
///-- 以 IO流的方式读取文件:
private void yeLoadByIO(string path)
{
//Image img = GetComponent<Image>();
double startTime = (double)Time.time;
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] bytes = new byte[fileStream.Length]; //创建文件长度缓冲区
fileStream.Read(bytes, 0, (int)fileStream.Length);//读取文件
fileStream.Close(); //释放文件读取流
fileStream.Dispose(); //释放本机屏幕资源
fileStream = null;
//创建Texture
int width = 300;
int height = 372;
Texture2D eTexture = new Texture2D(width, height);
eTexture.LoadImage(bytes);
//创建Sprite
Sprite eSprite = Sprite.Create(eTexture, new Rect(0, 0, eTexture.width, eTexture.height), new Vector2(0.5f, 0.5f));
//eImgBG.sprite = eSprite;
startTime = (double)Time.time - startTime;
Debug.Log($"---IO加载时长:{startTime}--");
}
}