美文网首页
Unity资源管理

Unity资源管理

作者: BugUnknown | 来源:发表于2018-08-25 17:18 被阅读25次

资源管理导图

资源管理.png

准备工作

存储等下需要加载的资源
  • 设置Asset Labels(Unity右下角)
    • Cube ------ cubeprefab
    • mat ------- cubemat
    • pic ------- cubetexture

待会加载资源就是靠标签加载的

一键打包的实现

using UnityEngine;
using UnityEditor;
using System.IO;

public class BuildAssetBundle : Editor
{
    [MenuItem("AssetBundle/Build/Windows")]
    public static void BuildWindows()
    {
        //AssetBundle的存储路径
        string path = Application.dataPath + "/AssetBundle/Windows/";
        //如果路径不存在
        if (!File.Exists(path))
        {
            //创建路径
            Directory.CreateDirectory(path);
        }
        //打包
        BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
        //打印
        Debug.Log("打包成功");
    }
}
一键打包效果图.gif

打包完成后删掉"CubeAsset"文件夹(其实删不删无所谓,主要是确认资源是从AssetBundle里面加载出来的)

AssetBundle文件夹的信息


AssetBundle.png

加载纹理图(无依赖关系)

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class LoadTexture : MonoBehaviour
{
    private string path;
    public string bundleName;
    public string assetName;

    private void Awake()
    {
        path = "file://" +Application.dataPath + "/AssetBundle/Windows/";
    }

    private IEnumerator Start()
    {
        WWW www = new WWW(path + bundleName);
        //下载
        yield return www;
        //获取AssetBundle
        AssetBundle ab = www.assetBundle;
        //加载资源
        Texture t = ab.LoadAsset(assetName) as Texture;
        //放图片
        GetComponent<RawImage>().texture = t;
        //释放资源
        ab.Unload(false);
    }
}

bundleName和assetNam根据一键打包的结果名称和原图片资源名称决定

加载图片效果图.gif

加载预设体(有依赖关系)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadCube : MonoBehaviour
{
    private string path;
    /// <summary>
    /// 资源的Bundle名称
    /// </summary>
    public string bundleName;
    /// <summary>
    /// 资源本身的名称
    /// </summary>
    public string assetName;
    /// <summary>
    /// 版本号
    /// </summary>
    public int version = 0;

    private void Awake()
    {
        path = "file://" + Application.dataPath + "/AssetBundle/Windows/";

    }

    private IEnumerator Start()
    {
        //下载整个资源管理系统的说明文件
        WWW www = WWW.LoadFromCacheOrDownload(path+"Windows",version);
        //下载
        yield return www;
        //获取到说明文件的Bundle
        AssetBundle mani = www.assetBundle;
        //加载Mani文件
        AssetBundleManifest manifest = mani.LoadAsset("AssetBundleManifest") as AssetBundleManifest;
        //查当前要加载的资源的所有依赖
        string[] deps = manifest.GetAllDependencies(bundleName);
        //释放ManiBundle
        mani.Unload(false);
        //声明依赖的Bundle数组
        AssetBundle[] depBundles = new AssetBundle[deps.Length];
        //依次下载依赖
        for (int i = 0; i < depBundles.Length; i++)
        {
            //下载依赖
            www = WWW.LoadFromCacheOrDownload(path + deps[i], version);
            //等待下载
            yield return www;
            //获取Bundle
            depBundles[i] = www.assetBundle;
        }
        //下载最终的资源bundle
        www = WWW.LoadFromCacheOrDownload(path + bundleName, version);
        //等待
        yield return www;
        //获取Bundle 
        AssetBundle realBundle = www.assetBundle;
        //获取资源
        GameObject prefab = realBundle.LoadAsset(assetName) as GameObject;
        //生成对象
        Instantiate(prefab);
        //释放Bundle
        realBundle.Unload(false);
        for (int i = 0; i < deps.Length; i++)
        {
            depBundles[i].Unload(false);
        }
    }
}
加载预设体效果图.gif

相关文章

网友评论

      本文标题:Unity资源管理

      本文链接:https://www.haomeiwen.com/subject/wydgiftx.html