繁体   English   中英

错误 CS0246:找不到类型或命名空间名称“StreamingContext”(是否缺少 using 指令或程序集引用?)

[英]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(62,40):错误 CS0246:找不到类型或命名空间名称“StreamingContext”(是否缺少 using 指令或程序集引用?)

Assets\Scripts\Save System\SaveData.cs(13,31):错误 CS0246:找不到类型或命名空间名称“PlaceableObjectData”(是否缺少 using 指令或程序集引用?)

Assets\Scripts\Save System\SaveData.cs(61,6):错误 CS0246:找不到类型或命名空间名称“OnDeserializedAttribute”(是否缺少 using 指令或程序集引用?)

Assets\Scripts\Save System\SaveData.cs(61,6):错误 CS0246:找不到类型或命名空间名称“OnDeserialized”(是否缺少 using 指令或程序集引用?)

那是我正在处理的脚本:

    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):错误 CS0103:名称“目录”在当前上下文中不存在

Assets\Scripts\Save System\SaveSystem.cs(20,13):错误 CS0103:名称“目录”在当前上下文中不存在

Assets\Scripts\Save System\SaveData.cs(26,21):错误 CS0246:找不到类型或命名空间名称“placeableObjectDatas”(是否缺少 using 指令或程序集引用?)

Assets\Scripts\Save System\SaveSystem.cs(31,28):错误 CS0246:找不到类型或命名空间名称“JsonSerializerSettings”(是否缺少 using 指令或程序集引用?)

Assets\Scripts\Save System\SaveSystem.cs(32,42):错误 CS0103:名称“ReferenceLoopHandling”在当前上下文中不存在

Assets\Scripts\Save System\SaveSystem.cs(34,29):错误 CS0103:名称“JsonConvert”在当前上下文中不存在

Assets\Scripts\Save System\SaveSystem.cs(36,19):错误 CS1061:“string”不包含“WriteAllText”的定义,并且没有可访问的扩展方法“WriteAllText”接受类型为“string”的第一个参数找到(您是否缺少 using 指令或程序集引用?)

Assets\Scripts\Save System\SaveData.cs(48,21):错误 CS0246:找不到类型或命名空间名称“placeableObjectDatas”(是否缺少 using 指令或程序集引用?)

Assets\Scripts\Save System\SaveSystem.cs(42,22): error CS1061: 'string' 不包含 'Exists' 的定义,并且没有可访问的扩展方法 'Exists' 接受类型为 'string' 的第一个参数找到(您是否缺少 using 指令或程序集引用?)

Assets\Scripts\Save System\SaveSystem.cs(45,42): error CS1061: 'string' 不包含 'ReadAllText' 的定义并且没有可访问的扩展方法 'ReadAllText' 接受类型为 'string' 的第一个参数找到(您是否缺少 using 指令或程序集引用?)

Assets\Scripts\Save System\SaveSystem.cs(47,13):错误 CS0246:找不到类型或命名空间名称“saveData”(是否缺少 using 指令或程序集引用?)

Assets\Scripts\Save System\SaveSystem.cs(47,31):错误 CS0103:名称“JsonConvert”在当前上下文中不存在

Assets\Scripts\Save System\SaveSystem.cs(47,61):错误 CS0246:找不到类型或命名空间名称“saveData”(是否缺少 using 指令或程序集引用?)

您可以通过using System.Runtime.Serialization; 在文件的顶部。

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

这是 y 的代码,错误在第 7 行

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private new RigidBody rigidbody;

    public float movementSpeed;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody = GetComponent<RigidBody>();
    }

    // Update is called once per frame
    void Update()
    {
        float hor = Input.GetAxisRaw("Horizontal");
        float ver = Input.GetAxisRaw("Vertical");

        if (hor != 0 || ver != 0) {
            Vector3 direction = (transform.forward * ver + transform.right * hor).normalized;

            rigidbody.velocity = direction * movementSpeed;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM