ResourceManager(五)—— ResourceCommon
来源:互联网 发布:mcs51单片机是几位 编辑:程序博客网 时间:2024/05/16 12:23
目录为:Assets/Scripts/ResourceManager/ResourceCommon.cs
ResourceCommon.cs
//#define Resource_UseLogusing System;using System.IO;using System.Collections;using System.Collections.Generic;using UnityEngine;using Object = UnityEngine.Object;//这个类和BuildCommon很像public class ResourceCommon{ public static string textFilePath = Application.streamingAssetsPath; public static string assetbundleFilePath = Application.dataPath + "/assetbundles/"; public static string assetbundleFileSuffix = ".bytes"; public static string DEBUGTYPENAME = "Resource"; //public static string mServerAssetPath = @"http://192.168.123.1/assetbundles/"; //Server AssetPath public static string mServerAssetPath = "file:///c:/"; //封装了一些Debug.Log public static void Log(string fileName) { //#if Resource_UseLog Debug.Log (fileName, DEBUGTYPENAME, true); //#endif } //根据资源路径获取资源名称 public static string getResourceName(string resPathName) { int index = resPathName.LastIndexOf ("/"); if (index == -1) { return resPathName; } else { return resPathName.Substring (index + 1, resPathName.Length - index - 1); } } //这个挺奇怪的,去掉suffix然后返回 public static string getFileName(string fileName) { int index = fileName.IndexOf ("."); if (-1 == index) { throw new Exception ("can not find !!!"); } return fileName.Substring (0, index); } //返回单纯的fileName //例如Scenes/Scene1.unity.bytes //返回"Scene1.unity" public static string getFileName(string filePath, bool suffix) { if (!suffix) { string path = filePath.Replace ("\\", "/"); int index = path.LastIndexOf ("/"); if (-1 == index) { throw new Exception ("can not find !!!"); } int index2 = path.LastIndexOf ("."); if (-1 == index2) { throw new Exception ("can not find !!!"); } return path.Substring (index + 1, index2 - index - 1); } else { string path = filePath.Replace ("\\", "/"); int index = path.LastIndexOf ("/"); if (-1 == index) { throw new Exception ("can not find !!!"); } return path.Substring (index + 1, path.Length - index - 1); } } //返回文件夹名,但是传进来的必须是长目录的文件夹路径 public static string getFolder(string path) { path = path.Replace ("\\", "/"); int index = path.LastIndexOf ("/"); if (-1 == index) { throw new Exception ("can not find !!!"); } return path.Substring (index + 1, path.Length - index - 1); } //返回后缀 public static string getFileSuffix(string filePath) { int index = filePath.LastIndexOf ("."); if (-1 == index) { throw new Exception ("can not find Suffix !!! the filePath is : " + filePath); } return filePath.Substring (index + 1, filePath.Length - index - 1); } //获取所给目录下的所有文件,recursion表示是否递归 public static void getFiles(string path, bool recursion, Dictionary<string, List<string>> allFiles, bool useSuffix, string suffix) { if (recursion) { string[] dirs = Directory.GetDirectories (path); foreach (string dir in dirs) { if (getFolder(dir) == ".svn") { continue; } getFiles (dir, recursion, allFiles, useSuffix, suffix); } } string[] files = Directory.GetFiles (path); foreach (string file in files) { string fileSuffix = getFileSuffix (file); if (fileSuffix == "meta" || fileSuffix == "dll") { continue; } if (useSuffix && fileSuffix != suffix) { continue; } string relativePath = file.Replace ("\\", "/"); relativePath = relativePath.Replace (Application.dataPath, ""); string fileName = getFileName (file, true); if (allFiles.ContainsKey(fileName)) { allFiles [fileName].Add (relativePath); } else { List<string> list = new List<string> (); list.Add (relativePath); allFiles.Add (fileName, list); } } } public static void CheckFolder(string path) { if (!Directory.Exists(path)) { Directory.CreateDirectory (path); } } //除去了文件名 public static string getPath(string filePath) { string path = filePath.Replace ("\\", "/"); int index = path.LastIndexOf ("/"); if (-1 == index) { throw new Exception ("can not find !!!"); } return path.Substring (0, index); } public static string getLocalPath(string path) { string localPath = string.Format ("{0}/{1}", Application.persistentDataPath, path); if (!File.Exists(localPath)) { if (Application.platform == RuntimePlatform.Android) { localPath = string.Format ("{0}/{1}", Application.streamingAssetsPath, path); } else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer) { localPath = string.Format ("file://{0}/{1}", Application.streamingAssetsPath, path); } return localPath; } return "file:///" + localPath; } //Server Path public static string getServerPath(string path) { return string.Format ("{0}/{1}", mServerAssetPath, path); } //以bytes形式返回AssetBundle资源 public static byte[] getAssetBundleFileBytes(string path, ref int size) { string localPath; //Android和IOS环境使用的沙箱目录 if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) { localPath = string.Format ("{0}/{1}", Application.persistentDataPath, path + ResourceCommon.assetbundleFileSuffix); } //Windows下使用assetbundle资源目录 else { localPath = ResourceCommon.assetbundleFilePath + path + ResourceCommon.assetbundleFileSuffix; } //首先检测沙箱目录中是否有更新资源 if (File.Exists(localPath)) { try { FileStream bundleFile = File.Open(localPath, FileMode.Open, FileAccess.Read); byte[] bytes = new byte[bundleFile.Length]; bundleFile.Read(bytes, 0, (int)bundleFile.Length); size = (int)bundleFile.Length; bundleFile.Close(); return bytes; } catch (Exception e) { Debug.LogError (e.Message, "CreateAssetBundle", true); return null; } } //原始包中 else { TextAsset bundleFile = Resources.Load (path) as TextAsset; if (null == bundleFile) { Debug.LogError ("load : " + path + " bundleFile error!!!", "CreateAssetBundle", true); } size = bundleFile.bytes.Length; return bundleFile.bytes; } } //------------------------------------------------------------------------------------- public delegate void Handle_CreateFromMemory(AssetBundleCreateRequest request, int size); public static IEnumerator _CreateFromMemory(string path, Handle_CreateFromMemory handle) { int size = 0; AssetBundleCreateRequest bundleRequest = AssetBundle.CreateFromMemory (ResourceCommon.getAssetBundleFileBytes (path, ref size)); yield return bundleRequest; handle (bundleRequest, size); } public delegate void HandleFinishLoadAsyncFromAssetBundle(AssetBundleRequest request); public static IEnumerator LoadAsyncFromAssetBundle(AssetBundle assetbundle, string name, System.Type type, HandleFinishLoadAsyncFromAssetBundle handle) { AssetBundleRequest request = assetbundle.LoadAsync (name, type); yield return request; handle (request); } //------------------------------------------------------------------------------------------------- //仅用于win_editor //用于Debug public static AssetBundle loadAssetBundleImmediateForDebug(string path) { string localPath = string.Format ("file://{0}/{1}", assetbundleFilePath, path + ResourceCommon.assetbundleFileSuffix); WWW www = new WWW (localPath); if (!string.IsNullOrEmpty(www.error)) { Debug.LogError ("the assetbundle path is error: " + path + "\nand the localPath is : " + localPath); } while (true) { byte[] b = www.bytes; if (www.isDone) { break; } } return www.assetBundle; }}
阅读全文
0 0
- ResourceManager(五)—— ResourceCommon
- ResourceManager(一)—— ArchiveManager
- ResourceManager(二)—— AssetInfoManager
- ResourceManager(三)—— Request,ResourceAsyncOperation
- ResourceManager(四)—— ResourceUnit
- ResourceManager(六)—— ResourcesManager
- 研磨Hadoop源码(五)ResourceManager启动分析1
- 研磨Hadoop源码(五)ResourceManager启动分析1
- ResourceManager(七)—— 打包AssetBundle然后从AssetBundle加载资源
- ResourceManager(八)—— 在安卓机子上实现加载AssetBundle
- ResourceManager
- ResourceManager
- 【Hadoop】Hadoop官方文档翻译—— YARN ResourceManager High Availability 2.7.3
- Hadoop官方文档翻译—— YARN ResourceManager High Availability 2.7.3
- FLEX实践—在MXML与AS中分别引用resourceManager
- 使用 ResourceManager 类检索资源(来自MSDN)
- 研磨Hadoop源码(六)ResourceManager启动分析2
- 研磨Hadoop源码(六)ResourceManager启动分析2
- 服务器刚部署好IIS,网站不能访问
- C++学习18:#include的两种形式
- FZU
- 带锁的门
- 【BZOJ1924】【SDOI2010】所驼门王的宝藏
- ResourceManager(五)—— ResourceCommon
- js
- Java泛型
- SQL语句之查询基础(二)
- TensorFlow入门
- Java中Arrays类的两个方法:deepEquals和equals
- quartz集群分布式(并发)部署解决方案-Spring
- SQL语句之高级查询
- 网易2018校园招聘编程题真题集合