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

Godot C#:如何在Inspector中导出可编辑的自定义结构体列表?

解决Godot C#自定义结构体列表Inspector编辑问题

正确的结构体定义与导出方式

你之前的问题核心是没适配Godot的序列化规则,Unity的序列化逻辑和Godot不一样,按下面的步骤来就能实现:

  1. 给自定义结构体添加[Godot.Serializable]特性
    注意这是Godot命名空间下的Serializable,不是System的,同时结构体的字段要加上[Export]标记才能在Inspector显示。

    示例代码:

    using Godot;
    
    [Godot.Serializable]
    public struct SnapPoint
    {
        [Export]
        public Vector3 Position;
        
        [Export]
        public string Name;
    }
    
  2. 使用Godot.Collections.Array<T>并标记[Export]
    不要用C#原生的List<T>或数组,改用Godot自带的Array容器,同时给这个数组字段加[Export]:

    public class YourNode : Node3D
    {
        [Export]
        public Godot.Collections.Array<SnapPoint> SnapPoints;
    }
    

这样设置后,你就能在Inspector面板里直接添加、删除SnapPoint元素,每个元素的Position和Name也能直接编辑,和Unity里的体验一致。

为什么之前的方案不行?

  • 原生C#集合(List/数组):Godot的导出系统对原生C#集合的支持有限,必须用Godot.Collections下的容器才能正确序列化。
  • 仅加[System.Serializable]:这个是给.NET序列化用的,Godot的Inspector依赖自身的序列化系统,必须加[Godot.Serializable]才能识别自定义类型。
  • 改用Resource:确实可行但没必要,因为自定义结构体只要适配Godot序列化就可以直接导出,不需要额外创建资源。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 07:02:42