private static void RemovedDeletedPrefabFromloadout() { // If the prefab paint mode is the current one in polybrush, // and the prefab that has just been deleted is in the loadout, // Need to remove it from there or error spam will occur PolybrushEditor editor = PolybrushEditor.instance; if (editor == null || editor.tool != BrushTool.Prefab) { return; } BrushModePrefab brushMode = (BrushModePrefab)editor.mode; PrefabLoadoutEditor loadouteditor = brushMode.prefabLoadoutEditor; if (loadouteditor == null) { return; } List <LoadoutInfo> toRemove = new List <LoadoutInfo>(); foreach (LoadoutInfo info in loadouteditor.CurrentLoadout) { if (info.prefab == null) { toRemove.Add(info); } } foreach (LoadoutInfo info in toRemove) { loadouteditor.RemovePrefabFromLoadout(info); } // Clear the list of selected items in the current PrefabPalette // NOTE: This is not ideal, but it's easier to make it this way for now // a solution would be to keep a reference to the deleted items before deleting them // then make a comparison with the new list, to keep selected only the ones that were // not deleted and refresh the indices of the selected list loadouteditor.prefabPaletteEditors[loadouteditor.currentPalette].selected.Clear(); }
/// <summary> /// Draws previews for a prefab in the palette. /// </summary> /// <param name="prefab">Prefab being previewed</param> /// <param name="index">index of the prefab in `prefabs`</param> /// <param name="thumbSize">Size of the preview texture</param> private void DrawPrefabPreview(SerializedProperty prefab, int index, int thumbSize, float x, float y) { Rect r = new Rect(x, y, thumbSize, thumbSize); Rect rightClickZone = new Rect(r); // Texture Preview UnityEngine.Object o = prefab.FindPropertyRelative("gameObject").objectReferenceValue; Texture2D preview = PreviewsDatabase.GetAssetPreview(o); if (selected.Contains(index)) { Rect r2 = new Rect(r); r2.x -= 1; r2.y -= 1; r2.width += 2; r2.height += 2; EditorGUI.DrawRect(r2, Color.blue); } EditorGUI.DrawPreviewTexture(r, preview); // Those numbers were obtained by empirical experimentation r.x += thumbSize - 17; r.y += thumbSize - 17; r.width = 17; r.height = 17; LoadoutInfo li = new LoadoutInfo(target as PrefabPalette, index); bool isLoaded = loadoutEditor.ContainsPrefab(li); Event e = Event.current; bool rightClick = (e.type == EventType.MouseDown || e.type == EventType.ContextClick) && rightClickZone.Contains(e.mousePosition) && e.button == 1; bool b1 = GUI.Toggle(r, isLoaded, ""); // Reducing the width by 1 to ensure the button is not larger than the thumbnail. // Otherwise button is slightly too large and horizontal scrollbar may appear. bool b2 = GUILayout.Button("", GUIStyle.none, GUILayout.Width(thumbSize - 1), GUILayout.Height(thumbSize)); // Set the focus to nothing in case the user want to press delete or backspace key // I dont know why but If we don't do that the Textfield with the name of prefab settings never looses focus if (b2 || rightClick) { GUI.FocusControl(null); e.Use(); } if (rightClick) { rightClickTime = redrawCounter; shouldopencontextmenu = true; idx = index; if (!selected.Contains(index)) { selected.Clear(); selected.Add(index); } return; } else if (shouldopencontextmenu && redrawCounter > rightClickTime) { loadoutEditor.OpenCopyPasteMenu(new LoadoutInfo(target as PrefabPalette, idx), selected); shouldopencontextmenu = false; idx = -1; // reset the redraw counter to avoid overflow redrawCounter = 0; } if (b1 && !isLoaded) { loadoutEditor.AddPrefabInLoadout(li); //loadoutEditor.loadouts.Add(li); } else if (!b1 && isLoaded) { loadoutEditor.RemovePrefabFromLoadout(li); //loadoutEditor.loadouts.Remove(li); } else if (b2) { if (Event.current.shift || Event.current.control) { if (!selected.Add(index)) { selected.Remove(index); } } else { if (selected.Count == 1 && selected.Contains(index)) { selected.Remove(index); } else { selected.Clear(); selected.Add(index); } } if (onSelectionChanged != null) { onSelectionChanged(selected); } GUI.changed = true; } }