Home > Blockchain >  Don't allow adding/removing list elements from inspector
Don't allow adding/removing list elements from inspector

Time:10-23

How can i code so people can't add/remove list elements from the inspector, but still be able to see the elements?

Something like

[ReadOnlyField, NonReorderable]public List<CustomType> CustomTypeList = new();

CodePudding user response:

It's not possible. Maybe you can try creating a custom editor script just to show them in the inspector.

CodePudding user response:

Sure, you could hack a solution with OnValidate as described here. The general flow would be to read the list of items when the object is enabled and store those in a secondary array. When OnValidate is called, if the list visible in the Inspector doesn’t match the peivate list, then overwrite the elements in the visible list with the private items. Note, you can’t just assign one List object to another. This won’t copy a list, it’ll just make both variables point to the same list. So, the answer is yes, it can be done.

Now, ask me if this is a good idea. The answer would be, no, I don’t see any good reason for this. If you’re worried about a designer, for example, mucking up your list of items, then either move those items elsewhere the designer is told not to touch, hard code the items, or instantiate them if need be.

  • Related