繁体   English   中英

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

[英]Unity - custom struct name in inspector based on struct' filed(s)

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

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

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

但是当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;
    }
}

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

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

就是这样的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM