- Home /
Remove the size box
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.
Thank you in advance
Answer by LeEHil · Apr 26, 2021 at 06:15 AM
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);
}
}
Solution 2:
private void DisplayArray(SerializedProperty array, string caption=null)
{
EditorGUILayout.LabelField((caption == null) ? array.name : caption);
for (int i = 0; i < array.arraySize; i++)
{
EditorGUILayout.PropertyField(array.GetArrayElementAtIndex(i));
}
//other methods:
//array.InsertArrayElementAtIndex;
//array.DeleteArrayElementAtIndex;
//array.MoveArrayElement;
}
}
In the inspector this will look like this:
Now all you'd have to do is:
[CustomEditor(typeof(blablabla))]
public class blablablaEdior : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();
SerializedProperty listProp = serializedObject.FindProperty("list");
DisplayArray(listProp);
serializedObject.ApplyModifiedProperties();
}
}
@JVLVince : solved
You could even add your own context menu:
private void DisplayArray(SerializedProperty array, string caption=null)
{
EditorGUILayout.LabelField((caption == null) ? array.name : caption);
for (int i = 0; i < array.arraySize; i++)
{
EditorGUILayout.PropertyField(array.GetArrayElementAtIndex(i));
//check the current editors EventSystem if there is a right click on this property and display a context menu
Event e = Event.current;
Rect position = GUILayoutUtility.GetLastRect();
if (e.type == EventType.MouseDown && e.button == 1 && position.Contains(e.mousePosition))
{
GenericMenu context = new GenericMenu();
context.AddItem(new GUIContent("Delete"), false, Delete, array.GetArrayElementAtIndex(i));
context.AddItem(new GUIContent("Add"), false, Add, array.GetArrayElementAtIndex(i));
context.ShowAsContext();
}
}
//other methods:
//array.InsertArrayElementAtIndex;
//array.DeleteArrayElementAtIndex;
//array.MoveArrayElement;
}
void Add(object obj)
{
}
void Delete(object obj)
{
}
Answer by JVLVince · Dec 22, 2017 at 08:11 AM
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!!!
I use this attribute, but the input field with the size is still editable. (Only the elements of the list are read-only)
hmm I will check this. $$anonymous$$aybe I was missed something. Tks for let me know :)