[英]Unity - custom struct name in inspector based on struct' filed(s)
I have a custom serializable struct stored in as a list elements. 我有一个自定义的可序列化结构存储在列表元素中。 When only one field of a struct is 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;
}
}
then this whole struct is named in inspector after that element 然后整个结构在该元素之后的检查器中命名
but when more than one field in struct is public 但是当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;
}
}
then the struct is named just "Element N" 然后结构被命名为“元素N”
How can I provide custom inspector name for my struct? 如何为我的结构提供自定义检查器名称?
Ie something like this: 就是这样的:
[NameInInspector]
string n = "(" + Importance.ToString() + ") " + Descr;
It's caused by how Unity serializes the MemoryMoment
struct. 这是由Unity如何序列化
MemoryMoment
结构引起的。
Basically, if there's a string
as the first declared field in the struct, then Unity will use its content to "name" the Element of the list. 基本上,如果
string
是结构中第一个声明的字段,那么Unity将使用其内容来“命名”列表的元素。
So, if you want to read the content of Descr
instead of Element X
you just need to move the declaration public string Descr;
因此,如果您想要读取
Descr
而不是Element X
的内容,您只需要移动声明public string Descr;
on top of all declarations: 在所有声明之上:
[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.