c# - 我如何更改枚举元素内的任何变量?

标签 c# list unity3d enums

这是我的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
     
public class Item
{
    public Inventory inventory;
    public enum ItemType
    {
        Blocks,
        Potions,
        Weapons,
    }

    public ItemType itemType
    public bool stackable;
     
}

我想做的是仅将枚举 ItemType Blocks 更改为可堆叠,这样我就可以使用将项目添加到列表中,如果项目被阻止,它将默认设置为可堆叠。

我怎样才能做到这一点?

我尝试了 ItemTypes.Blocks.stackable = true; 但它不起作用。

最佳答案

你的问题措辞很差,所以很困惑。我将对您的意思做出最好的猜测,并据此提出建议。首先,从类中获取 enum。嵌套类型通常不是一个好主意。

 public enum ItemType
 {
     Blocks,
     Potions,
     Weapons
 }

接下来,使用属性来公开公共(public)数据而不是字段。然后您可以提供其他实现细节:

 public class Item
 {
     public Inventory Inventory { get; set; }
     public ItemType ItemType { get; set; }
     public bool IsStackable => ItemType == ItemType.Blocks;
 }

IsStackable 属性是只读的,它的值是根据 ItemType 属性的值动态生成的。

关于c# - 我如何更改枚举元素内的任何变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72358758/

相关文章:

unity3d - 无法使用 ARcore : Manifest Merger failed 在 Unity 2019.2.4f1 中构建 .APK

c# - 自动填充检查器数组

c# - ID一次性级联

c# - Stopped System.Timers.Timer 在设置 AutoReset 后开始触发 Elapsed 事件

c# - 从 F# 中的 map 中删除所有内容

c# - 为什么写入 24 位结构不是原子的(当写入 32 位结构时似乎是)?

Python:返回两个或多个列表共有的项目

c# - 我可以在没有 Visual Studio 的情况下编译 DLL 吗?

python - 为什么这个程序中索引会越界?

list - 为什么 "and"和 "or"在收到列表后会提供这些结果?