Unity中资源动态加载的几种方式比较

初学Unity的过程中,会发现打包发布程序后,unity会自动将场景需要引用到的资源打包到安装包里,没有到的不会跟进去。我们在编辑器里看到的Asset中的文件结构只是工作于编辑器环境下的,在游戏中unity会重新组织数据库。这是我们一定会遇到一个需求,即动态的加载我们自己的文件,而且想维护这个文件存储和加载的位置,并且是各种自定义的文件。

比如说你换装,需要动态从磁盘load一个模型,正常情况下如果这个模型没有被场景引用到,它都根本不会被打进安装包的。再比如说我想存储一个自己定义的配置的文件,想把它放在磁盘的某个位置,动态的读,这些需求就要动态的加载资源。



1.首先总结下unity有哪几种资源类型

unity资源的类型:

  -a) Unity内置的常用asset,                   fbx\jpg...


  - b) textasset:                                         txt、binary等,对应了它的TextAsset类,可以直接读入文本或者二进制byte


   -c) scriptable object                             它是序列化的Object实例,例如把一个Object实例保存成scriptable object,读入进来后就直接在内存中变成那个实例


   - d) asset bundle                                  它是一个资源压缩包,里面包含了一堆资源


通常我们自定义的文件类型可以通过textasset 或scriptable object 来存储,区别在于前者是一个字节或文本流,后者要对应于程序中一个定义了的类型,textasset 还通常用于资源的加密。


2.动态load资源的几种途径:

   -通过Resources模块,调用它的load函数:可以直接load并返回某个类型的Object,前提是要把这个资源放在Resource命名的文件夹下,Unity不关有没有场景引用,都会将其全部打入到安装包中。

        -通过bundle的形式:即将资源打成 asset bundle 放在服务器或本地磁盘,然后使用WWW模块get 下来,然后从这个bundle中load某个object。

        -通过AssetDatabase.loadasset :这种方式只在editor范围内有效,游戏运行时没有这个函数,它通常是在开发中调试用的



       Resources的方式需要把所有资源全部打入安装包,这对游戏的分包发布(微端)和版本升级(patch)是不利的,所以unity推荐的方式是不用它,都用bundle的方式替代,把资源达成几个小的bundle,用哪个就load哪个,这样还能分包发布和patch,但是在开发过程中,不可能没更新一个资源就打一次bundle,所以editor环境下可以使用AssetDatabase来模拟,这通常需要我们封装一个dynamic resource的loader模块,在不同的环境下做不同实现。

public class AssetBuilder
{
    [MenuItem("Assets/Create SceneConfig Asset", false, -20)]
    public static void MakeScene()
    {
        SGGame.SceneConfig config = new SGGame.SceneConfig();
        AssetDatabase.CreateAsset(config, "Assets/../StreamingAssets/Scene.asset");
    }

    [MenuItem("Assets/Create CreditsConfig Asset", false, -20)]
    public static void MakeCreditsScene()
    {
        SGGame.CreditsConfig config = new SGGame.CreditsConfig();
        AssetDatabase.CreateAsset(config, "Assets/Resources/Config/CreditsConfig.asset");
    }

    [MenuItem("Assets/Create TestConfig Asset", false, -20)]
    public static void MakeTestScene()
    {
        SGGame.TestConfig config = new SGGame.TestConfig();
        AssetDatabase.CreateAsset(config, "Assets/Resources/Config/TestConfig.asset");
    }

    [MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking", false, -20)]
    public static void BuildWithoutDependencies()
    {
        if (Selection.activeObject == null)
        {
            EditorUtility.DisplayDialog("", "Please select an object to bundle before running the Build AssetBundle command.", "Ok", "");
            return;
        }

        string selectionName = Selection.activeObject.name;
        string path = EditorUtility.SaveFilePanel("Save Resource", "", selectionName, "unity3d");
        if (path.Length > 0)
        {
            // Build the resource file from the active selection.
            if (Application.platform == RuntimePlatform.WindowsEditor)
                BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
            else if (Application.platform == RuntimePlatform.OSXEditor)
                BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.iPhone);
        }
    }

    [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies", false, -20)]
    public static void BuildTrackDependencies()
    {
        if (Selection.activeObject == null)
        {
            EditorUtility.DisplayDialog("", "Please select an object to bundle before running the Build AssetBundle command.", "Ok", "");
            return;
        }

        string selectionName = Selection.activeObject.name;
        string path = EditorUtility.SaveFilePanel("Save Resource", "", selectionName, "assetbundle");
        if (path.Length > 0)
        {
            Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            // ToDo: The selection currently includes everything.
            //       A good optimization would be to filter this array so that it includes only what we need it to include.
            if (Application.platform == RuntimePlatform.WindowsEditor)
                BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
            else if (Application.platform == RuntimePlatform.OSXEditor)
                BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.iPhone);

            Selection.objects = selection;
        }
    }

    [MenuItem("Assets/[Windows_Editor]Build AssetBundle From Selection - Track dependencies", false, -20)]
    public static void BuildWindowsTrackDependencies()
    {
        if (Selection.activeObject == null)
        {
            EditorUtility.DisplayDialog("", "Please select an object to bundle before running the Build AssetBundle command.", "Ok", "");
            return;
        }
        Object[] SelectionObjects = Selection.objects;
        foreach (Object activeObject in SelectionObjects)
        {
            string selectionName = activeObject.name;
            string path = EditorUtility.SaveFilePanel("Save Resource", "", selectionName, "assetbundle");
            if (path.Length > 0)
            {
                Selection.activeObject = activeObject;
                Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
                foreach (Object selObj in selection)
                {
                    Debug.Log(selObj.name);
                }
                // ToDo: The selection currently includes everything.
                //       A good optimization would be to filter this array so that it includes only what we need it to include.         
                BuildPipeline.BuildAssetBundle(activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
            }
        }
    }

    [MenuItem("Assets/[Android]Build AssetBundle From Selection - Track dependencies", false, -20)]
    public static void BuildAndroidTrackDependencies()
    {
        if (Selection.activeObject == null)
        {
            EditorUtility.DisplayDialog("", "Please select an object to bundle before running the Build AssetBundle command.", "Ok", "");
            return;
        }
        Object[] SelectionObjects = Selection.objects;
        foreach (Object activeObject in SelectionObjects)
        {
            string selectionName = activeObject.name;
            string path = EditorUtility.SaveFilePanel("Save Resource", "", selectionName, "assetbundle");
            if (path.Length > 0)
            {
                Selection.activeObject = activeObject;
                Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);                
                foreach (Object selObj in selection)
                {
                    Debug.Log(selObj.name);
                }
                // ToDo: The selection currently includes everything.
                //       A good optimization would be to filter this array so that it includes only what we need it to include.         
                BuildPipeline.BuildAssetBundle(activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.Android);                
            }
        }
    }

    [MenuItem("Assets/[iPhone]Build AssetBundle From Selection - Track dependencies", false, -20)]
    public static void BuildIPhoneTrackDependencies()
    {
        if (Selection.activeObject == null)
        {
            EditorUtility.DisplayDialog("", "Please select an object to bundle before running the Build AssetBundle command.", "Ok", "");
            return;
        }
        Object[] SelectionObjects = Selection.objects;
        foreach (Object activeObject in SelectionObjects)
        {
            string selectionName = activeObject.name;
            string path = EditorUtility.SaveFilePanel("Save Resource", "", selectionName, "assetbundle");
            if (path.Length > 0)
            {
                Selection.activeObject = activeObject;
                Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
                foreach (Object selObj in selection)
                {
                    Debug.Log(selObj.name);
                }
                // ToDo: The selection currently includes everything.
                //       A good optimization would be to filter this array so that it includes only what we need it to include.         
                BuildPipeline.BuildAssetBundle(activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.iPhone);
            }
        }
    }
}

3.动态资源的存放

  有时我需要存放一些自己的文件在磁盘上,例如我想把几个bundle放在初始的安装里, unity有一个streaming asset的概念,用于提供存储接口的访问。我们需要在编辑器建立一个StreamingAssets名字的文件夹,把需要我们放在客户磁盘上的动态文件放在这个文件夹下面,这样安装后,这些文件会放在用户磁盘的指定位置,这个位置可以通过Application.streamingAssetsPath来得到。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值