I am working on a game and I have a database with alot of information. I want to remove de size box where you can set the size of the list because it is all added with script.
I looked all over the internet and I cant find any information about it.
I know this is an old post, but still, there is not really a satisfying answer to me. So first of all, if I understood your question right there are several ways to hide/disable the “size” field. Just in case anyone else will need this in the future:
Solution 1:
private void DisplayArray(SerializedProperty array, string caption=null)
{
EditorGUILayout.LabelField((caption == null) ? array.name : caption);
int size = array.arraySize;
array.Next(true); //generic field (list)
array.Next(true); //arrays size field
{
GUI.enabled = false;
EditorGUILayout.PropertyField(array);
}
for (int i = 0; i < size; i++)
{
array.Next(false); //first array element
EditorGUILayout.PropertyField(array);
}
}
I think you are talking about the readonly field, you want to serialize your data but don’t want it editable. This is my approach. (I get this somewhere in this community but forgot where is it)Create a attribute class like this:
using UnityEngine;
public class ReadOnlyAttribute : PropertyAttribute{ }
Then create an editor class to draw the attribute:
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyAttributeDrawer : PropertyDrawer{
public override float GetPropertyHeight(SerializedProperty property,
GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, true);
}
public override void OnGUI(Rect position,
SerializedProperty property,
GUIContent label)
{
GUI.enabled = false;
EditorGUI.PropertyField(position, property, label, true);
GUI.enabled = true;
}
}
Finally just add this attribute to which field you want to read only.
[ReadOnly]public List<YourType> m_lists;
Edit1: This Solution just make the list’s element readonly, the size still editable (I was wrong) I checking solution for this question.
Edit2: This answer can not solve your question, since someone has commented here so I will keep this answer here. And I will add my opinion:
In case the appearance of this list inside editor not important, I think you can try the solution as @Nikhil12 provided in comment - completely disable it appearance in editor: use [HideInInspector] attribute.
In case this list appearance is important, I think you should write the custom editor yourself. You can take a look here.
And if some one can solve this problem I also want to hearing it. Cheers!!!