public TileList(TesseraGenerator generator, string title, SerializedProperty list, bool allowSingleTile = true, bool allowCheckboxes = true, bool allowList = true) { this.generator = generator; this.title = title; this.list = list; this.allowSingleTile = allowSingleTile; this.allowCheckboxes = allowCheckboxes; this.allowList = allowList; // Pick display type if (SingleTileEnabled) { displayType = TileListDisplayType.SingleTile; } else if (CheckboxesEnabled) { displayType = TileListDisplayType.Checkboxes; } else { displayType = TileListDisplayType.List; } }
public void Draw() { var set = new HashSet <TesseraTile>(); for (int i = 0; i < list.arraySize; i++) { set.Add((TesseraTile)list.GetArrayElementAtIndex(i).objectReferenceValue); } var t = title; if (displayType == TileListDisplayType.SingleTile) { t = t.TrimEnd('s'); } EditorGUILayout.PropertyField(list, new GUIContent(t), false); if (list.isExpanded) { int oldIndentLevel = EditorGUI.indentLevel; EditorGUI.indentLevel++; var optionStrings = new List <string>(); var optionValues = new List <TileListDisplayType>(); int selectedIndex = -1; void Add(string s, TileListDisplayType value) { if (displayType == value) { selectedIndex = optionValues.Count; } optionStrings.Add(s); optionValues.Add(value); } if (SingleTileEnabled) { Add("Single Tile", TileListDisplayType.SingleTile); } if (CheckboxesEnabled) { Add("Checkboxes", TileListDisplayType.Checkboxes); } if (ListEnabled) { Add("List", TileListDisplayType.List); } //displayType = (TileListDisplayType)EditorGUILayout.EnumPopup(new GUIContent(), displayType, Enabled, true); if (optionStrings.Count <= 1) { selectedIndex = 0; } else { selectedIndex = EditorGUILayout.Popup(selectedIndex, optionStrings.ToArray()); } displayType = optionValues[selectedIndex]; switch (displayType) { case TileListDisplayType.SingleTile: { var current = list.arraySize == 0 ? null : list.GetArrayElementAtIndex(0).objectReferenceValue; var next = EditorGUILayout.ObjectField(current, typeof(TesseraTile), true); if (next == null) { list.arraySize = 0; } else { list.arraySize = 1; list.GetArrayElementAtIndex(0).objectReferenceValue = next; } break; } case TileListDisplayType.Checkboxes: foreach (var tileEntry in generator.tiles) { var tile = tileEntry.tile; var current = set.Contains(tile); if (tile == null) { continue; } var newValue = EditorGUILayout.Toggle(tile.name, current); if (newValue && !current) { list.arraySize += 1; list.GetArrayElementAtIndex(list.arraySize - 1).objectReferenceValue = tile; } if (!newValue && current) { for (int i = 0; i < list.arraySize; i++) { if (list.GetArrayElementAtIndex(i).objectReferenceValue == tile) { // https://answers.unity.com/questions/555724/serializedpropertydeletearrayelementatindex-leaves.html list.GetArrayElementAtIndex(i).objectReferenceValue = null; list.DeleteArrayElementAtIndex(i); break; } } } } break; case TileListDisplayType.List: EditorGUILayout.PropertyField(list.FindPropertyRelative("Array.size")); for (int i = 0; i < list.arraySize; i++) { EditorGUILayout.PropertyField(list.GetArrayElementAtIndex(i)); } break; } EditorGUI.indentLevel = oldIndentLevel; } }