c# - 错误 CS0246 : The type or namespace name 'StreamingContext' could not be found (are you missing a using directive or an assembly reference?)

标签 c# unity-game-engine

我在尝试编写此脚本以在游戏中保存和加载时遇到这些错误。

Assets\Scripts\Save System\SaveData.cs(62,40): error CS0246: The type or namespace name 'StreamingContext' could not be found (are you missing a using directive or an assembly reference?)

Assets\Scripts\Save System\SaveData.cs(13,31): error CS0246: The type or namespace name 'PlaceableObjectData' could not be found (are you missing a using directive or an assembly reference?)

Assets\Scripts\Save System\SaveData.cs(61,6): error CS0246: The type or namespace name 'OnDeserializedAttribute' could not be found (are you missing a using directive or an assembly reference?)

Assets\Scripts\Save System\SaveData.cs(61,6): error CS0246: The type or namespace name 'OnDeserialized' could not be found (are you missing a using directive or an assembly reference?)

这是我正在编写的脚本:

    using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization;


[Serializable]

public class SaveData
{
    public static int IdCount;

    public Dictionary<string, PlaceableObjectsData> placeableObjectDatas =
        new Dictionary<string, PlaceableObjectsData>();

    public static string GenerateId()
    {
        IdCount++;
        return IdCount.ToString();

    }

     public void AddData(Data data)
    {
        if (data is placeableObjectDatas plObjData)

        {

            if (placeableObjectDatas.ContainsKey(plObjData.ID))
        {
            placeableObjectDatas[plObjData.ID] = plObjData;

        }
        else
        
        {
            placeableObjectDatas.Add(plObjData.ID, plObjData);
        }

        }

   }

      public void RemoveData(Data data)
       {

        if (data is placeableObjectDatas plObjData)

        {

            if (placeableObjectDatas.ContainsKey(plObjData.ID))
            {
                placeableObjectDatas.Remove(plObjData.ID);

            }

        }

    }

    [OnDeserialized]
    internal void OnDeserializedMethod(StreamingContext context)
    {
    placeableObjectDatas ??= new Dictionary<string, PlaceableObjectsData>();

    }

}

编辑:

这是 PlaceableObjectData 的脚本:

using System;
using UnityEngine;



public class PlaceableObjectsData : Data
{
    public string assetName;
    public Vector3 position;
}

编辑2

Assets\Scripts\Save System\SaveSystem.cs(17,13): error CS0103: The name 'Directory' does not exist in the current context

Assets\Scripts\Save System\SaveSystem.cs(20,13): error CS0103: The name 'Directory' does not exist in the current context

Assets\Scripts\Save System\SaveData.cs(26,21): error CS0246: The type or namespace name 'placeableObjectDatas' could not be found (are you missing a using directive or an assembly reference?)

Assets\Scripts\Save System\SaveSystem.cs(31,28): error CS0246: The type or namespace name 'JsonSerializerSettings' could not be found (are you missing a using directive or an assembly reference?)

Assets\Scripts\Save System\SaveSystem.cs(32,42): error CS0103: The name 'ReferenceLoopHandling' does not exist in the current context

Assets\Scripts\Save System\SaveSystem.cs(34,29): error CS0103: The name 'JsonConvert' does not exist in the current context

Assets\Scripts\Save System\SaveSystem.cs(36,19): error CS1061: 'string' does not contain a definition for 'WriteAllText' and no accessible extension method 'WriteAllText' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

Assets\Scripts\Save System\SaveData.cs(48,21): error CS0246: The type or namespace name 'placeableObjectDatas' could not be found (are you missing a using directive or an assembly reference?)

Assets\Scripts\Save System\SaveSystem.cs(42,22): error CS1061: 'string' does not contain a definition for 'Exists' and no accessible extension method 'Exists' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

Assets\Scripts\Save System\SaveSystem.cs(45,42): error CS1061: 'string' does not contain a definition for 'ReadAllText' and no accessible extension method 'ReadAllText' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

Assets\Scripts\Save System\SaveSystem.cs(47,13): error CS0246: The type or namespace name 'saveData' could not be found (are you missing a using directive or an assembly reference?)

Assets\Scripts\Save System\SaveSystem.cs(47,31): error CS0103: The name 'JsonConvert' does not exist in the current context

Assets\Scripts\Save System\SaveSystem.cs(47,61): error CS0246: The type or namespace name 'saveData' could not be found (are you missing a using directive or an assembly reference?)

最佳答案

您可以通过在文件顶部添加 using System.Runtime.Serialization; 来修复错误 1,3 和 4。

但我从未听说过 PlaceableObjectData,它是否可能是您在其他命名空间中创建的自定义类?然后,您还必须使用 using 关键字导入该命名空间。

关于c# - 错误 CS0246 : The type or namespace name 'StreamingContext' could not be found (are you missing a using directive or an assembly reference?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73250763/

相关文章:

c# - MVC 一种类型,一页 - 两种不同的 DisplayFor

c# - 如何在 Visual Studio 中生成包含所有属性的 ToString()?

c# - 反射 - 将一个委托(delegate)添加到另一个委托(delegate)的调用列表

android - 在 Android Unity 上找不到 gdiplus.dll

unity-game-engine - Unity中看不到飞机

c# - BindingSource ListChanged 事件在位置更改时触发

c# - 如何将分钟解析为秒?

unity-game-engine - 如何统一在地形上绘制图像

android - Google Analytics 适用于桌面设备,但不适用于 Android 设备

c# - Unity3D SetActive 卡住UI面板