Last active
April 28, 2019 13:45
-
-
Save yangruihan/58d5672382ddff4456d65c6443b29dce to your computer and use it in GitHub Desktop.
游戏工具类
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.IO; | |
using System.Linq; | |
using System; | |
public static class GameUtility | |
{ | |
public const string AssetsFolderName = "Assets"; | |
public static string FormatToUnityPath(string path) | |
{ | |
return path.Replace("\\", "/"); | |
} | |
public static string FormatToSysFilePath(string path) | |
{ | |
return path.Replace("/", "\\"); | |
} | |
public static string FullPathToAssetPath(string fullPath) | |
{ | |
fullPath = FormatToUnityPath(fullPath); | |
if (!fullPath.StartsWith(Application.dataPath)) | |
{ | |
return null; | |
} | |
var retPath = fullPath.Replace(Application.dataPath, ""); | |
return AssetsFolderName + retPath; | |
} | |
public static string GetFileExtension(string path) | |
{ | |
var extension = Path.GetExtension(path); | |
return extension == null ? null : extension.ToLower(); | |
} | |
public static string[] GetSpecifyFilesInFolder(string path, string[] extensions = null, bool exclude = false) | |
{ | |
if (string.IsNullOrEmpty(path)) | |
{ | |
return null; | |
} | |
if (extensions == null) | |
{ | |
return Directory.GetFiles(path, "*.*", SearchOption.AllDirectories); | |
} | |
else if (exclude) | |
{ | |
return Directory.GetFiles(path, "*.*", SearchOption.AllDirectories) | |
.Where(f => !extensions.Contains(GetFileExtension(f))).ToArray(); | |
} | |
else | |
{ | |
return Directory.GetFiles(path, "*.*", SearchOption.AllDirectories) | |
.Where(f => extensions.Contains(GetFileExtension(f))).ToArray(); | |
} | |
} | |
public static string[] GetSpecifyFilesInFolder(string path, string pattern) | |
{ | |
if (string.IsNullOrEmpty(path)) | |
{ | |
return null; | |
} | |
return Directory.GetFiles(path, pattern, SearchOption.AllDirectories); | |
} | |
public static string[] GetAllFilesInFolder(string path) | |
{ | |
return GetSpecifyFilesInFolder(path); | |
} | |
public static string[] GetAllDirsInFolder(string path) | |
{ | |
return Directory.GetDirectories(path, "*", SearchOption.AllDirectories); | |
} | |
public static void CheckFileAndCreateDirWhenNeeded(string filePath) | |
{ | |
if (string.IsNullOrEmpty(filePath)) | |
{ | |
return; | |
} | |
var fileInfo = new FileInfo(filePath); | |
var dirInfo = fileInfo.Directory; | |
if (dirInfo != null && !dirInfo.Exists) | |
{ | |
Directory.CreateDirectory(dirInfo.FullName); | |
} | |
} | |
public static void CheckDirAndCreateWhenNeeded(string folderPath) | |
{ | |
if (string.IsNullOrEmpty(folderPath)) | |
{ | |
return; | |
} | |
if (!Directory.Exists(folderPath)) | |
{ | |
Directory.CreateDirectory(folderPath); | |
} | |
} | |
public static bool SafeWriteAllBytes(string outFile, byte[] outBytes) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(outFile)) | |
{ | |
return false; | |
} | |
CheckFileAndCreateDirWhenNeeded(outFile); | |
if (File.Exists(outFile)) | |
{ | |
File.SetAttributes(outFile, FileAttributes.Normal); | |
} | |
File.WriteAllBytes(outFile, outBytes); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError( | |
string.Format("SafeWriteAllBytes failed! path = {0} with err = {1}", outFile, ex.Message)); | |
return false; | |
} | |
} | |
public static bool SafeWriteAllLines(string outFile, string[] outLines) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(outFile)) | |
{ | |
return false; | |
} | |
CheckFileAndCreateDirWhenNeeded(outFile); | |
if (File.Exists(outFile)) | |
{ | |
File.SetAttributes(outFile, FileAttributes.Normal); | |
} | |
File.WriteAllLines(outFile, outLines); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError( | |
string.Format("SafeWriteAllLines failed! path = {0} with err = {1}", outFile, ex.Message)); | |
return false; | |
} | |
} | |
public static bool SafeWriteAllText(string outFile, string text) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(outFile)) | |
{ | |
return false; | |
} | |
CheckFileAndCreateDirWhenNeeded(outFile); | |
if (File.Exists(outFile)) | |
{ | |
File.SetAttributes(outFile, FileAttributes.Normal); | |
} | |
File.WriteAllText(outFile, text); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError(string.Format("SafeWriteAllText failed! path = {0} with err = {1}", outFile, | |
ex.Message)); | |
return false; | |
} | |
} | |
public static byte[] SafeReadAllBytes(string inFile) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(inFile)) | |
{ | |
return null; | |
} | |
if (!File.Exists(inFile)) | |
{ | |
return null; | |
} | |
File.SetAttributes(inFile, FileAttributes.Normal); | |
return File.ReadAllBytes(inFile); | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError(string.Format("SafeReadAllBytes failed! path = {0} with err = {1}", inFile, ex.Message)); | |
return null; | |
} | |
} | |
public static string[] SafeReadAllLines(string inFile) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(inFile)) | |
{ | |
return null; | |
} | |
if (!File.Exists(inFile)) | |
{ | |
return null; | |
} | |
File.SetAttributes(inFile, FileAttributes.Normal); | |
return File.ReadAllLines(inFile); | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError(string.Format("SafeReadAllLines failed! path = {0} with err = {1}", inFile, ex.Message)); | |
return null; | |
} | |
} | |
public static string SafeReadAllText(string inFile) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(inFile)) | |
{ | |
return null; | |
} | |
if (!File.Exists(inFile)) | |
{ | |
return null; | |
} | |
File.SetAttributes(inFile, FileAttributes.Normal); | |
return File.ReadAllText(inFile); | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError(string.Format("SafeReadAllText failed! path = {0} with err = {1}", inFile, ex.Message)); | |
return null; | |
} | |
} | |
public static void DeleteDirectory(string dirPath) | |
{ | |
var files = Directory.GetFiles(dirPath); | |
var dirs = Directory.GetDirectories(dirPath); | |
foreach (var file in files) | |
{ | |
File.SetAttributes(file, FileAttributes.Normal); | |
File.Delete(file); | |
} | |
foreach (var dir in dirs) | |
{ | |
DeleteDirectory(dir); | |
} | |
Directory.Delete(dirPath, false); | |
} | |
public static bool SafeClearDir(string folderPath) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(folderPath)) | |
{ | |
return true; | |
} | |
if (Directory.Exists(folderPath)) | |
{ | |
DeleteDirectory(folderPath); | |
} | |
Directory.CreateDirectory(folderPath); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError(string.Format("SafeClearDir failed! path = {0} with err = {1}", folderPath, ex.Message)); | |
return false; | |
} | |
} | |
public static bool SafeDeleteDir(string folderPath) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(folderPath)) | |
{ | |
return true; | |
} | |
if (Directory.Exists(folderPath)) | |
{ | |
DeleteDirectory(folderPath); | |
} | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError(string.Format("SafeDeleteDir failed! path = {0} with err: {1}", folderPath, ex.Message)); | |
return false; | |
} | |
} | |
public static bool SafeDeleteFile(string filePath) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(filePath)) | |
{ | |
return true; | |
} | |
if (!File.Exists(filePath)) | |
{ | |
return true; | |
} | |
File.SetAttributes(filePath, FileAttributes.Normal); | |
File.Delete(filePath); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError(string.Format("SafeDeleteFile failed! path = {0} with err: {1}", filePath, ex.Message)); | |
return false; | |
} | |
} | |
public static bool SafeRenameFile(string sourceFileName, string destFileName) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(sourceFileName)) | |
{ | |
return false; | |
} | |
if (!File.Exists(sourceFileName)) | |
{ | |
return true; | |
} | |
SafeDeleteFile(destFileName); | |
File.SetAttributes(sourceFileName, FileAttributes.Normal); | |
File.Move(sourceFileName, destFileName); | |
return true; | |
} | |
catch (System.Exception ex) | |
{ | |
Debug.LogError( | |
string.Format("SafeRenameFile failed! path = {0} with err: {1}", sourceFileName, ex.Message)); | |
return false; | |
} | |
} | |
public static bool SafeCopyFile(string fromFile, string toFile) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(fromFile)) | |
{ | |
return false; | |
} | |
if (!File.Exists(fromFile)) | |
{ | |
return false; | |
} | |
CheckFileAndCreateDirWhenNeeded(toFile); | |
SafeDeleteFile(toFile); | |
File.Copy(fromFile, toFile, true); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError( | |
string.Format("SafeCopyFile failed! formFile = {0}, toFile = {1}, with err = {2}", fromFile, toFile, | |
ex.Message)); | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.IO; | |
using System.Linq; | |
using System; | |
public static class GameUtility | |
{ | |
public const string AssetsFolderName = "Assets"; | |
public static string FormatToUnityPath(string path) | |
{ | |
return path.Replace("\\", "/"); | |
} | |
public static string FormatToSysFilePath(string path) | |
{ | |
return path.Replace("/", "\\"); | |
} | |
public static string FullPathToAssetPath(string fullPath) | |
{ | |
fullPath = FormatToUnityPath(fullPath); | |
if (!fullPath.StartsWith(Application.dataPath)) | |
{ | |
return null; | |
} | |
var retPath = fullPath.Replace(Application.dataPath, ""); | |
return AssetsFolderName + retPath; | |
} | |
public static string GetFileExtension(string path) | |
{ | |
return Path.GetExtension(path)?.ToLower(); | |
} | |
public static string[] GetSpecifyFilesInFolder(string path, string[] extensions = null, bool exclude = false) | |
{ | |
if (string.IsNullOrEmpty(path)) | |
{ | |
return null; | |
} | |
if (extensions == null) | |
{ | |
return Directory.GetFiles(path, "*.*", SearchOption.AllDirectories); | |
} | |
else if (exclude) | |
{ | |
return Directory.GetFiles(path, "*.*", SearchOption.AllDirectories) | |
.Where(f => !extensions.Contains(GetFileExtension(f))).ToArray(); | |
} | |
else | |
{ | |
return Directory.GetFiles(path, "*.*", SearchOption.AllDirectories) | |
.Where(f => extensions.Contains(GetFileExtension(f))).ToArray(); | |
} | |
} | |
public static string[] GetSpecifyFilesInFolder(string path, string pattern) | |
{ | |
if (string.IsNullOrEmpty(path)) | |
{ | |
return null; | |
} | |
return Directory.GetFiles(path, pattern, SearchOption.AllDirectories); | |
} | |
public static string[] GetAllFilesInFolder(string path) | |
{ | |
return GetSpecifyFilesInFolder(path); | |
} | |
public static string[] GetAllDirsInFolder(string path) | |
{ | |
return Directory.GetDirectories(path, "*", SearchOption.AllDirectories); | |
} | |
public static void CheckFileAndCreateDirWhenNeeded(string filePath) | |
{ | |
if (string.IsNullOrEmpty(filePath)) | |
{ | |
return; | |
} | |
var fileInfo = new FileInfo(filePath); | |
var dirInfo = fileInfo.Directory; | |
if (dirInfo != null && !dirInfo.Exists) | |
{ | |
Directory.CreateDirectory(dirInfo.FullName); | |
} | |
} | |
public static void CheckDirAndCreateWhenNeeded(string folderPath) | |
{ | |
if (string.IsNullOrEmpty(folderPath)) | |
{ | |
return; | |
} | |
if (!Directory.Exists(folderPath)) | |
{ | |
Directory.CreateDirectory(folderPath); | |
} | |
} | |
public static bool SafeWriteAllBytes(string outFile, byte[] outBytes) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(outFile)) | |
{ | |
return false; | |
} | |
CheckFileAndCreateDirWhenNeeded(outFile); | |
if (File.Exists(outFile)) | |
{ | |
File.SetAttributes(outFile, FileAttributes.Normal); | |
} | |
File.WriteAllBytes(outFile, outBytes); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError($"SafeWriteAllBytes failed! path = {outFile} with err = {ex.Message}"); | |
return false; | |
} | |
} | |
public static bool SafeWriteAllLines(string outFile, string[] outLines) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(outFile)) | |
{ | |
return false; | |
} | |
CheckFileAndCreateDirWhenNeeded(outFile); | |
if (File.Exists(outFile)) | |
{ | |
File.SetAttributes(outFile, FileAttributes.Normal); | |
} | |
File.WriteAllLines(outFile, outLines); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError($"SafeWriteAllLines failed! path = {outFile} with err = {ex.Message}"); | |
return false; | |
} | |
} | |
public static bool SafeWriteAllText(string outFile, string text) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(outFile)) | |
{ | |
return false; | |
} | |
CheckFileAndCreateDirWhenNeeded(outFile); | |
if (File.Exists(outFile)) | |
{ | |
File.SetAttributes(outFile, FileAttributes.Normal); | |
} | |
File.WriteAllText(outFile, text); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError($"SafeWriteAllText failed! path = {outFile} with err = {ex.Message}"); | |
return false; | |
} | |
} | |
public static byte[] SafeReadAllBytes(string inFile) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(inFile)) | |
{ | |
return null; | |
} | |
if (!File.Exists(inFile)) | |
{ | |
return null; | |
} | |
File.SetAttributes(inFile, FileAttributes.Normal); | |
return File.ReadAllBytes(inFile); | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError($"SafeReadAllBytes failed! path = {inFile} with err = {ex.Message}"); | |
return null; | |
} | |
} | |
public static string[] SafeReadAllLines(string inFile) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(inFile)) | |
{ | |
return null; | |
} | |
if (!File.Exists(inFile)) | |
{ | |
return null; | |
} | |
File.SetAttributes(inFile, FileAttributes.Normal); | |
return File.ReadAllLines(inFile); | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError($"SafeReadAllLines failed! path = {inFile} with err = {ex.Message}"); | |
return null; | |
} | |
} | |
public static string SafeReadAllText(string inFile) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(inFile)) | |
{ | |
return null; | |
} | |
if (!File.Exists(inFile)) | |
{ | |
return null; | |
} | |
File.SetAttributes(inFile, FileAttributes.Normal); | |
return File.ReadAllText(inFile); | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError($"SafeReadAllText failed! path = {inFile} with err = {ex.Message}"); | |
return null; | |
} | |
} | |
public static void DeleteDirectory(string dirPath) | |
{ | |
var files = Directory.GetFiles(dirPath); | |
var dirs = Directory.GetDirectories(dirPath); | |
foreach (var file in files) | |
{ | |
File.SetAttributes(file, FileAttributes.Normal); | |
File.Delete(file); | |
} | |
foreach (var dir in dirs) | |
{ | |
DeleteDirectory(dir); | |
} | |
Directory.Delete(dirPath, false); | |
} | |
public static bool SafeClearDir(string folderPath) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(folderPath)) | |
{ | |
return true; | |
} | |
if (Directory.Exists(folderPath)) | |
{ | |
DeleteDirectory(folderPath); | |
} | |
Directory.CreateDirectory(folderPath); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError($"SafeClearDir failed! path = {folderPath} with err = {ex.Message}"); | |
return false; | |
} | |
} | |
public static bool SafeDeleteDir(string folderPath) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(folderPath)) | |
{ | |
return true; | |
} | |
if (Directory.Exists(folderPath)) | |
{ | |
DeleteDirectory(folderPath); | |
} | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError($"SafeDeleteDir failed! path = {folderPath} with err: {ex.Message}"); | |
return false; | |
} | |
} | |
public static bool SafeDeleteFile(string filePath) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(filePath)) | |
{ | |
return true; | |
} | |
if (!File.Exists(filePath)) | |
{ | |
return true; | |
} | |
File.SetAttributes(filePath, FileAttributes.Normal); | |
File.Delete(filePath); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError($"SafeDeleteFile failed! path = {filePath} with err: {ex.Message}"); | |
return false; | |
} | |
} | |
public static bool SafeRenameFile(string sourceFileName, string destFileName) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(sourceFileName)) | |
{ | |
return false; | |
} | |
if (!File.Exists(sourceFileName)) | |
{ | |
return true; | |
} | |
SafeDeleteFile(destFileName); | |
File.SetAttributes(sourceFileName, FileAttributes.Normal); | |
File.Move(sourceFileName, destFileName); | |
return true; | |
} | |
catch (System.Exception ex) | |
{ | |
Debug.LogError( | |
$"SafeRenameFile failed! path = {sourceFileName} with err: {ex.Message}"); | |
return false; | |
} | |
} | |
public static bool SafeCopyFile(string fromFile, string toFile) | |
{ | |
try | |
{ | |
if (string.IsNullOrEmpty(fromFile)) | |
{ | |
return false; | |
} | |
if (!File.Exists(fromFile)) | |
{ | |
return false; | |
} | |
CheckFileAndCreateDirWhenNeeded(toFile); | |
SafeDeleteFile(toFile); | |
File.Copy(fromFile, toFile, true); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogError($"SafeCopyFile failed! formFile = {fromFile}, toFile = {toFile}, with err = {ex.Message}"); | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment