You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#技术咨询:如何向序列化类中添加节点

Hey there! No worries at all—we all start out as newbies, and your question makes perfect sense. Let's break down how to "add nodes" to your serializable UISpellInfo class, since it sounds like you're either looking to add new serializable fields to the class itself, or create a collection of UISpellInfo instances you can add to (like the editable list in Unity's Inspector).

1. Adding new serializable fields (nodes) to the UISpellInfo class

Since you're using Unity's [Serializable] attribute, adding a new field that gets serialized (and shows up in the Unity Inspector) is straightforward, as long as you follow these simple rules:

  • The field is public (like your existing ID, Name, etc.), or marked with [SerializeField] if you want to keep it private
  • The field's type is serializable: this includes basic types like int/float/string, Unity built-ins like Sprite, or your own custom classes marked with [Serializable]

For example, if you wanted to add a Damage field to track spell damage, just insert it directly into your class:

namespace DuloGames.UI {
[Serializable]
public class UISpellInfo {
    public int ID;
    public string Name;
    public Sprite Icon;
    public string Description;
    public float Range;
    public float Cooldown;
    public float CastTime;
    public float PowerCost;
    public float test;
    // New serializable field (your "new node")
    public float Damage;
    [BitMask(typeof(UISpellInfo_Flags))]
    public UISpellInfo_Flags Flags;
}
}

If you prefer keeping the field private but still want it serialized, use the [SerializeField] attribute like this:

[SerializeField]
private float _damage;

2. Adding multiple UISpellInfo instances (a collection of nodes)

If you're trying to create a list of UISpellInfo objects that you can add to (like clicking a "+" button in the Unity Inspector to add new spell entries), you'll need a wrapper class or a serializable list:

namespace DuloGames.UI {
[Serializable]
public class UISpellInfo {
    // Your existing fields here...
}

// Wrapper class to hold a list of UISpellInfo instances
[Serializable]
public class SpellInfoCollection {
    public List<UISpellInfo> Spells = new List<UISpellInfo>();
}

Then, if you add a SpellInfoCollection field to a MonoBehaviour script, you'll see a editable list in the Inspector where you can click "+" to add new UISpellInfo nodes.

A quick note on dynamic nodes

If you were thinking of adding completely dynamic fields (like arbitrary key-value pairs that aren't predefined in the class), Unity's default serialization doesn't support this. For that, you'd need to use a library like Newtonsoft.Json (which Unity offers as a package) to handle dynamic JSON serialization, but that's a more advanced approach. For most game use cases, the static field method above is the simplest and most reliable route.

内容的提问来源于stack exchange,提问作者Venelin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:29:47