序
比如,序列化了一个数组,保存到磁盘上。
原来的路径是"D://test.bin",能跑,但是有点问题:序列化出来的文件和原项目离的太远,不好管理。
要是能保存到unity工程的文件夹里就好了。这个路径该怎么写?有现成的api吗?
官方文档
Unity - Scripting API: Application.dataPath (unity3d.com)

试一试
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 序列化和反序列化用到的
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class Save : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// 00.文件的路径
string filePath = Application.dataPath + "/cache"+"/test.bin"; // 建议先在Assets文件夹下手动建一个cache文件夹,不然可能会报

文章讲述了如何在Unity项目中使用序列化功能将数组保存到工程文件夹,以及如何使用BinaryFormatter进行文件操作。作者提到将数据存储在Application.dataPath指定的cache文件夹,并展示了序列化和反序列化的代码示例。
最低0.47元/天 解锁文章
1303

被折叠的 条评论
为什么被折叠?



