//生成一个预览图 void GeneratePreview(PreviewItem item, bool isReCreate = true) { if (item == null || item.prefab == null) { return; } { string preview_path = Configure.ResAssetsPath + "/Preview/" + item.prefab.name + ".png"; if (!isReCreate && File.Exists(preview_path)) { Texture texture = UIEditorHelper.LoadTextureInLocal(preview_path); item.tex = texture; } else { Texture Tex = UIEditorHelper.GetAssetPreview(item.prefab); if (Tex != null) { DestroyTexture(item); item.tex = Tex; UIEditorHelper.SaveTextureToPNG(Tex, preview_path); } } item.dynamicTex = false; return; } }
PreviewItem AddGUID(string guid, int index) { GameObject go = UIEditorHelper.GUIDToObject <GameObject>(guid); if (go != null) { PreviewItem ent = new PreviewItem(); ent.prefab = go; ent.guid = guid; GeneratePreview(ent, false); if (index < mItems.size) { if (!IsInList(mItems, ent)) { mItems.Insert(index, ent); } } else { if (!IsInList(mItems, ent)) { mItems.Add(ent); } } return(ent); } return(null); }
//删除一个Item的图片信息 void DestroyTexture(PreviewItem item) { if (item != null && item.dynamicTex && item.tex != null) { DestroyImmediate(item.tex); item.dynamicTex = false; item.tex = null; } }
bool IsInList(BetterList <PreviewItem> betterList, PreviewItem previewItem) { for (int i = 0; i < betterList.size; i++) { if (betterList[i].guid == previewItem.guid) { return(true); } } return(false); }
void RemoveItem(object obj) { if (this == null) { return; } int index = (int)obj; if (index < mItems.size && index > -1) { PreviewItem item = mItems[index]; DestroyTexture(item); mItems.RemoveAt(index); } Save(); }
void AddItem(GameObject go, int index) { string guid = UIEditorHelper.ObjectToGUID(go); if (string.IsNullOrEmpty(guid)) { string path = EditorUtility.SaveFilePanelInProject("Save a prefab", go.name + ".prefab", "prefab", "Save prefab as...", ""); if (string.IsNullOrEmpty(path)) { return; } go = PrefabUtility.CreatePrefab(path, go); if (go == null) { return; } guid = UIEditorHelper.ObjectToGUID(go); if (string.IsNullOrEmpty(guid)) { return; } } PreviewItem ent = new PreviewItem(); ent.prefab = go; ent.guid = guid; GeneratePreview(ent); RectivateLights(); if (index < mItems.size) { mItems.Insert(index, ent); } else { mItems.Add(ent); } Save(); }
//每次完成拖拽 都会创建出来一个新的Obj PreviewItem CreateItemByPath(string path) { if (!string.IsNullOrEmpty(path)) { path = FileUtil.GetProjectRelativePath(path); string guid = AssetDatabase.AssetPathToGUID(path); if (!string.IsNullOrEmpty(guid)) { GameObject go = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject; PreviewItem ent = new PreviewItem(); ent.prefab = go; ent.guid = guid; GeneratePreview(ent); return(ent); } else { Debug.Log("No GUID"); } } return(null); }
//绘制预览界面 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); }