美文网首页unity
Unity 资源管理与AssetBundle详解

Unity 资源管理与AssetBundle详解

作者: 柚梨柚梨 | 来源:发表于2018-03-22 08:05 被阅读34次

    什么是资源(Asset)

    http://blog.csdn.net/u014230923/article/details/51433455
    Unity常用的资源大概有3类:

    1. 纯资源(material,texture,shader,audio ……)这些资源不能直接拖到场景里使用。
    2. 预置(prefab),这种资源需要实例化instantiate之后才能使用
    3. scene也是一种资源

    还有一些平时不太关注的:脚本对象,文本文件,unity自己内置的资源(像新建粒子时的默认材质之类的),这些也是资源。

    资源管理

    http://blog.csdn.net/qq_18995513/article/details/51955609
    Unity的资源管理模式,包括在编辑器管理(使用AssetDatabase)和在运行时管理(使用Resources和AssetBundle)。

    1. 编辑器管理: AssetDatabase

    在编辑器内加载卸载资源,并不能在游戏发布时使用,它只能在编辑器内使用。但是,它加载速度快,效率高,适合在测试时使用

    因为只能在编辑器使用,要加个宏防止报错:

    #if UNITY_EDITOR
        using UnityEditor;
    #endif
    

    加载(Load) Asset

    #if UNITY_EDITOR
                assetInfo.asset = UnityEditor.AssetDatabase.LoadAssetAtPath(BundleUtils.RealPath(assetPath), type);
                assetInfo.assetName = assetPath;
                assetInfo.type = type;
                assets[assetPath] = assetInfo;
    #else
    

    卸载(Unload) Asset

    https://docs.unity3d.com/ScriptReference/Resources.UnloadAsset.html

     Resources.UnloadAsset(asset);
    

    Unloads assetToUnload from memory.

    This function can only be called on Assets that are stored on disk.

    If there are any references from game objects in the scene to the asset and it is being used then Unity will reload the asset from disk as soon as it is accessed.

    需要注意的是,调用Resources.UnloadAsset()来清理资源时,只是标记该资源需要被GC回收,但不是立刻就被回收的。需要调用

    System.GC.Collect();
    

    使用脚本创建Asset

    // 创建资源
    public class TestCreateAsset : MonoBehaviour {
    
        void Start () {
    #if UNITY_EDITOR
            // 加载资源
            Material mat = AssetDatabase.LoadAssetAtPath("Assets/Materials/New Material.mat", typeof(Material)) as Material;
            // 以mat为模板创建mat2
            Material mat2 = Object.Instantiate<Material>(mat);
            // 创建资源
            AssetDatabase.CreateAsset(mat2, "Assets/Materials/1.mat");
            // 刷新编辑器,使刚创建的资源立刻被导入,才能接下来立刻使用上该资源
            AssetDatabase.Refresh();
            // 使用资源
            Debug.Log(mat2.name);
    #endif
        }
    }
    

    运行脚本后,会发现在编辑器里就会出现:Project视窗中多了一个刚创建的1.mat资源。

    修改Asset

    具体见上面链接

    2. 在运行时管理:使用Resources[1]

    Resources.Load就是从一个缺省打进程序包里的AssetBundle里加载资源,而一般AssetBundle文件需要你自己创建,运行时 动态加载,可以指定路径和来源的。

    3. 在运行时管理:使用AssetBundle[2]

    存储结构存储结构

    注意下图有一些错误需要更新:

    • 这张图中的CreateFromFile函数在5.6.0中被去掉了,LoadFromFile是一样的
    • AssetBundle并不一定是整个被解压缩到内存里,根据Unity官方文档解释,只有LZMA格式压缩的AssetBundle会被解压到内存,而未压缩/LZ4压缩的Assetbundle是直接从磁盘读取的。

      The function supports bundles of any compression type. In case of lzma compression, the data will be decompressed to the memory. Uncompressed and chunk-compressed bundles can be read directly from disk. [3]

    AssetBundle的内存占用AssetBundle的内存占用 [4]

    各种加载和初始化:

    • AssetBundle.LoadFromFile
      创建一个AssetBundle内存镜像。注意同一个assetBundle文件在没有Unload之前不能再次被使用

    • WWW.AssetBundle
      同上。当然要先new一个再 yield return 然后才能使用

    • AssetBundle.Load(name)
      从AssetBundle读取一个指定名称的Asset并生成Asset内存对象。如果多次Load同名对象,除第一次外都只会返回已经生成的Asset对象,也就是说多次Load一个Asset并不会生成多个副本(singleton)

    • Resources.Load(string path, Type systemTypeInstance)
      同上。只是Resources文件夹加载. e.g. rend.material.mainTexture = Resources.Load("glass") as Texture;

    • 一个比较特殊的AssetBundle: 生成AssetBundle的时候会生成一些Manifest,根目录下还会有一个与根目录同名的AssetBundle以及Manifest文件,通过运行时加载这个AssetBundle,可以得到一个AssetBundleManifest对象,然后就可以通过这个对象得到AssetBundle直接的依赖关系。

      • AssetBundleManifest存储的是依赖文件列表。我们得知道AssetBundle们在哪儿,所以获得根目录下根目录同名的AssetBundleManifest是将其它AssetBundle从硬盘load到内存的第一步。具体可以进行如下操作
      • AssetBundle manifestBundle = AssetBundle.CreateFromFile(Application.dataPath +"/ab/Assetbundle");
        AssetBundleManifest manifest = manifestBundle.LoadAsset("AssetBundleManifest") as AssetBundleManifest;
      • string[] bundleList = bundleManifest.GetAllAssetBundles(); 可以获得所有bundle
      • string[] dependFiles = bundleManifest.GetAllDependencies(bundleName); 可以获得依赖文件列表
    • Instantiate(Object original)
      • 注意: texture之类的是不用instantiate的,只要把需要贴图的位置指向它就可以了, e.g.
        obj1.renderer.material.mainTexture = AssetBundle1.Load("wall") as Texture;

      • 对于prefab,只有实例化才能使用。因此需要将从AssetBundle load出来的Prefab进行instantiate才能使用。

      • 所有prefab实例都是prefab的克隆,所以运行时生成对象会有Clone标记。

      • 这个函数的clone的是这个object的完整结构,包括其所有Component和子物体(详见以下附的官方文档),浅Copy,并不复制所有引用类型

        When you call Instantiate() on anything that exists in a Scene, such as a Prefab or a GameObject, Unity serializes the object. This happens both at runtime and in the Editor. Note that everything that derives from UnityEngine.Object can be serialized.

        Unity then creates a new GameObject and deserializes (that is, loads) the data onto the new GameObject. Next, Unity runs the same serialization code in a different variant to report which other UnityEngine.Objects are being referenced. It checks all referenced UnityEngine.Objects to see if they are part of the data being Instantiated(). If the reference points to something “external”, such as a Texture, Unity keeps that reference as it is. If the reference points to something “internal”, such as a child GameObject, Unity patches the reference to the corresponding copy.

    各种释放

    • 释放对象

      • 自动释放:
        • 在进入新场景(scene/level)的时候会自动销毁所有场景内对象,然后加载新场景里的对象[5]
        • 但是,可以通过DontDestroyOnLoad(object)使得某个对象不被自动销毁。如果这个对象是component或者game object,那么它的整个transform hierarchy都不会被销毁。
      • 手动释放:
        • Destroy()[6]可用于销毁gameobject, component或asset。销毁object会把整个gameobject销毁,包括它的所有component和transform children。销毁component会把这个component从gameobject上去掉(所以,此时component没有从内存中去掉,不过如果没有任何引用关系了,可以通过下面提到的Resources.UnloadUnusedAssets()释放)。
        • Destroy()后内存的回收会delay到当前update结束后执行。如果要立即执行,需要使用GC.Collect()强制垃圾收集器立即释放内存
        • Destroy()如果用于销毁从文件加载的Asset对象会销毁相应的资源文件!但是如果销毁的Asset是Copy的或者用脚本动态生成的,只会销毁内存对象。[2]
        • Resources.UnloadAsset(Object): 显式的释放已加载的Asset对象,只能卸载磁盘文件加载的Asset对象
        • Resources.UnloadUnusedAssets(): 用于释放所有没有引用的Asset对象
    • 释放AssetBundle

      • AssetBundle.Unload(false): 释放AssetBundle文件内存镜像
      • AssetBundle.Unload(true): 释放AssetBundle文件内存镜像同时销毁所有已经Load的Assets内存对象

    1. Unity官方文档 Resources: https://docs.unity3d.com/540/Documentation/ScriptReference/Resources.html

    2. Reference: http://www.cnblogs.com/88999660/archive/2013/03/15/2961663.html

    3. https://docs.unity3d.com/ScriptReference/AssetBundle.LoadFromFile.html

    4. http://blog.csdn.net/u011926026/article/details/70155178

    5. Unity官方文档 Object.DontDestroyOnLoad: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

    6. Unity官方文档 Destroy: https://docs.unity3d.com/ScriptReference/Object.Destroy.html

    相关文章

      网友评论

        本文标题:Unity 资源管理与AssetBundle详解

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