Unity - 基于struct'提交的检查器中的自定义结构名称

时间:2018-02-08 11:14:49

标签: c# unity3d serialization struct

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

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

然后整个结构在该元素http://fotovideotec.de/adobe_rgb/

之后的检查器中命名

但是当struct中的多个字段是公共

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

1 个答案:

答案 0 :(得分:6)

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