//绘制预览界面 public void DrawPreviewEvent() { Event currentEvent = Event.current; EventType type = currentEvent.type; int x = m_cellPadding, y = m_cellPadding; int width = Screen.width - m_cellPadding; int spacingX = cellSize + m_cellPadding; int spacingY = spacingX; if (m_mode == Mode.DetailedMode) { spacingY += 32; } GameObject[] draggeds = draggedObjects; bool isDragging = (draggeds != null); int indexUnderMouse = GetCellUnderMouse(spacingX, spacingY); if (isDragging) { foreach (var gameObject in draggeds) { var result = FindItem(gameObject); if (result != null) { _selections.Add(result); } } } string searchFilter = EditorPrefs.GetString("PrefabWin_SearchFilter", null); if (mReset && type == EventType.Repaint) { mReset = false; foreach (PreviewItem item in mItems) { GeneratePreview(item, false); } RectivateLights(); } bool eligibleToDrag = (currentEvent.mousePosition.y < Screen.height - 40); if (type == EventType.MouseDown) { m_mouseIsInside = true; } else if (type == EventType.MouseDrag) { m_mouseIsInside = true; if (indexUnderMouse != -1 && eligibleToDrag) { if (draggedObjectIsOurs) { DragAndDrop.StartDrag("Prefab Tool"); } currentEvent.Use(); } } else if (type == EventType.MouseUp) { DragAndDrop.PrepareStartDrag(); m_mouseIsInside = false; Repaint(); } else if (type == EventType.DragUpdated) { m_mouseIsInside = true; UpdateVisual(); currentEvent.Use(); } else if (type == EventType.DragPerform) { if (draggeds != null) { if (_selections != null) { foreach (var selection in _selections) { DestroyTexture(selection); mItems.Remove(selection); } } foreach (var dragged in draggeds) { AddItem(dragged, indexUnderMouse); ++indexUnderMouse; } draggeds = null; } m_mouseIsInside = false; currentEvent.Use(); } else if (type == EventType.DragExited || type == EventType.Ignore) { m_mouseIsInside = false; } if (!m_mouseIsInside) { _selections.Clear(); draggeds = null; } BetterList <int> indices = new BetterList <int>(); for (int i = 0; i < mItems.size;) { if (draggeds != null && indices.size == indexUnderMouse) { indices.Add(-1); } var has = _selections.Exists(item => item == mItems[i]); if (!has) { if (string.IsNullOrEmpty(searchFilter) || mItems[i].prefab.name.IndexOf(searchFilter, System.StringComparison.CurrentCultureIgnoreCase) != -1) { indices.Add(i); } } ++i; } if (!indices.Contains(-1)) { indices.Add(-1); } if (eligibleToDrag && type == EventType.MouseDown && indexUnderMouse > -1) { GUIUtility.keyboardControl = 0; if (currentEvent.button == 0 && indexUnderMouse < indices.size) { int index = indices[indexUnderMouse]; if (index != -1 && index < mItems.size) { _selections.Add(mItems[index]); draggedObjects = _selections.Select(item => item.prefab).ToArray(); draggeds = _selections.Select(item => item.prefab).ToArray(); currentEvent.Use(); } } } m_pos = EditorGUILayout.BeginScrollView(m_pos); { Color normal = new Color(1f, 1f, 1f, 0.5f); for (int i = 0; i < indices.size; ++i) { int index = indices[i]; PreviewItem item = (index != -1) ? mItems[index] : _selections.Count == 0 ? null : _selections[0]; if (item != null && item.prefab == null) { mItems.RemoveAt(index); continue; } Rect rect = new Rect(x, y, cellSize, cellSize); Rect inner = rect; inner.xMin += 2f; inner.xMax -= 2f; inner.yMin += 2f; inner.yMax -= 2f; rect.yMax -= 1f; if (!isDragging && (m_mode == Mode.CompactMode || (item == null || item.tex != null))) { m_content.tooltip = (item != null) ? item.prefab.name : "Click to add"; } else { m_content.tooltip = ""; } GUI.color = normal; UIEditorHelper.DrawTiledTexture(inner, UIEditorHelper.backdropTexture); GUI.color = Color.white; GUI.backgroundColor = normal; if (GUI.Button(rect, m_content, "Button")) { if (item == null || currentEvent.button == 0) { string path = EditorUtility.OpenFilePanel("Add a prefab", "", "prefab"); if (!string.IsNullOrEmpty(path)) { PreviewItem newEnt = CreateItemByPath(path); if (newEnt != null) { mItems.Add(newEnt); Save(); } } } else if (currentEvent.button == 1) { ContextMenu.AddItemWithArge("更新预览", false, delegate { GeneratePreview(item, true); }, index); ContextMenu.AddItemWithArge("删除当前", false, RemoveItem, index); ContextMenu.Show(); } } string caption = (item == null) ? "" : item.prefab.name.Replace("Control - ", ""); if (item != null) { if (item.tex == null) { GeneratePreview(item, false); } if (item.tex != null) { GUI.DrawTexture(inner, item.tex); var labelPos = new Rect(inner); var labelStyle = EditorStyles.label; labelPos.height = labelStyle.lineHeight; labelPos.y = inner.height - labelPos.height + 5; labelStyle.fontSize = 16; labelStyle.alignment = TextAnchor.LowerCenter; { GUI.color = Color.black; var name = item.prefab.name.Split('('); if (name.Length == 2) { GUI.Label(rect, name[0] + "\n(" + name[1], labelStyle); } else { GUI.Label(rect, item.prefab.name, labelStyle); } } labelStyle.alignment = TextAnchor.UpperLeft; labelStyle.fontSize = m_labelDefaultFontSize; } else if (m_mode != Mode.DetailedMode) { GUI.Label(inner, caption, m_style); caption = ""; } } else { GUI.Label(inner, "Add", m_style); } if (m_mode == Mode.DetailedMode) { GUI.backgroundColor = new Color(1f, 1f, 1f, 0.5f); GUI.contentColor = new Color(1f, 1f, 1f, 0.7f); GUI.Label(new Rect(rect.x, rect.y + rect.height, rect.width, 32f), caption, "ProgressBarBack"); GUI.contentColor = Color.white; GUI.backgroundColor = Color.white; } x += spacingX; if (x + spacingX > width) { y += spacingY; x = m_cellPadding; } } GUILayout.Space(y + spacingY); } EditorGUILayout.EndScrollView(); GUILayout.BeginHorizontal(); { string after = EditorGUILayout.TextField("", searchFilter, "SearchTextField", GUILayout.Width(Screen.width - 20f)); if (GUILayout.Button("", "SearchCancelButton", GUILayout.Width(18f))) { after = ""; GUIUtility.keyboardControl = 0; } if (searchFilter != after) { EditorPrefs.SetString("PrefabWin_SearchFilter", after); searchFilter = after; } } GUILayout.EndHorizontal(); SizePercent = EditorGUILayout.Slider(SizePercent, 0, 2); }