Unity - 在检视面板中基于结构体字段自定义结构体名称

3

我有一个自定义的可序列化结构体,存储在列表元素中。 当结构体只有一个公共字段时

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

那么整个结构体将被命名为该元素的检查器名称在此输入图片描述

但是当结构体中有多个公共字段时

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

在SO上不接受截图,请将您的代码作为文本包含。 - Ashley Medway
编辑。代码而非屏幕截图。 - Math Guy
你尝试过创建自定义检视面板吗? - mrogal.ski
尝试使用PropertyDrawer,但它仅适用于类。 - Math Guy
3
我看不出那两个踩的原因,楼主马上就通过发布代码而不是截图进行了更正。 - Galandil
@Galandil 我也注意到了那个踩票,所以我给了 OP 一个赞。我认为踩票的人是来自不理解问题的 C# 用户。 - Programmer
1个回答

8

这是由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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接