using System.Collections.Generic; using UnityEngine; using System.IO; using System.Threading.Tasks; using System.Threading; using UnityEngine.Networking; using System.Collections; using UnityEngine.Events; /// /// 与Editor/GenerateAssetsConfig 配合使用 快速获取资源整路径 /// namespace XFramework { public class ResourcesManager { static ResourcesManager() { //txt>dic Load(); } //记录预制件名称以及其对应的预制件路径 static private Dictionary dic = new Dictionary(); static private void Load() { //加载配置文件,文本类型 TextAsset textAsset = Resources. Load("ResMap"); if (textAsset == null) { Debug.LogError("未生成ResMap,确定不使用RM功能时可忽略"); return; } string mapText = textAsset.text; //读配置文件 对文本逐行读取 StringReader reader = new StringReader(mapText); string line = null; while ((line = reader.ReadLine()) != null) { string[] keyValue = line.Split('='); dic.Add(keyValue[0], keyValue[1]); } } //需要调用的方法 key value static private string GetValue(string key) { if (dic.ContainsKey(key)) { return dic[key]; } Debug.LogError($"ResMap中不存在{key}"); return null; } ////路径》得到对象 【预制件】 path=BaseMeleeAttackSkill //public static async Task Load(string path) //{ // //Skill/BaseMeleeAttackSkill // ResourceRequest request = Resources.LoadAsync(GetValue(path)); // await request; // return request.asset; //} public static T Load(string resName) where T :Object { return Resources.Load(GetValue(resName)); } //获取资源完整路径 public static string GetPath(string prefabName) { return GetValue(prefabName); } } public class StreamingAssetsManager { static string streamingAssetaPath; //移动平台待测试 static StreamingAssetsManager() { #if UNITY_STANDALONE_WIN || UNITY_EDITOR streamingAssetaPath = Application.streamingAssetsPath; #elif UNITY_ANDROID || UNITY_IPHONE streamingAssetaPath = Application.persistentDataPath; #endif //txt>dic Load(); } //记录预制件名称以及其对应的预制件路径 static private Dictionary dic = new Dictionary(); //加载本地配置文件 static private void Load() { //加载配置文件,文本类型 string mapText = File.ReadAllText(streamingAssetaPath + "/SAMap.txt"); //读配置文件 对文本逐行读取 StringReader reader = new StringReader(mapText); string line = null; while ((line = reader.ReadLine()) != null) { string[] keyValue = line.Split('='); dic.Add(keyValue[0], keyValue[1]); } } /// /// 获取完整路径 /// /// /// public static string GetPath(string assetsName) { if (dic.ContainsKey(assetsName)) { return $"{streamingAssetaPath}/{dic[assetsName]}"; } else { Debug.LogWarning($"SAMap中不存在{assetsName}"); return null; } } public IEnumerator Load(string url,UnityAction loaded) where T: Object { UnityWebRequest webRequest = UnityWebRequest.Get(url); yield return webRequest.SendWebRequest(); if (webRequest.isDone&& webRequest.error == null) { //loaded.Invoke(webRequest.downloadHandler.) } } } }