月度存档: 12月 2014

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倍。

 

text配置表优化

项目中字符串配置表是最大的一张表,足足有一万多条记录。

我们目前的存储方式是按csv类似格式存为scriptableobject。这张表的解析基本上占用总解析时间的25%~35%,因此优化这张表非常有必要。

查看了下text表中。其中重复内容大概按25%左右。因为表是按key,value键值对存储,计算了下key占用了45%的空间,这里优化的余地非常大。

1、不再存储key,而是将key改为int型的hashcode,这样,可以节省45%的空间

以下是java中的hashcode实现,测试了下,目前没发现有重复。

public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;

for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}

2、针对有重复的value的情况,另存一个Map<hashcode,hashcode>,如果发现在map中存在当前使用的hashcode,则替代为使用map中的对应的value hashcode去查找。这样可以再减少25%的空间。

android生成的keystore默认密码

好吧,我们一个项目开发人员一不小心使用了默认生成的debug.keystore提交了app,结果线上无法换keystore,我们只好将debug.keystore拿过来用了。

debug.keystore默认密码是 android。

unity3d 中尽量避免使用foreach

foreach会消耗掉额外的内存,生成gc alloc,为了压榨手机的最后一部分性能,尽量避免使用foreach。

遍历数组直接使用:

for(int m=0;m<array.length;m++){

// array[m] 操作

}

遍历dictinory使用,这种方式不会造成gc

  • var enumerator = dummyDic.GetEnumerator();
  • while (enumerator.MoveNext()) {
  • // loop body goes here
  • }

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