簡體   English   中英

只讓字符串成為某個值:Scriptable Objects

[英]Only letting a string be a certain value: Scriptable Objects

我正在為我的游戲中的任何項目開發一個可編寫腳本的 object 類型,並且我有一個名為 s_Type 的字符串。 我只希望這個字符串是某個值; “食物”、“故事”、“收藏品”或“一無所有”。 我雖然關於使用 class 但它會在我的腳本中造成混亂,我更喜歡使用簡單的字符串。 如果字符串的值不是上述之一,我怎么能返回錯誤?

這是我當前的腳本:

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

public class PickableItem : ScriptableObject
{
    [Header("Properties")]

    /// <summary>
    ///(string) The name of the item
    /// </summary>
    public string s_ItemName;

    /// <summary>
    /// (string) Description of the item
    /// </summary>
    public string s_Description;

    /// <summary>
    /// (string) types can be as followed: "food", "story", "collectible", "nothing"
    /// </summary>
    public string s_Type;
}

使用枚舉值:

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

public class PickableItem : ScriptableObject
{
    [Header("Properties")]

    /// <summary>
    ///(string) The name of the item
    /// </summary>
    public string s_ItemName;

    /// <summary>
    /// (string) Description of the item
    /// </summary>
    public string s_Description;

    /// <summary>
    /// (string) types can be as followed: "food", "story", "collectible", "nothing"
    /// </summary>
    public string s_Type  => s_TypeEnum.ToString();

    /// <summary>
    /// (string) types can be as followed: "food", "story", "collectible", "nothing"
    /// </summary>
    [SerializeField] private sType s_TypeEnum;
}

public enum sType 
{
    nothing, 
    story,
    collectible, 
    food
}

如果輸入不是預期值,您可以將字段轉換為屬性並使其 setter 方法拋出異常:

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

public class PickableItem : ScriptableObject
{
    [Header("Properties")]

    /// <summary>
    ///(string) The name of the item
    /// </summary>
    public string s_ItemName;

    /// <summary>
    /// (string) Description of the item
    /// </summary>
    public string s_Description;
    
    /// <summary>
    /// (string) a private storage field for s_Type, can be named whatever you want
    /// </summary>
    private string s_type;
    
    /// <summary>
    /// (string) types can be as followed: "food", "story", "collectible", "nothing"
    /// </summary>
    public string s_Type {
      get {
        return s_type;
      }
      set {
        switch (value) {
          case "food":
          case "story":
          case "collectible":
          case "nothing":
            s_type = value;
            break;
          default:
            throw new ArgumentException("Unknown PickableItem type!");
        }
      }
    }
}

但是創建一個enum可能會好得多,特別是如果這些值來自您的代碼而不是用戶輸入。 不過,這就是你的答案。

好吧,你不能強制規定字符串只能是那些詞,但如果你願意,你可以使用一個屬性,如下所示:

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

public class PickableItem : ScriptableObject
{
    [Header("Properties")]

    /// <summary>
    ///(string) The name of the item
    /// </summary>
    public string s_ItemName;

    /// <summary>
    /// (string) Description of the item
    /// </summary>
    public string s_Description;

    /// <summary>
    /// (string) types can be as followed: "food", "story", "collectible", "nothing"
    /// </summary>
    private string s_Type0;

    public string s_Type
    {
        get
        {
            return s_Type0;
        }

        set
        {
            switch (value)
            {
                case "food":
                case "story":
                case "collectible":
                case "nothing":
                    s_Type0 = value;
                    break;
                default:
                    throw new InvalidArgumentException("Invalid value!");
            }
        }
    }
}

但我更喜歡這樣做:

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

public class PickableItem : ScriptableObject
{
    public enum S_TYPE
    {
        Food,
        Story,
        Collectible,
        Nothing
    }

    [Header("Properties")]
    /// <summary>
    ///(string) The name of the item
    /// </summary>
    public string s_ItemName;

    /// <summary>
    /// (string) Description of the item
    /// </summary>
    public string s_Description;

    /// <summary>
    /// (string) types can be as followed: "food", "story", "collectible", "nothing"
    /// </summary>
    public S_TYPE s_Type;
}

然后您可以使用s_Type.ToString()獲取字符串值,然后進行相應檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM