- Home /
ReorderableList always show all element. How to hide some element ?
UnityEditorInternal.ReorderableList this is useful in Editor to display Array. But i have a problem when want to display element a part of SerializedProperty(isArray). Sometime, i have a large list,but i need to edit the a part of list (may be only several element), so i do not to display all element of the list. I try to look up API and other infomation,but i don't get solution to the problem. so, i test to use remove should not to display element and DoLayoutList() again. then add remove data agins,you know if i remove from SerializedProperty the data will lose. the result is fine. the Code like below:
List<string> cache = new List<string>();
//get not display element to cache.
for (int i = 10; i < mySerializedProperty.arraySize; ++i)
{
cache.Add(mySerializedProperty.GetArrayElementAtIndex(i).stringValue);
}
//remove not display element from SerializedProperty array
for (int i = 10; i < mySerializedProperty.arraySize; ++i)
{
mySerializedProperty.DeleteArrayElementAtIndex(i);
}
//Refresh
_injectionTypeReorderableList.DoLayoutList();
//Add SerializedProperty array size and assign from cache to SerializedProperty array element.
for (int i = 0; i < cache.Count; ++i)
{
mySerializedProperty.arraySize++;
mySerializedProperty.GetArrayElementAtIndex(mySerializedProperty.arraySize - 1).stringValue = cache[i];
}
However,as you can see, this way not good, it need remove and add again.if i have large element need to hide and show when at the right time, this way will be nightmare. the ReorderableList have other good way to handle this problem ?
Answer by AnC_CN · Sep 21, 2017 at 06:46 PM
I find answer with mix reflection and SerializedProperty First need separate display list and actual SerializedProperty. Then, you need to create display list copy from SerializedProperty(is array ). Because the reason of :
1.add to cache list
2.remove from SerializedProperty(is array ).
3.refresh
4.add data from cache list to SerializedProperty(is array ).
this way will remove data from array(at step 2) and add data( at step 4) one more time. So we need to create display list copy from SerializedProperty(is array ) only to display. the display list remove and add not affect SerializedProperty(is array ).only change value will be save.
In general,have 3 ways,1. like above; 2. directly use actual class type; 3. by reflection
when created display list: revalue ReorderableList. the code like below:
protected void RefreshReorderableList()
{
HashSet<int> hideElement = new HashSet<int>() { 1, 2, 3, 4 };
List<SerializedProperty> displayList = new List<SerializedProperty>();
for (int i = 0; i < hideElement.Count; ++i)
{
if (hideElement.Contains(i))
continue;
displayList.Add(_luaInjectionDataList.GetArrayElementAtIndex(i));
}
//Now we had a display list and the list add and remove not affect source SerializedProperty.
_reorderableList.list = displayList;
_reorderableList.DoLayoutList();
}
}
And now Step below:
1.create display list
2.refresh
Because display list not affect source serializedProperty, And display list element is ref of serializedProperty element, so the change of element content in display list will be save too.
However, this is trouble too. That is you need to define custom Add,Remove delegate and so on, for correct change source serializedProperty when display list remove,add. I think may be have another way.
Your answer
Follow this Question
Related Questions
Unity Editor SerializedProperty how to assign to array with larger data directly. 1 Answer
A node in a childnode? 1 Answer
when i build run for IOS then it shows error 0 Answers
PlayerPrefs not saving on Windows Devices but saving on Unity Editor and Android. 0 Answers
Hide GUI objects when going to the scene 0 Answers