英文:
Generate APK with files (json) in Unity 3D
问题
我有关于我的项目的问题,为了让它更清楚,我在Assets文件夹中有.json文件,尽管我认为它们应该在Resources文件夹中。问题是,当我在Oculus上测试我的项目时,读取文件是可以的,但当我生成apk并运行游戏时,它不再能够读取这些文件。
起初,我以为是apk内部路径发生了变化,但当我解包apk时,我没有找到.json文件,或者它是否已经转换成其他类型的文件了。我是3D和Unity方面的新手,希望你能理解。
英文:
I have a question about my project, to put it in context I have .json files in the Assets folder, although I think they should be in Resources. The thing is that when I test my project on the Oculus, reading the files works, but when I generate the apk and run the game, it no longer reads the files.
At first I thought that it changed the path inside the apk but when I unpacked the apk I didn't find the .json or if it was converted into another type of file. I am new to 3D and Unity I hope your understanding.
答案1
得分: 1
Unity 只会将被引用的内容编译到最终构建中,例如用于材质的纹理,附加到场景中的游戏物体的脚本(MonoBehaviours
)等。
您对使用Resources
目录的猜测是正确的。如果您想传递那些不能被Unity引用的文件,您需要将这些文件放入Assets/Resources
目录中。然后,您可以使用Resources.Load()
方法(或Resources
类中的其他方法)来读取这些文件。
之所以在使用Unity编辑器进行测试时能够正常工作,是因为它不会模拟哪些资源会被传递到您的构建中,因此它将始终使用项目中的所有文件。
编辑:Unity文档还建议在Resources目录变得过于臃肿时使用Addressables
包,因此您还可以将Addressables
包视为解决您问题的潜在解决方案。
英文:
Unity will only compile things into your final build that are referenced, such as Textures being used in Materials, Scripts (MonoBehaviours
) that are attached to GameObjects
within scenes, etc.
Your hunch on using the Resources
directory was correct. If you want to pass along files that aren't / can't be referenced by Unity, then you need to put these files into the Assets/Resources
directory. Then you can read the files using the Resources.Load()
method (or a different method in the Resources
class).
The reason why it worked while testing using the Unity Editor, is because it doesn't simulate which resources do- and do not get passed onto your build, so it'll always use all the files that are in your project.
Edit: The Unity documentation also recommends to use the Addressables
package if your Resources directory gets too bloated, so you can also see the Addressables
package as a potential solution to your problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论