我有一个成员,我想揭露:
[Serializable] private sealed class InputGameObjectStack: Stack<GameObject> { public InputGameObjectStack(): base(_InputGameObjectStackSize) { } }
在MonoBehaviour
里面,我有:
[SerializeField] private InputGameObjectStack _inputGameObjectStack;
在编辑器脚本中,我可以从MonoBehaviour
对象(如SerializedProperty
)中获取属性,但没有什么可用的,允许我遍历元素。
有没有任何方法显示在检查器堆栈的constents不可变(没有function推/popup)?
假设我们有以下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); } } } }
现在,在编辑器中,您可以看到标题(因为我们在启动函数中添加了项目) – 当您运行场景时,您会看到一个不错的列表: