目的:读取streamingassets下的文件中指定的一段字节
已知:文件中的起始位置,和需要读取的长度
1.android下读取
1.1 不能直接使用C#的FileStream,读取失败
var buffer = new byte[size];
FileStream stream = File.OpenRead(path);
stream.Read(buffer , pos, size);
报错:
IsolatedStorageException: Could not find a part of the path "/jar:file:/data/app/com.xxx.xxxx-1/base.apk!/assets/xxx.pack".
1.2 可以使用Unity原生接口与Android交互
主要过程:
Java
public class XXXPlugin extends UnityPlayerNativeActivity {
protected AssetManager assetManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
assetManager = getAssets();
}
//返回字节数组
public byte[] LoadBytes(String path,int offset,int len)
{
//可以缓存起来,不需每次都open
InputStream inputStream = assetManager.open(path);
try {
byte buf[] = new byte[len];
inputStream.reset();
//注意skip、read的可靠性
inputStream.skip(offset);
inputStream.read(buf,0,len)
inputStream.close();
return buf;
}
catch (IOException e) {
Log.v ("unity", e.getMessage());
}
return null;
}
}
注意skip、read的可靠性,每次调用不一定能返回正确长度,可能需多次调用。
C#:
public static byte[] read_streamingpath_bytes(string path,I