gpt4 book ai didi

c# - Unity - 在检查器中基于结构字段的自定义结构名称

转载 作者:行者123 更新时间:2023-11-30 14:22:38 25 4
gpt4 key购买 nike

我有一个自定义的可序列化结构作为列表元素存储。当一个结构体只有一个字段是 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;
}
}

然后整个结构在检查器中以该元素命名 enter image description here

但是当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;
}
}

然后该结构被命名为“Element N”enter image description here

如何为我的结构提供自定义检查器名称?

即像这样:

[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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com