[英]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;
}
}
如何為我的結構提供自定義檢查器名稱?
就是這樣的:
[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.