我有一个自定义的可序列化结构作为列表元素存储。 当一个结构体只有一个字段是 public 时
[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中有多个字段是public时
[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;
}
}
关于c# - Unity - 在检查器中基于结构字段的自定义结构名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48684182/