简体   繁体   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?)

I have these errors when trying to write this script for save and load in my game.尝试编写此脚本以在我的游戏中保存和加载时出现这些错误。

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

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

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

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

That is the script i'm working on:那是我正在处理的脚本:

    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>();

    }

}

EDIT:编辑:

This is the script for PlaceableObjectData:这是 PlaceableObjectData 的脚本:

using System;
using UnityEngine;



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

EDIT 2编辑 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(17,13):错误 CS0103:名称“目录”在当前上下文中不存在

Assets\Scripts\Save System\SaveSystem.cs(20,13): error CS0103: The name 'Directory' does not exist in the current context Assets\Scripts\Save System\SaveSystem.cs(20,13):错误 CS0103:名称“目录”在当前上下文中不存在

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\SaveData.cs(26,21):错误 CS0246:找不到类型或命名空间名称“placeableObjectDatas”(是否缺少 using 指令或程序集引用?)

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

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(32,42):错误 CS0103:名称“ReferenceLoopHandling”在当前上下文中不存在

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(34,29):错误 CS0103:名称“JsonConvert”在当前上下文中不存在

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

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\SaveData.cs(48,21):错误 CS0246:找不到类型或命名空间名称“placeableObjectDatas”(是否缺少 using 指令或程序集引用?)

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(42,22): error CS1061: 'string' 不包含 'Exists' 的定义,并且没有可访问的扩展方法 'Exists' 接受类型为 'string' 的第一个参数找到(您是否缺少 using 指令或程序集引用?)

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(45,42): error CS1061: 'string' 不包含 'ReadAllText' 的定义并且没有可访问的扩展方法 'ReadAllText' 接受类型为 'string' 的第一个参数找到(您是否缺少 using 指令或程序集引用?)

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

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

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

You can fix error 1,3 and 4 by adding using System.Runtime.Serialization;您可以通过using System.Runtime.Serialization; at the top of File.在文件的顶部。

But I've never heard of PlaceableObjectData, is it maybe a custom class you have created in a other namespace?但我从未听说过 PlaceableObjectData,它可能是您在其他命名空间中创建的自定义 class 吗? Then you have to also import that namespace with the using keyword.然后,您还必须using关键字导入该命名空间。

this is the code y was making the error is in the line 7这是 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.

相关问题 错误 CS0246:找不到类型或命名空间名称“IWebHostEnvironment”(您是否缺少 using 指令或程序集引用?) - error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) 错误 CS0246:找不到类型或命名空间名称“BannerPosition”是否缺少 using 指令或程序集引用? - error CS0246: The type or namespace name 'BannerPosition' could not be found are you missing a using directive or an assembly reference? 错误 CS0246:找不到类型或命名空间名称“Npgsql”(您是否缺少 using 指令或程序集引用?) - error CS0246: The type or namespace name 'Npgsql' could not be found (are you missing a using directive or an assembly reference?) 错误CS0246找不到类型或名称空间名称“ Windows”(是否缺少using指令或程序集引用?) - Error CS0246 The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) 错误 CS0246 找不到类型或命名空间名称“CreateRandomAnswersForKey”(您是否缺少 using 指令或程序集引用?)? - Error CS0246 The type or namespace name 'CreateRandomAnswersForKey' could not be found (are you missing a using directive or an assembly reference?)? CS0246:找不到类型或名称空间名称“ Employee”(您是否缺少using指令或程序集引用?)? - CS0246: The type or namespace name 'Employee' could not be found (are you missing a using directive or an assembly reference?)? CS0246 找不到类型或命名空间名称“ErrorViewModel”(您是否缺少 using 指令或程序集引用?) - CS0246 The type or namespace name 'ErrorViewModel' could not be found (are you missing a using directive or an assembly reference?) CS0246:找不到类型或命名空间名称&#39;MySql&#39;(您是否缺少using指令或程序集引用 - CS0246: The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference Unity 错误 CS0246:找不到类型或命名空间名称“InputField”(您是否缺少 using 指令或程序集引用?) - Unity error CS0246: The type or namespace name 'InputField' could not be found (are you missing a using directive or an assembly reference?) C# 错误:错误 CS0246 找不到类型或命名空间名称“”(您是否缺少 using 指令或程序集引用?) - C# Error: Error CS0246 The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM