zoukankan      html  css  js  c++  java
  • unity 加载资源

    Unity3D中的资源的处理种类

    Unity中的资源资源的处理种类大致分为:Resources、StreamingAssets、AssetBundle

    Resources

    是作为一个Unity的保留文件夹出现的,也就是如果你新建的文件夹的名字叫Resources,那么里面的内容在打包时都会被无条件的打到发布包中。

    特点:

    1. 只读,即不能动态修改。所以想要动态更新的资源不要放在这里。
    2. 会将文件夹内的资源打包集成到.asset文件里面。因此建议可以放一些Prefab,因为Prefab在打包时会自动过滤掉不需要的资源,有利于减小资源包的大小。
    3. 资源读取使用Resources.Load()。

    StreamingAssets
    StreamingAssets和Resources很像。同样作为一个只读的Unity3D的保留文件夹出现。不过两者也有很大的区别,那就是Resources文件夹中的内容在打包时会被压缩和加密。而StreamingAsset文件夹中的内容则会原封不动的打入包中,因此StreamingAssets主要用来存放一些二进制文件。

    特点:

    只读不可写。
    主要用来存放二进制文件。
    只能用过WWW类来读取。

    AssetBundle

    AssetBundle就是把prefab或者二进制文件封装成AssetBundle文件。

    特点:

    1. 是Unity3D定义的一种二进制类型。
    2. 使用WWW类来下载。

    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%3A%2F%2Fimage13.m1905.cn%2Fuploadfile%2Fs2010%2F0127%2F20100127083234166.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 }
    WWWTest
  • 相关阅读:
    idea html,js修改不用重启进程
    opencv rtsp 人脸识别
    The system is running in low-graphics mode UB16
    阿里云ecs 增加虚拟网卡
    rtsp
    mysql5.7报err 1055错误 sql_mode=only_full_group_by
    python 生成requirements.txt
    Linux 保护文件 不给修改
    logback logback.xml常用配置详解(三) <filter>
    logback 常用配置详解(二) <appender>
  • 原文地址:https://www.cnblogs.com/ASsss/p/10477261.html
Copyright © 2011-2022 走看看