Use CustomEditor to display the content of your stack
For Example
Assume we have the following class 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"));
}
}
Create another class inside the existing class so it could access the private members, like so:
StackPreview added inside 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"));
}
// 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);
}
}
}
}
Now, in the editor you can see just the header (because we're adding items in the start function) - when you run the scene you get a nice list:
