using System; using System.Collections; using System.IO; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; public class GameTools : MonoBehaviour { #region 单例 private static GameTools share; static bool quit; public static GameTools Share { get { if (quit == true) { return null; } if (share == null) { share = FindObjectOfType(); if (share == null) { var obj = new GameObject("游戏工具"); share = obj.AddComponent(); DontDestroyOnLoad(obj); } } return share; } } public void OnDestroy() { quit = true; } #endregion #region 加载资源 /// /// 加载图片 /// public static void LoadImage(Image image, string Path) { try { byte[] imgData = File.ReadAllBytes(Path); Texture2D tex = new Texture2D(1920, 1080); tex.LoadImage(imgData); image.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f)); } catch (Exception e) { Debug.Log(e.ToString()); } } /// /// 加载图片 /// public static void LoadImage(string url, Action complete) { Share.StartCoroutine(Share.loadImageHander(url, complete)); } IEnumerator loadImageHander(string url, Action complete) { using (var request = UnityWebRequestTexture.GetTexture(url)) { yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError) { complete(null); } else { complete(DownloadHandlerTexture.GetContent(request)); } } } /// /// 释放图片 /// public static void ReleaseImage(Image image) { if (image.sprite != null) { var tex = image.sprite.texture; if (tex != null) { Resources.UnloadAsset(tex); } image.sprite = null; } } /// /// 释放图片 /// public static void ReleaseImage(RawImage image) { var tex = image.texture; if (tex != null) { Resources.UnloadAsset(tex); image.texture = null; } } /// /// 加载文本 /// public static void LoadHtml(string url, Action complete) { Share.StartCoroutine(Share.loadHtmlHander(url, complete)); } IEnumerator loadHtmlHander(string url, Action complete) { using (var request = UnityWebRequest.Get(url)) { yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError) { complete(null); } else { complete(request.downloadHandler.text); } } } /// /// 加载mp3 /// public static void LoadAudio(string url, Action complete) { Share.StartCoroutine(Share.loadAudioHander(url, complete)); } IEnumerator loadAudioHander(string url, Action complete) { using (var request = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.MPEG)) { yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError) { complete(null); } else { complete(DownloadHandlerAudioClip.GetContent(request)); } } } /// /// 添加组件 /// public static T LoadComponent(GameObject target) where T : Component { var cat = target.GetComponent(); if (cat == null) { cat = target.AddComponent(); } return cat; } /// /// 添加预制体 /// public static T LoadPrefab(string prefab, Transform parent) where T : Component { var pre = Resources.Load(prefab); if (pre != null) { var temp = Instantiate(pre); if (parent != null) { temp.transform.SetParent(parent, false); } return temp.GetComponent(); } return default(T); } #endregion #region 文件 /// /// 创建目录文件夹 有就不创建 /// public static void CreateDirectory(string filePath) { if (!string.IsNullOrEmpty(filePath)) { if (!File.Exists(filePath)) { Directory.CreateDirectory(filePath); } } } /// /// 写入文件 /// /// /// public static void WriteFile(string filePath, byte[] bytes) { using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write)) { fs.SetLength(0); fs.Write(bytes, 0, bytes.Length); } } /// /// 继续写入文件 /// /// /// public static void AppendWriteFile(string filePath, byte[] bytes) { using (FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write)) { fs.Write(bytes, 0, bytes.Length); } } /// /// 读取文件 /// /// /// public static byte[] ReadFile(string filePath) { using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { byte[] infbytes = new byte[fs.Length]; fs.Read(infbytes, 0, infbytes.Length); return infbytes; } } /// /// 移动文件 /// public static void MoveFiles(string oldPath, string newPath) { if (!Directory.Exists(oldPath)) return; if (!Directory.Exists(newPath)) Directory.CreateDirectory(newPath); string[] files = Directory.GetFiles(oldPath); foreach (string file in files) { string fileName = Path.GetFileName(file); string destinationPath = Path.Combine(newPath, fileName); File.Move(file, destinationPath); } } /// /// 删除目录 /// public static void DeleteDirectory(string directoryPath) { try { if (Directory.Exists(directoryPath)) { DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath); foreach (FileInfo file in directoryInfo.GetFiles()) { file.Attributes = FileAttributes.Normal; file.Delete(); } foreach (DirectoryInfo subdirectory in directoryInfo.GetDirectories()) { DeleteDirectory(subdirectory.FullName); } directoryInfo.Delete(); Debug.Log("目录删除成功:" + directoryPath); } } catch (Exception e) { Debug.Log(e.ToString()); throw; } } #endregion #region 延迟 public void Delay(float delay, Action action) { StartCoroutine(DelayToDo(delay, action)); } IEnumerator DelayToDo(float delay, Action action) { // 等待指定的延迟时间 yield return new WaitForSeconds(delay); action(); } #endregion }