using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using UnityEngine; namespace Wing.Utils { public class UtilsHelper { public static string GetDataPath() { string path = ""; if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) { path = Application.persistentDataPath + "/"; } else if (Application.platform == RuntimePlatform.WindowsPlayer) { path = Application.dataPath + "/"; } else if (Application.platform == RuntimePlatform.WindowsEditor) { path = Application.dataPath + "/../"; } else { path = Application.persistentDataPath + "/"; } return path; } public static string GetResourcePath() { return GetDataPath() + "ResData"; } public static IEnumerator LoadTexture(string url, Action cb) { //这里的url可以是web路径也可以是本地路径file:// WWW www = new WWW(url); //挂起程序段,等资源下载完成后,继续执行下去 yield return www; //判断是否有错误产生 if (string.IsNullOrEmpty(www.error)) { //把下载好的图片回调给调用者 cb.Invoke(www.texture); //释放资源 www.Dispose(); } } public static IEnumerator Load(string url, Action cb) { //这里的url可以是web路径也可以是本地路径file:// WWW www = new WWW(url); //挂起程序段,等资源下载完成后,继续执行下去 yield return www; //判断是否有错误产生 if (string.IsNullOrEmpty(www.error)) { //把下载好的图片回调给调用者 cb.Invoke(www); //释放资源 www.Dispose(); } } public static IEnumerator Load(string url, Action cb, object data) { //这里的url可以是web路径也可以是本地路径file:// WWW www = new WWW(url); //挂起程序段,等资源下载完成后,继续执行下去 yield return www; //判断是否有错误产生 if (string.IsNullOrEmpty(www.error)) { //把下载好的图片回调给调用者 cb.Invoke(www, data); //释放资源 www.Dispose(); } } public static IEnumerator LoadAssetBundle(string url, Action ab) { WWW www = new WWW(url); yield return www; if (string.IsNullOrEmpty(www.error)) { ab.Invoke(www.assetBundle); www.Dispose(); } } public static string StringMD5(string data) { byte[] result = Encoding.Default.GetBytes(data.Trim()); MD5 md5 = new MD5CryptoServiceProvider(); byte[] output = md5.ComputeHash(result); return BitConverter.ToString(output).Replace("-", ""); } public static void SaveTextureFile(Texture2D incomingTexture, string filename) { byte[] bytes = incomingTexture.EncodeToPNG(); string dir = Path.GetDirectoryName(filename); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } File.WriteAllBytes(filename, bytes); } public static bool SaveRenderTextureToPNG(Texture inputTex, Material mat, string filename) { RenderTexture temp = RenderTexture.GetTemporary(inputTex.width, inputTex.height, 0, RenderTextureFormat.ARGB32); Graphics.Blit(inputTex, temp, mat); bool ret = SaveRenderTextureToPNG(temp, filename); RenderTexture.ReleaseTemporary(temp); return ret; } public static bool SaveRenderTextureToPNG(RenderTexture rt, string filename) { Texture2D png = CreateTexture2DFromRT(rt); SaveTextureFile(png, filename); Texture2D.DestroyImmediate(png); png = null; return true; } public static Texture2D CreateTexture2DFromRT(RenderTexture rt, Rect? rect = null) { RenderTexture prev = RenderTexture.active; RenderTexture.active = rt; if(rect == null) { rect = new Rect(0, 0, rt.width, rt.height); } else { float width = rect.Value.width; if(rect.Value.width + rect.Value.x > rt.width) { width = rt.width - rect.Value.x; } float height = rect.Value.height; if(rect.Value.height + rect.Value.y > rt.height) { height = rt.height - rect.Value.y; } rect = new Rect(rect.Value.x, rect.Value.y, width, height); } Texture2D texture = new Texture2D((int)rect.Value.width, (int)rect.Value.height, TextureFormat.ARGB32, false); texture.ReadPixels(rect.Value, 0 , 0); texture.Apply(); RenderTexture.active = prev; return texture; } } }