File I/O in Unity3D

本文介绍了一种在Unity3D中实现跨平台文件读写的方法,使用Application.persistentDataPath来确保代码能在不同平台上正常运行,如PC、Mac、iOS及Android等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

At the moment, there is no platform-independent way to save data inUnity3D.

If you want to open a file for reading or writing, you need to use the proper path. This code will do the trick and works on PC, Mac, iOS and Android:

       string fileName = "";
#if UNITY_IPHONE			
	string fileNameBase = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/'));
	fileName = fileNameBase.Substring(0, fileNameBase.LastIndexOf('/')) + "/Documents/" + FILE_NAME;
#elif UNITY_ANDROID
	fileName = Application.persistentDataPath + "/" + FILE_NAME ;
#else
	fileName = Application.dataPath + "/" + FILE_NAME;
#endif
 
fileWriter = File.CreateText(fileName);
fileWriter.WriteLine("Hello world");
fileWriter.Close();

[edit]

Since Unity 3.3, it is no longer necessary to use platform-specific code for simple file I/O. Use Application.persistentDataPath to do the trick.

This code would replace the code above:

	string fileName = Application.persistentDataPath + "/" + FILE_NAME;
	fileWriter = File.CreateText(fileName);
	fileWriter.WriteLine("Hello world");
	fileWriter.Close();

Thanks, erique, for pointing this out on the Unity forum.

报错:guo@guo-Dell-G15-5520:~/g2o仿真/src/build$ make Consolidate compiler generated dependencies of target g2o_demo [ 50%] Building CXX object CMakeFiles/g2o_demo.dir/optimize.cc.o /home/guo/g2o仿真/src/optimize.cc: In member function ‘void TrajectoryOptimizer::optimizeTrajectory(std::vector<Eigen::Transform<double, 3, 1> >&)’: /home/guo/g2o仿真/src/optimize.cc:28:62: error: call of overloaded ‘make_unique<g2o::BlockSolverX>(std::remove_reference<std::unique_ptr<g2o::LinearSolverDense<Eigen::Matrix<double, -1, -1> >, std::default_delete<g2o::LinearSolverDense<Eigen::Matrix<double, -1, -1> > > >&>::type)’ is ambiguous 28 | make_unique<g2o::BlockSolverX>(move(linearSolver))); | ^ In file included from /usr/include/c++/9/memory:80, from /opt/ros/noetic/include/g2o/core/robust_kernel.h:30, from /opt/ros/noetic/include/g2o/core/base_binary_edge.h:34, from /home/guo/g2o仿真/src/optimize.cc:6: /usr/include/c++/9/bits/unique_ptr.h:856:5: note: candidate: ‘typename std::_MakeUniq<_Tp>::__single_object std::make_unique(_Args&& ...) [with _Tp = g2o::BlockSolver<g2o::BlockSolverTraits<-1, -1> >; _Args = {std::unique_ptr<g2o::LinearSolverDense<Eigen::Matrix<double, -1, -1, 0, -1, -1> >, std::default_delete<g2o::LinearSolverDense<Eigen::Matrix<double, -1, -1, 0, -1, -1> > > >}; typename std::_MakeUniq<_Tp>::__single_object = std::unique_ptr<g2o::BlockSolver<g2o::BlockSolverTraits<-1, -1> >, std::default_delete<g2o::BlockSolver<g2o::BlockSolverTraits<-1, -1> > > >]’ 856 | make_unique(_Args&&... __args) | ^~~~~~~~~~~ In file included from /opt/ros/noetic/include/g2o/core/base_binary_edge.h:36, from /home/guo/g2o仿真/src/optimize.cc:6: /opt/ros/noetic/include/g2o/stuff/misc.h:52:20: note: candidate: ‘std::unique_ptr<_Tp> g2o::make_unique(ArgTs&& ...) [with T = g2o::BlockSolver<g2o::BlockSolverTraits<-1, -1> >; ArgTs = {std::unique_ptr<g2o::LinearSolverDense<Eigen::Matrix<double, -1, -1, 0, -1, -1> >, std::default_delete<g2o::LinearSolverDense<Eigen::Matrix<double, -1, -1, 0, -1, -1> > > >}]’ 52 | std::unique_ptr<T> make_unique(ArgTs&& ...args) | ^~~~~~~~~~~ make[2]: *** [CMakeFiles/g2o_demo.dir/build.make:76:CMakeFiles/g2o_demo.dir/optimize.cc.o] 错误 1 make[1]: *** [CMakeFiles/Makefile2:673:CMakeFiles/g2o_demo.dir/all] 错误 2 make: *** [Makefile:146:all] 错误 2
最新发布
08-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值