Unity中的资源资源的处理种类大致分为:Resources、StreamingAssets、AssetBundle Resources是作为一个Unity的保留文件夹出现的,也就是如果你新建的文件夹的名字叫Resources,那么里面的内容在打包时都会被无条件的打到发布包中。 特点:
StreamingAssets 特点: 只读不可写。 AssetBundleAssetBundle就是把prefab或者二进制文件封装成AssetBundle文件。 特点:
Resources首先我们新建一个Resources目录,并且并将资源放在这目录中。 1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.UI; 4 5 public class LoadResources : MonoBehaviour { 6 7 public Image image; 8 9 // Use this for initialization 10 void Start () { 11 12 image.overrideSprite = Resources.Load ("animotiong_2", typeof(Sprite)) as Sprite; 13 14 } 15 16 }Resources StreamingAssets首先我们新建一个StreamingAssets目录,并且并将资源放在这目录中。 1 using UnityEngine; 2 using System.Collections; 3 4 public class LoadResources : MonoBehaviour { 5 6 string _result; 7 8 9 // Use this for initialization 10 void Start () { 11 12 StartCoroutine(LoadXML()); 13 14 } 15 16 IEnumerator LoadXML() { 17 string sPath= Application.streamingAssetsPath "/test.xml"; 18 WWW www = new WWW(sPath); 19 yield return www; 20 _result = www.text; 21 } 22 }streamingAssetsPath 读取配置表 1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using System.IO; 5 namespace Commom 6 { 7 ///<summary> 8 ///资源加载管理类 提供Resources加载资源相关的功能 9 ///</summary> 10 public class ResourcesManager 11 { 12 private static Dictionary<string, string> resMap; 13 14 //1.获取配置文件 形成数据结构Dictionary<string,string> 15 private static string GetConfigFile() 16 { 17 string configPath = Application. 18 streamingAssetsPath "/ResConfig.txt"; 19 //当前路径只适应于安卓平台 20 if (Application.platform != RuntimePlatform.Android) 21 configPath = "file://" configPath; 22 23 WWW www = new WWW(configPath); 24 //有可能文件比较大 读完 25 while (true) 26 { 27 if (www.isDone) 28 { 29 return www.text; 30 } 31 } 32 } 33 //2形成数据结构Dictionary<string,string> 34 public static void BuildMap() 35 { 36 string strConfig= GetConfigFile(); 37 //获取配置文件 38 resMap = new Dictionary<string, string>(); 39 //字符串读取器 40 StringReader reader= new StringReader(strConfig); 41 42 //一行一行读 43 string line = null; 44 while ((line=reader.ReadLine())!=null) 45 { 46 string[] keyvalue = line.Split('='); 47 resMap.Add(keyvalue[0], keyvalue[1]); 48 } 49 //foreach (var item in resMap.Keys) 50 //{ 51 // Debug.Log(resMap[item]); 52 //} 53 54 } 55 static ResourcesManager() 56 { 57 BuildMap(); 58 } 59 //3.根据资源名称 获取完整路径.加载资源 60 public static T Load<T>(string resName) where T : Object 61 { 62 //根据资源名称 查找路径 63 string path = resMap[resName]; 64 //加载资源 65 return Resources.Load<T>(path); 66 } 67 } 68 }ResourcesManager WWW 加载资源 从网络网址加载资源 1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 6 public class WWWTest : MonoBehaviour 7 { 8 string url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1551776540985&di=fe944512c8b2d0e80313bdc488acc4ac&imgtype=0&src=http://image13./uploadfile/s2010/0127/20100127083234166.jpg"; 9 10 private void Start() 11 { 12 StartCoroutine(WWW()); 13 } 14 private IEnumerator WWW() 15 { 16 WWW www = new WWW(url); 17 yield return www; // 等待直至异步下载完成,才继续往下执行 18 if (!string.IsNullOrEmpty(www.error)) //检测是否www.有错误 如果不为空则终止 19 { 20 yield break; 21 } 22 if (www.isDone) //检测是否加载完成 23 { 24 transform.Find("RawImage").GetComponent<RawImage>().texture = www.texture; 25 } 26 } 27 } |
|