如何创建自定义检查器来显示genericsStack <T>类

我有一个成员,我想揭露:

[Serializable] private sealed class InputGameObjectStack: Stack<GameObject> { public InputGameObjectStack(): base(_InputGameObjectStackSize) { } } 

MonoBehaviour里面,我有:

 [SerializeField] private InputGameObjectStack _inputGameObjectStack; 

在编辑器脚本中,我可以从MonoBehaviour对象(如SerializedProperty )中获取属性,但没有什么可用的,允许我遍历元素。

有没有任何方法显示在检查器堆栈的constents不可变(没有function推/popup)?

使用CustomEditor来显示你的堆栈的内容

例如

假设我们有以下TestScript

TestScript

 public class TestScript : MonoBehaviour { private Stack<GameObject> myStack = new Stack<GameObject>(); void Start() { myStack.Push(new GameObject("Red Arrow")); myStack.Push(new GameObject("Green Ball")); myStack.Push(new GameObject("Blue Cake")); } } 

在现有类中创建另一个类以便可以访问私有成员,如下所示:

TestScript中添加StackPreview

 public class TestScript : MonoBehaviour { private Stack<GameObject> myStack = new Stack<GameObject>(); void Start() { myStack.Push(new GameObject("Red Arrow")); myStack.Push(new GameObject("Green Ball")); myStack.Push(new GameObject("Blue Cake")); } // This class is inside the TestClass so it could access its private fields // this custom editor will show up on any object with TestScript attached to it // you don't need (and can't) attach this class to a gameobject [CustomEditor(typeof(TestScript))] public class StackPreview : Editor { public override void OnInspectorGUI() { // get the target script as TestScript and get the stack from it var ts = (TestScript)target; var stack = ts.myStack; // some styling for the header, this is optional var bold = new GUIStyle(); bold.fontStyle = FontStyle.Bold; GUILayout.Label("Items in my stack", bold); // add a label for each item, you can add more properties // you can even access components inside each item and display them // for example if every item had a sprite we could easily show it foreach (var item in stack) { GUILayout.Label(item.name); } } } } 

现在,在编辑器中,您可以看到标题(因为我们在启动函数中添加了项目) – 当您运行场景时,您会看到一个不错的列表:

运行场景后的结果