簡體   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