unity3d streaming asset文件加载

如果使用www加载资源的方式:

参考:http://docs.unity3d.com/Manual/StreamingAssets.html

安卓的路径需要 “jar:file://”开头,因为安卓自身是apk压缩格式

ios和win需要添加 “file:///”开头。

 

2014.1.4附:

1、这是现在用的加载方式(加载速度很快)

 WWW www = new WWW(path);
                yield return www;
                if (!string.IsNullOrEmpty(www.error))
                {
                    Debug.LogError("www error:" + www.error);
                }
                if (www.assetBundle.mainAsset != null)
                {
                    AssetBundle bundle = www.assetBundle;
                    ConfigDataCSVScriptableObject obj = bundle.mainAsset as ConfigDataCSVScriptableObject;
                    callback(obj);
                    bundle.Unload(true);
                    www.Dispose();
                }
                else
                {
                    Debug.LogError("can't find assetbundle with url:" + path);
                }

2、这是原来用的加载方式(加载速度很慢,慢上4~5倍)

if (File.Exists(path))
                {
                    byte[] bytes = File.ReadAllBytes(path);
                    AssetBundleCreateRequest request = AssetBundle.CreateFromMemory(bytes);
                    yield return request;
                    while (!request.isDone)
                    {
                        yield return request.isDone;
                    }
                    AssetBundle bundle = request.assetBundle;
                    
                    ConfigDataCSVScriptableObject obj = bundle.mainAsset as ConfigDataCSVScriptableObject;
                    callback(obj);
                    bytes = null;
                    bundle.Unload(true);
                }
                else
                {
                    Debug.LogError("can't find assetbundle with path:" + path);
                }

以我现在的能力,还无法了解为什么第二种为什么会很慢,也可能是中间存在一次内存copy的原因?

2014.01.06 附,查找一下慢的原因。

记录了不同加载方式解析消耗的时间,发现解析消耗的时间相差无已,看起来应该加载入内存消耗的时间不相同。

找到原因了:

AssetBundleCreateRequest request = AssetBundle.CreateFromMemory(bytes);

这一句导致的异常的慢。

WWW是加载时直接转为了assetbundle,少了这一步,所以非常快。

如果将第二种方式修改为使用WWW加载assetbundle,速度提升了近7倍。

 

发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /opt/wordpress/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048