C# Unity—基于struct';存档
我有一个自定义的可序列化结构作为列表元素存储在中。 当结构中只有一个字段是公共字段时C# Unity—基于struct';存档,c#,unity3d,serialization,struct,C#,Unity3d,Serialization,Struct,我有一个自定义的可序列化结构作为列表元素存储在中。 当结构中只有一个字段是公共字段时 [System.Serializable] public struct MemoryMoment { float Importance; //Subjective Person who; Room where; string when; string what; //string why; public string Descr; publi
[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;
}
}
[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;
}
}
然后整个结构在inspector中以该元素命名
但当struct中有多个字段是公共字段时
[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;
}
}
[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;
}
}
然后结构名为“Element N”
如何为我的结构提供自定义检查器名称?
也就是说,类似这样的事情:
[NameInInspector]
string n = "(" + Importance.ToString() + ") " + Descr;
这是由Unity如何序列化
memorymont
struct引起的
基本上,如果结构中有一个字符串作为第一个声明的字段,那么Unity将使用其内容“命名”列表中的元素
因此,如果要读取Descr
的内容而不是Element X
,只需移动声明公共字符串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;
}
}
不接受屏幕截图,请将代码作为text.Edited包含。代码而不是屏幕截图。您是否尝试创建自定义检查器?尝试了PropertyDrawer,但它仅适用于类。我不知道这两次否决的原因,请立即通过发布代码而不是屏幕截图进行更正。