在Native插件中加载资源(Unity)

hat*_*ero 2 mono resources unity-game-engine

如何从本机插件中加载资源(文本文件,纹理等)?我正在尝试实现Resources.Load()的单调调用,但我不确定如何处理将从此操作返回的Object(假设它成功).任何帮助将不胜感激 :).

ble*_*ter 8

Unity支持的插件直接从本机文件系统加载资源的方法是将这些资源放入项目中名为"StreamingAssets"的文件夹中.安装基于Unity的应用程序后,此文件夹的内容将复制到本机文件系统(Android除外,请参见下文).原生端上此文件夹的路径因平台而异.

在Unity v4.x及更高版本中,此路径可用作 Application.streamingAssetsPath;

请注意,在Android上,放置在StreamingAssets中的文件会打包成.jar文件,尽管可以通过解压缩.jar文件来访问它们.

在Unity v3.x中,您必须自己手动构建路径,如下所示:

  • 除iOS和Android之外的所有平台: Application.dataPath + "/StreamingAssets"
  • iOS版: Application.dataPath + "/Raw"
  • Android:.jar的路径是: "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/";
Run Code Online (Sandbox Code Playgroud)

请注意,在Android上,Android 2.2及更早版本无法直接解压大型.jars(通常大于1 MB),因此您需要将其作为边缘情况处理.

参考文献:http://unity3d.com/support/documentation/Manual/StreamingAssets.html,以及http://answers.unity3d.com/questions/126578/how-do-i-get-into-my-streamingassets-folder -from-t.htmlhttp://answers.unity3d.com/questions/176129/accessing-game-files-in-xcode-project.html