我目前正在为我的游戏在统一的保存/加载系统工作。但我不知道我做错了什么。以下是我的错误:
以下是我的错误:
ArgumentException: Name具有无效的chars System.IO.FileStream..ctor (System.String路径、System.IO.FileMode模式、System.IO.FileAccess access、System.IO.FileShare共享、System.Int32 bufferSize、System.Boolean匿名、System.IO.FileOptions选项) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.IO.FileStream..ctor (System.String path、System.IO.FileMode模式、System.IO.FileAccess access、System.IO.FileShare share )( System.Int32 bufferSize) (at <695d1cc93cca45069c528c15c9fdd749>:0) (包装器远程处理-调用-检查) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int) System.IO.File.Create (System.String路径,Assets/Items/Inventory/Scipts/InventoryObject.cs:30) bufferSize) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.IO.File.Create (System.String path) (at <695d1cc93cca45069c528c15c9fdd749>:0) InventoryObject.Save () (at <695d1cc93cca45069c528c15c9fdd749>:0 Player.Update () (at资产/Player/Inventory/Player.cs:22)
下面是我剩下的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
[CreateAssetMenu(fileName = "New Inventory", menuName = "Inventory System/Inventory")]
public class InventoryObject : ScriptableObject, ISerializationCallbackReceiver
{
public string savePath;
public ItemDatabaseObject database;
public List<InventorySlot> Container = new List<InventorySlot>();
public void AddItem(ItemObject _item, int _amount)
{
for (int i = 0; i < Container.Count; i++)
{
if(Container[i].item == _item)
{
Container[i].AddAmount(_amount);
return;
}
}
Container.Add(new InventorySlot(database.GetId[_item],_item, _amount));
}
public void Save()
{
string savePath = JsonUtility.ToJson(this, true);
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(string.Concat(Application.persistentDataPath, savePath));
bf.Serialize(file, savePath);
file.Close();
}
public void Load()
{
if(File.Exists(string.Concat(Application.persistentDataPath, savePath)))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(string.Concat(Application.persistentDataPath, savePath), FileMode.Open);
JsonUtility.FromJsonOverwrite(bf.Deserialize(file).ToString(), this);
file.Close();
}
}
public void OnAfterDeserialize()
{
for (int i = 0; i < Container.Count; i++)
Container[i].item = database.GetItem[Container[i].ID];
}
public void OnBeforeSerialize()
{
}
}
[System.Serializable]
public class InventorySlot
{
public int ID;
public ItemObject item;
public int amount;
public InventorySlot(int _id, ItemObject _item, int _amount)
{
ID = _id;
item = _item;
amount = _amount;
}
public void AddAmount(int value)
{
amount += value;
}
}如果你有任何问题,问他们,因为我真的想解决这个愚蠢的错误!
发布于 2022-05-12 13:14:08
文件的名称不能包含一些异常字符。在windows中,您不能有任何文件名,其字符如下图所示。

将文件的名称打印到控制台,并检查是否有任何无效字符。
Debug.Log("file name: " + string.Concat(Application.persistentDataPath, savePath));https://stackoverflow.com/questions/72216081
复制相似问题