c# - Unity - 在检查器中基于结构字段的自定义结构名称

标签 c# unity3d serialization struct

我有一个自定义的可序列化结构作为列表元素存储。 当一个结构体只有一个字段是 public 时

[System.Serializable]
public struct MemoryMoment {
    float Importance;   //Subjective
    Person who;
    Room where;
    string when;
    string what;
    //string why;
    public string Descr;

    public MemoryMoment (float importance, Person who, Room where, string when, string what) {
        this.Importance = importance;
        this.who = who;
        this.where = where;
        this.when = when;
        this.what = what;
        //this.why = "UNUSED";
        this.Descr = where.Type.ToString () + " " + when + ", " + who.Name + " " + what;
    }
}

然后整个结构在检查器中以该元素命名 enter image description here

但是当struct中有多个字段是public时

[System.Serializable]
public struct MemoryMoment {
    public float Importance;    //Subjective
    Person who;
    Room where;
    string when;
    string what;
    //string why;
    public string Descr;

    public MemoryMoment (float importance, Person who, Room where, string when, string what) {
        this.Importance = importance;
        this.who = who;
        this.where = where;
        this.when = when;
        this.what = what;
        //this.why = "UNUSED";
        this.Descr = where.Type.ToString () + " " + when + ", " + who.Name + " " + what;
    }
}

然后该结构被命名为“Element N”enter image description here

如何为我的结构提供自定义检查器名称?

即像这样:

[NameInInspector]
string n = "(" + Importance.ToString() + ") " + Descr;

最佳答案

这是由 Unity 如何序列化 MemoryMoment 结构引起的。

基本上,如果有一个 string 作为结构中第一个声明的字段,那么 Unity 将使用它的内容来“命名”列表的元素。

因此,如果您想读取Descr 的内容而不是Element X,您只需移动声明public string Descr;在所有声明之上:

[System.Serializable]
public struct MemoryMoment {
    public string Descr;
    public float Importance;    //Subjective
    Person who;
    Room where;
    string when;
    string what;
    //string why;

    public MemoryMoment (float importance, Person who, Room where, string when, string what) {
        this.Importance = importance;
        this.who = who;
        this.where = where;
        this.when = when;
        this.what = what;
        //this.why = "UNUSED";
        this.Descr = where.Type.ToString () + " " + when + ", " + who.Name + " " + what;
    }
}

关于c# - Unity - 在检查器中基于结构字段的自定义结构名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48684182/

相关文章:

c# - 我正在尝试从日志文件中解析乌克兰日期/时间字符串

c# - 通过向下转换参数自动重载和调用函数

unity3d - 单击按钮时的麦克风录音

node.js - 序列化失败(grpc/node)

C#序列化

c# - 重命名 Windows azure 的暂存 url

c# - 如何检查我是否正在进行交易?

c# - 在Unity3D中无法接收来自PLC的C#UDP消息,但在Wireshark中可见

android - 在 Android 上访问 Unity StreamingAssets

java - 可序列化与捕获 java/命令类有何关系?