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

Mat*_*Guy 3 c# serialization struct unity-game-engine

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

[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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后整个结构在该元素之后的检查器中命名在此输入图像描述

但是当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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后结构被命名为"元素N"在此输入图像描述

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

就是这样的:

[NameInInspector]
string n = "(" + Importance.ToString() + ") " + Descr;
Run Code Online (Sandbox Code Playgroud)

Gal*_*dil 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;
    }
}
Run Code Online (Sandbox Code Playgroud)