简体   繁体   English

如何更改枚举元素内的任何变量?

[英]How do i change any variables inside of a enum element?

This is my code这是我的代码

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;
     
}

and what I am trying to do is to change only the enum ItemType Blocks to stackable so that I can just use the add the item to the list, and if the item is blocked, it will be set to stackable by default.我想要做的只是将枚举 ItemType Blocks更改为可堆叠,这样我就可以使用将项目添加到列表中,如果项目被阻止,默认情况下它将设置为可堆叠。

how can I achieve this?我怎样才能做到这一点?

I tried ItemTypes.Blocks.stackable = true;我试过ItemTypes.Blocks.stackable = true; But it won't work.但它不会起作用。

Your question is quite poorly-worded so it is rather confusing.你的问题措辞很糟糕,所以很混乱。 I'm going to make my best guess as to what you mean and recommend based on that.我将对您的意思做出最好的猜测,并据此提出建议。 Firstly, get the enum out of the class.首先,将enum从类中取出。 Nesting types is generally not a good idea.嵌套类型通常不是一个好主意。

 public enum ItemType
 {
     Blocks,
     Potions,
     Weapons
 }

Next, use properties to expose public data rather than fields.接下来,使用属性而不是字段来公开公共数据。 You can then provide additional implementation details:然后,您可以提供其他实施细节:

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

The IsStackable property is read-only and its value is generated on the fly based on the value of the ItemType property. IsStackable属性是只读的,它的值是根据ItemType属性的值动态生成的。

Option 1:选项1:

Use an extension method to determine what is stackable.使用扩展方法来确定什么是可堆叠的。

public class Item
{
    public Inventory inventory;
    public enum ItemType
    {
        Blocks,
        Potions,
        Weapons,
    }

    public ItemType itemType; // Defaults to Blocks
}

public static class ItemTypeExtensions
{
    public static bool IsStackable(this Item.ItemType type)
    {
        return type == Item.ItemType.Blocks;
    }
}

Usage用法

var item = new Item();
bool isStackable = item.itemType.IsStackable(); // true

item.itemType = Item.ItemType.Potions;
bool isStackable = item.itemType.IsStackable(); // false

Option 2:选项 2:

When you declare an enum value, it automatically uses the default element as the value.当你声明一个枚举值时,它会自动使用默认元素作为值。 For any enum, that is the one that has the value 0. If you don't give them values (as in this case), the compiler will assign the first element with value 0. Therefore, the way you have constructed the enum ItemTypes.Blocks is the default value.对于任何枚举,它的值为 0。如果您不给它们值(如本例所示),编译器将为第一个元素分配值 0。因此,您构造枚举ItemTypes.Blocks的方式ItemTypes.Blocks是默认值。 You can rely on this default when your Item object is constructed.构建Item对象时,您可以依赖此默认值。

public class Item
{
    public Inventory inventory;
    public enum ItemType
    {
        Blocks,
        Potions,
        Weapons,
    }

    public ItemType itemType; // Defaults to Blocks
    public bool stackable = true; // Set default explicitly (which matches up with blocks)
}

However, if you want the boolean value to stay in sync with your enum value, you either need to set them at the same time or create a method on the Item class to set them both together.但是,如果您希望布尔值与枚举值保持同步,则需要同时设置它们或在 Item 类上创建一个方法以将它们设置在一起。

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

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