我该如何在本地插件中加载资源(文本文件、纹理等)?我正在尝试实现对Resources.Load()的mono调用,但我不确定如何处理此操作将返回的对象(假设它成功)。非常感谢您的帮助 :)
我该如何在本地插件中加载资源(文本文件、纹理等)?我正在尝试实现对Resources.Load()的mono调用,但我不确定如何处理此操作将返回的对象(假设它成功)。非常感谢您的帮助 :)
Application.streamingAssetsPath;
获取该路径。Application.dataPath + "/StreamingAssets"
Application.dataPath + "/Raw"
"jar:file://" + Application.dataPath + "!/assets/"
if (Application.platform == RuntimePlatform.IPhonePlayer) dir = Application.dataPath + "/Raw/";
else if (Application.platform == RuntimePlatform.Android) {
// On Android, we need to unpack the StreamingAssets from the .jar file in which
// they're archived into the native file system.
// Set "filesNeeded" to a list of files you want unpacked.
dir = Application.temporaryCachePath + "/";
foreach (string basename in filesNeeded) {
if (!File.Exists(dir + basename)) {
WWW unpackerWWW = new WWW("jar:file://" + Application.dataPath + "!/assets/" + basename);
while (!unpackerWWW.isDone) { } // This will block in the webplayer.
if (!string.IsNullOrEmpty(unpackerWWW.error)) {
Debug.Log("Error unpacking 'jar:file://" + Application.dataPath + "!/assets/" + basename + "'");
dir = "";
break;
}
File.WriteAllBytes(dir + basename, unpackerWWW.bytes); // 64MB limit on File.WriteAllBytes.
}
}
}
else dir = Application.dataPath + "/StreamingAssets/";
NSString* bundlePath = [[NSBundle mainBundle] bundlePath];
NSString* streamingAssetsPath = [NSString stringWithFormat:@"%@/Data/Raw/", bundlePath];
安卓
我不喜欢在安卓上使用流媒体资产,因为它需要复制所有文件,这很麻烦。更清晰的做法是在插件目录中创建文档定义的目录结构。
例如,在您的Unity项目中,图像可以像这样定位:
Assets/Plugins/Android/res/drawable/image.png
Context context = (Context)UnityPlayer.currentActivity;
String packageName = context.getPackageName();
int imageResourceId = context.getResources().getIdentifier("image", "drawable", packageName);
其余的处理方式由您决定!希望这有所帮助 :)