zoukankan      html  css  js  c++  java
  • 下载某资源文件并加载其中的所有Prefab到场景中

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 
     5 /// <summary>
     6 /// LoadAssets打开某资源文件并加载其中的所有Prefab到场景中
     7 /// </summary>
     8 public class LoadAssets : MonoBehaviour
     9 {
    10     [HideInInspector]
    11     public string address;//资源所在文件夹路径
    12     public string fileToLoad;//资源文件名
    13     private string manifestAssetBundleName;//Manifest文件,一般与所在文件夹名称相同
    14     private AssetBundleManifest manifest;
    15     private List<AssetBundle> assetBundles = new List<AssetBundle>();
    16 
    17     private void Awake()
    18     {
    19         if(address.Equals(""))
    20             return;
    21         //Manifest文件的名称与生成的AssetBundle所在的文件夹同名
    22         string[] ss = address.Split('/');
    23         manifestAssetBundleName = ss[ss.Length - 2];
    24         LoadManifest(address + manifestAssetBundleName);
    25         LoadDepend(fileToLoad);
    26 
    27     }
    28     void OnGUI()
    29     {
    30         if (address.Equals(""))
    31             return;
    32         //第1步:读取Manifest
    33         if (GUILayout.Button("地图加载(选择map.unity3d)"))
    34         {
    35             StartCoroutine(LoadObj(address, fileToLoad));
    36             Caching.CleanCache();
    37         }
    38     }
    39     //同步下载并读取Manifest文件
    40     public void LoadManifest(string mainfestpath)
    41     {
    42         //Debug.Log("LoadManifest:"+mainfestpath);
    43         AssetBundle manifestBundle = AssetBundle.LoadFromFile(mainfestpath);
    44         if (manifestBundle != null)
    45         {
    46             manifest = (AssetBundleManifest)manifestBundle.LoadAsset("AssetBundleManifest");
    47         }
    48     }
    49     //获取资源所依赖的资源并下载
    50     void LoadDepend(string assetName)
    51     {
    52         string[] dps = manifest.GetAllDependencies(assetName);
    53         for (int i = 0; i < dps.Length; i++)
    54         {
    55             StartCoroutine(LoadAsset(address, dps[i]));
    56         }
    57     }
    58     //下载资源
    59     IEnumerator LoadAsset(string url, string fileName)
    60     {
    61         //Debug.Log("LoadAsset:" + url+" "+fileName);
    62         WWW www = WWW.LoadFromCacheOrDownload("file://" + url + fileName, manifest.GetAssetBundleHash(fileName));
    63         yield return www;
    64         assetBundles.Add(www.assetBundle);
    65         www.Dispose();
    66     }
    67     //下载并实例化游戏对象
    68     IEnumerator LoadObj(string url, string fileName)
    69     {
    70         //Debug.Log("LoadOBJ:" + url + " " + fileName);
    71 
    72         WWW www = WWW.LoadFromCacheOrDownload("file://" + url + fileName, manifest.GetAssetBundleHash(fileName));
    73         yield return www;
    74         string[] AssetsNames = www.assetBundle.GetAllAssetNames();
    75         foreach (string m in AssetsNames)
    76         {
    77             GameObject prefab = www.assetBundle.LoadAsset(m) as GameObject;
    78             GameObject obj = GameObject.Instantiate(prefab);
    79         }
    80 
    81         assetBundles.Add(www.assetBundle);
    82         www.Dispose();
    83         UnloadUnuseAssetBundle();
    84     }
    85     //清理未使用资源所占用的内存
    86     void UnloadUnuseAssetBundle()
    87     {
    88         foreach (var item in assetBundles)
    89         {
    90             item.Unload(false);
    91         }
    92     }
    93 }
  • 相关阅读:
    DS博客作业02--栈和队列
    DS博客作业02--线性表
    c博客06-2019-结构体&文件
    C博客作业05--2019-指针
    C语言博客作业04--数组
    C语言博客作业03--函数
    JAVA作业-.图书馆查书、借书、还书
    JAVA购物车
    5-互评-OO之接口-DAO模式代码阅读及应用
    DS博客作业05--查找
  • 原文地址:https://www.cnblogs.com/luxishi/p/6638645.html
Copyright © 2011-2022 走看看