/// <summary> /// Draw the custom wizard. /// </summary> void OnGUI() { NGUIEditorTools.SetLabelWidth(80f); if (NGUISettings.atlas == null) { GUILayout.Label("No Atlas selected.", "LODLevelNotifyText"); } else { UIAtlas atlas = NGUISettings.atlas; bool close = false; GUILayout.Label(atlas.name + " Sprites", "LODLevelNotifyText"); NGUIEditorTools.DrawSeparator(); GUILayout.BeginHorizontal(); GUILayout.Space(84f); string before = NGUISettings.partialSprite; string after = EditorGUILayout.TextField("", before, "SearchTextField"); if (before != after) { NGUISettings.partialSprite = after; } if (GUILayout.Button("", "SearchCancelButton", GUILayout.Width(18f))) { NGUISettings.partialSprite = ""; GUIUtility.keyboardControl = 0; } GUILayout.Space(84f); GUILayout.EndHorizontal(); Texture2D tex = atlas.texture as Texture2D; if (tex == null) { GUILayout.Label("The atlas doesn't have a texture to work with"); return; } BetterList <string> sprites = atlas.GetListOfSprites(NGUISettings.partialSprite); float size = 80f; float padded = size + 10f; int columns = Mathf.FloorToInt(Screen.width / padded); if (columns < 1) { columns = 1; } int offset = 0; Rect rect = new Rect(10f, 0, size, size); GUILayout.Space(10f); mPos = GUILayout.BeginScrollView(mPos); int rows = 1; while (offset < sprites.size) { GUILayout.BeginHorizontal(); { int col = 0; rect.x = 10f; for (; offset < sprites.size; ++offset) { UISpriteData sprite = atlas.GetSprite(sprites[offset]); if (sprite == null) { continue; } // Button comes first if (GUI.Button(rect, "")) { if (Event.current.button == 0) { float delta = Time.realtimeSinceStartup - mClickTime; mClickTime = Time.realtimeSinceStartup; if (NGUISettings.selectedSprite != sprite.name) { if (mSprite != null) { NGUIEditorTools.RegisterUndo("Atlas Selection", mSprite); mSprite.MakePixelPerfect(); EditorUtility.SetDirty(mSprite.gameObject); } NGUISettings.selectedSprite = sprite.name; NGUIEditorTools.RepaintSprites(); if (mCallback != null) { mCallback(sprite.name); } } else if (delta < 0.5f) { close = true; } } else { NGUIContextMenu.AddItem("Edit", false, EditSprite, sprite); NGUIContextMenu.AddItem("Delete", false, DeleteSprite, sprite); NGUIContextMenu.Show(); } } if (Event.current.type == EventType.Repaint) { // On top of the button we have a checkboard grid NGUIEditorTools.DrawTiledTexture(rect, NGUIEditorTools.backdropTexture); Rect uv = new Rect(sprite.x, sprite.y, sprite.width, sprite.height); uv = NGUIMath.ConvertToTexCoords(uv, tex.width, tex.height); // Calculate the texture's scale that's needed to display the sprite in the clipped area float scaleX = rect.width / uv.width; float scaleY = rect.height / uv.height; // Stretch the sprite so that it will appear proper float aspect = (scaleY / scaleX) / ((float)tex.height / tex.width); Rect clipRect = rect; if (aspect != 1f) { if (aspect < 1f) { // The sprite is taller than it is wider float padding = size * (1f - aspect) * 0.5f; clipRect.xMin += padding; clipRect.xMax -= padding; } else { // The sprite is wider than it is taller float padding = size * (1f - 1f / aspect) * 0.5f; clipRect.yMin += padding; clipRect.yMax -= padding; } } GUI.DrawTextureWithTexCoords(clipRect, tex, uv); // Draw the selection if (NGUISettings.selectedSprite == sprite.name) { NGUIEditorTools.DrawOutline(rect, new Color(0.4f, 1f, 0f, 1f)); } } 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), sprite.name, "ProgressBarBack"); GUI.contentColor = Color.white; GUI.backgroundColor = Color.white; if (++col >= columns) { ++offset; break; } rect.x += padded; } } GUILayout.EndHorizontal(); GUILayout.Space(padded); rect.y += padded + 26; ++rows; } GUILayout.Space(rows * 26); GUILayout.EndScrollView(); if (close) { Close(); } } }
/// <summary> /// Draw the custom wizard. /// </summary> void OnGUI() { Event currentEvent = Event.current; EventType type = currentEvent.type; int x = cellPadding, y = cellPadding; int width = Screen.width - cellPadding; int spacingX = cellSize + cellPadding; int spacingY = spacingX; if (mMode == Mode.DetailedMode) { spacingY += 32; } GameObject dragged = draggedObject; bool isDragging = (dragged != null); int indexUnderMouse = GetCellUnderMouse(spacingX, spacingY); Item selection = isDragging ? FindItem(dragged) : null; string searchFilter = NGUISettings.searchField; int newTab = mTab; GUILayout.BeginHorizontal(); if (GUILayout.Toggle(newTab == 0, "1", "ButtonLeft")) { newTab = 0; } if (GUILayout.Toggle(newTab == 1, "2", "ButtonMid")) { newTab = 1; } if (GUILayout.Toggle(newTab == 2, "3", "ButtonMid")) { newTab = 2; } if (GUILayout.Toggle(newTab == 3, "4", "ButtonMid")) { newTab = 3; } if (GUILayout.Toggle(newTab == 4, "5", "ButtonRight")) { newTab = 4; } GUILayout.EndHorizontal(); if (mTab != newTab) { Save(); mTab = newTab; mReset = true; NGUISettings.SetInt("NGUI Prefab Tab", mTab); Load(); } if (mReset && type == EventType.Repaint) { mReset = false; foreach (Item item in mItems) { GeneratePreview(item, null); } RectivateLights(); } // Search field 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) { NGUISettings.searchField = after; searchFilter = after; } } GUILayout.EndHorizontal(); bool eligibleToDrag = (currentEvent.mousePosition.y < Screen.height - 40); if (type == EventType.MouseDown) { mMouseIsInside = true; } else if (type == EventType.MouseDrag) { mMouseIsInside = true; if (indexUnderMouse != -1 && eligibleToDrag) { // Drag operation begins if (draggedObjectIsOurs) { DragAndDrop.StartDrag("Prefab Tool"); } currentEvent.Use(); } } else if (type == EventType.MouseUp) { DragAndDrop.PrepareStartDrag(); mMouseIsInside = false; Repaint(); } else if (type == EventType.DragUpdated) { // Something dragged into the window mMouseIsInside = true; UpdateVisual(); currentEvent.Use(); } else if (type == EventType.DragPerform) { // We've dropped a new object into the window if (dragged != null) { if (selection != null) { DestroyTexture(selection); mItems.Remove(selection); } AddItem(dragged, indexUnderMouse); draggedObject = null; } mMouseIsInside = false; currentEvent.Use(); } else if (type == EventType.DragExited || type == EventType.Ignore) { mMouseIsInside = false; } // If the mouse is not inside the window, clear the selection and dragged object if (!mMouseIsInside) { selection = null; dragged = null; } // Create a list of indices, inserting an entry of '-1' underneath the dragged object BetterList <int> indices = new BetterList <int>(); for (int i = 0; i < mItems.size;) { if (dragged != null && indices.size == indexUnderMouse) { indices.Add(-1); } if (mItems[i] != selection) { if (string.IsNullOrEmpty(searchFilter) || mItems[i].prefab.name.IndexOf(searchFilter, System.StringComparison.CurrentCultureIgnoreCase) != -1) { indices.Add(i); } } ++i; } // There must always be '-1' (Add/Move slot) present if (!indices.Contains(-1)) { indices.Add(-1); } // We want to start dragging something from within the window 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) { selection = mItems[index]; draggedObject = selection.prefab; dragged = selection.prefab; currentEvent.Use(); } } } //else if (type == EventType.MouseUp && currentEvent.button == 1 && indexUnderMouse > mItems.size) //{ // NGUIContextMenu.AddItem("Reset", false, RemoveItem, index); // NGUIContextMenu.Show(); //} // Draw the scroll view with prefabs mPos = GUILayout.BeginScrollView(mPos); { Color normal = new Color(1f, 1f, 1f, 0.5f); for (int i = 0; i < indices.size; ++i) { int index = indices[i]; Item ent = (index != -1) ? mItems[index] : selection; if (ent != null && ent.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; // Button seems to be mis-shaped. It's height is larger than its width by a single pixel. if (!isDragging && (mMode == Mode.CompactMode || (ent == null || ent.tex != null))) { mContent.tooltip = (ent != null) ? ent.prefab.name : "Click to add"; } else { mContent.tooltip = ""; } //if (ent == selection) { GUI.color = normal; NGUIEditorTools.DrawTiledTexture(inner, NGUIEditorTools.backdropTexture); } GUI.color = Color.white; GUI.backgroundColor = normal; if (GUI.Button(rect, mContent, "Button")) { if (ent == null || currentEvent.button == 0) { string path = EditorUtility.OpenFilePanel("Add a prefab", NGUISettings.currentPath, "prefab"); if (!string.IsNullOrEmpty(path)) { NGUISettings.currentPath = System.IO.Path.GetDirectoryName(path); Item newEnt = CreateItemByPath(path); if (newEnt != null) { mItems.Add(newEnt); Save(); } } } else if (currentEvent.button == 1) { NGUIContextMenu.AddItem("Delete", false, RemoveItem, index); NGUIContextMenu.Show(); } } string caption = (ent == null) ? "" : ent.prefab.name.Replace("Control - ", ""); if (ent != null) { if (ent.tex != null) { GUI.DrawTexture(inner, ent.tex); } else if (mMode != Mode.DetailedMode) { GUI.Label(inner, caption, mStyle); caption = ""; } } else { GUI.Label(inner, "Add", mStyle); } if (mMode == 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 = cellPadding; } } GUILayout.Space(y); } GUILayout.EndScrollView(); // Mode Mode modeAfter = (Mode)EditorGUILayout.EnumPopup(mMode); if (modeAfter != mMode) { mMode = modeAfter; mReset = true; NGUISettings.SetEnum("NGUI Prefab Mode", mMode); } }