/// <summary> /// 载入素材 /// </summary> public T LoadAsset <T>(string abname, string assetname) where T : UnityEngine.Object { abname = abname.ToLower(); #if UNITY_EDITOR && ASSET CsvItem item = null; if (CsvList.TryGetValue(abname, out item)) { var path = item.path + AppConst.dirSep + assetname + "." + item.ext; Util.Log(path); T temp = AssetDatabase.LoadAssetAtPath <T>(path); if (temp != null) { return(temp); } Util.Log("AssetDatabase.LoadAssetAtPath<T> is null"); } #endif AssetBundle bundle = LoadAssetBundle(abname); if (bundle == null) { Util.LogError("加载失败 : " + abname + " " + assetname); return(null); } return(bundle.LoadAsset <T>(assetname)); }
void Awake() { string path = Application.dataPath + "/InfoAssetBundles"; string[] files = Directory.GetFiles(path, "*.csv"); for (int i = 0; i < files.Length; i++) { string fileText = Util.GetFileText(files[i]); string[] lines = fileText.Split('\n'); for (int j = 0; j < lines.Length; j++) { string[] content = lines[j].Split(','); if (content.Length > 2) { string name = content[0].Substring(0, content[0].IndexOf(".unity3d")); CsvItem item = new CsvItem(); item.name = name; item.ext = content[1].Split('.')[1]; item.path = content[2].Trim(); CsvList.Add(name, item); } } } }
private Dictionary <string, Texture2D> LoadTexture(string abname) { if (mPureTexMap.ContainsKey(abname)) { return(mPureTexMap[abname]); } ResourceManager resMgr = facade.GetManager <ResourceManager>(ManagerName.Resource); Texture[] textures = null; #if UNITY_EDITOR && ASSET CsvItem item = null; if (resMgr.CsvList.TryGetValue(abname, out item)) { string[] paths = Directory.GetFiles(item.path, "*." + item.ext, SearchOption.AllDirectories); List <Texture> _sptList = new List <Texture>(); for (int i = 0; i < paths.Length; ++i) { _sptList.Add(AssetDatabase.LoadAssetAtPath <Texture>(paths[i])); } textures = _sptList.ToArray(); } #else AssetBundle ab = resMgr.LoadAssetBundle(abname); textures = ab.LoadAllAssets <Texture>(); #endif Dictionary <string, Texture2D> result = null; Texture2D texture; if (textures != null && textures.Length > 0) { result = new Dictionary <string, Texture2D>(); for (int i = 0; i < textures.Length; ++i) { texture = textures[i] as Texture2D; if (!result.ContainsKey(texture.name)) { result.Add(texture.name, texture); } } mPureTexMap.Add(abname, result); } return(result); }
/// <summary> /// 加载一个纹理集 /// </summary> /// <param name="abName"></param> /// <returns></returns> public Dictionary <string, Sprite> LoadAtlas(string abname) { abname = abname.ToLower(); if (mAtlasMap.ContainsKey(abname)) { return(mAtlasMap[abname]); } Dictionary <string, Sprite> sprDic = null; ResourceManager resMgr = facade.GetManager <ResourceManager>(ManagerName.Resource); Sprite[] sprs = null; #if UNITY_EDITOR && ASSET CsvItem item = null; if (resMgr.CsvList.TryGetValue(abname, out item)) { string[] paths = Directory.GetFiles(item.path, "*." + item.ext, SearchOption.AllDirectories); List <Sprite> _sptList = new List <Sprite>(); for (int i = 0; i < paths.Length; ++i) { _sptList.Add(AssetDatabase.LoadAssetAtPath <Sprite>(paths[i])); } sprs = _sptList.ToArray(); } #else AssetBundle ab = resMgr.LoadAssetBundle(abname); sprs = ab.LoadAllAssets <Sprite>(); #endif Texture2D tex2D = null; if (sprs != null && sprs.Length > 0) { sprDic = new Dictionary <string, Sprite>(); string tempStr = ""; int index = 0; Sprite spr; for (int i = 0; i < sprs.Length; ++i) { spr = sprs[i]; tex2D = spr.texture; tempStr = spr.name; index = tempStr.LastIndexOf('.'); if (index > 0) { tempStr = tempStr.Substring(0, index); } if (sprDic.ContainsKey(tempStr)) { Util.LogError("同名图片 : " + tempStr); continue; } sprDic.Add(tempStr, spr); } mAtlasMap.Add(abname, sprDic); if (tex2D != null) { mTexMap.Add(abname, tex2D); } } return(sprDic); }