/// <summary> /// Draw the specified sprite. /// </summary> public static void DrawTexture(Texture2D tex, Rect rect, Rect uv, Color color, Material mat) { // Create the texture rectangle that is centered inside rect. Rect outerRect = rect; outerRect.width = tex.width; outerRect.height = tex.height; if (outerRect.width > 0f) { float f = rect.width / outerRect.width; outerRect.width *= f; outerRect.height *= f; } if (rect.height > outerRect.height) { outerRect.y += (rect.height - outerRect.height) * 0.5f; } else if (outerRect.height > rect.height) { float f = rect.height / outerRect.height; outerRect.width *= f; outerRect.height *= f; } if (rect.width > outerRect.width) { outerRect.x += (rect.width - outerRect.width) * 0.5f; } // Draw the background NGUIEditorTools.DrawTiledTexture(outerRect, NGUIEditorTools.backdropTexture); // Draw the sprite GUI.color = color; if (mat == null) { GUI.DrawTextureWithTexCoords(outerRect, tex, uv, true); } else { // NOTE: There is an issue in Unity that prevents it from clipping the drawn preview // using BeginGroup/EndGroup, and there is no way to specify a UV rect... le'suq. UnityEditor.EditorGUI.DrawPreviewTexture(outerRect, tex, mat); } // Draw the lines around the sprite Handles.color = Color.black; Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMin), new Vector3(outerRect.xMin, outerRect.yMax)); Handles.DrawLine(new Vector3(outerRect.xMax, outerRect.yMin), new Vector3(outerRect.xMax, outerRect.yMax)); Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMin), new Vector3(outerRect.xMax, outerRect.yMin)); Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMax), new Vector3(outerRect.xMax, outerRect.yMax)); // Sprite size label string text = string.Format("Texture Size: {0}x{1}", Mathf.RoundToInt(tex.width), Mathf.RoundToInt(tex.height)); EditorGUI.DropShadowLabel(GUILayoutUtility.GetRect(Screen.width, 18f), text); }
/// <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); } }
/// <summary> /// Draw the specified sprite. /// </summary> public static void DrawSprite(Texture2D tex, Rect rect, Rect outer, Rect inner, Rect uv, Color color, Material mat) { // Create the texture rectangle that is centered inside rect. Rect outerRect = rect; outerRect.width = outer.width; outerRect.height = outer.height; if (outerRect.width > 0f) { float f = rect.width / outerRect.width; outerRect.width *= f; outerRect.height *= f; } if (rect.height > outerRect.height) { outerRect.y += (rect.height - outerRect.height) * 0.5f; } else if (outerRect.height > rect.height) { float f = rect.height / outerRect.height; outerRect.width *= f; outerRect.height *= f; } if (rect.width > outerRect.width) { outerRect.x += (rect.width - outerRect.width) * 0.5f; } // Draw the background NGUIEditorTools.DrawTiledTexture(outerRect, NGUIEditorTools.backdropTexture); // Draw the sprite GUI.color = color; if (mat == null) { GUI.DrawTextureWithTexCoords(outerRect, tex, uv, true); } else { // NOTE: There is an issue in Unity that prevents it from clipping the drawn preview // using BeginGroup/EndGroup, and there is no way to specify a UV rect... le'suq. UnityEditor.EditorGUI.DrawPreviewTexture(outerRect, tex, mat); } // Draw the border indicator lines GUI.BeginGroup(outerRect); { tex = NGUIEditorTools.contrastTexture; GUI.color = Color.white; if (inner.xMin != outer.xMin) { float x0 = (inner.xMin - outer.xMin) / outer.width * outerRect.width - 1; NGUIEditorTools.DrawTiledTexture(new Rect(x0, 0f, 1f, outerRect.height), tex); } if (inner.xMax != outer.xMax) { float x1 = (inner.xMax - outer.xMin) / outer.width * outerRect.width - 1; NGUIEditorTools.DrawTiledTexture(new Rect(x1, 0f, 1f, outerRect.height), tex); } if (inner.yMin != outer.yMin) { float y0 = (inner.yMin - outer.yMin) / outer.height * outerRect.height - 1; NGUIEditorTools.DrawTiledTexture(new Rect(0f, y0, outerRect.width, 1f), tex); } if (inner.yMax != outer.yMax) { float y1 = (inner.yMax - outer.yMin) / outer.height * outerRect.height - 1; NGUIEditorTools.DrawTiledTexture(new Rect(0f, y1, outerRect.width, 1f), tex); } } GUI.EndGroup(); // Draw the lines around the sprite Handles.color = Color.black; Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMin), new Vector3(outerRect.xMin, outerRect.yMax)); Handles.DrawLine(new Vector3(outerRect.xMax, outerRect.yMin), new Vector3(outerRect.xMax, outerRect.yMax)); Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMin), new Vector3(outerRect.xMax, outerRect.yMin)); Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMax), new Vector3(outerRect.xMax, outerRect.yMax)); // Sprite size label string text = string.Format("Sprite Size: {0}x{1}", Mathf.RoundToInt(Mathf.Abs(outer.width)), Mathf.RoundToInt(Mathf.Abs(outer.height))); EditorGUI.DropShadowLabel(GUILayoutUtility.GetRect(Screen.width, 18f), text); }
/// <summary> /// Draw the custom wizard. /// </summary> void OnGUI() { EditorGUIUtility.LookLikeControls(80f); if (mAtlas == null) { GUILayout.Label("No Atlas selected.", "LODLevelNotifyText"); } else { GUILayout.Label(mAtlas.name + " Sprites", "LODLevelNotifyText"); NGUIEditorTools.DrawSeparator(); GUILayout.BeginHorizontal(); GUILayout.Space(84f); string before = NGUISettings.partialSprite; string after = EditorGUILayout.TextField("", before, "SearchTextField"); NGUISettings.partialSprite = after; if (GUILayout.Button("", "SearchCancelButton", GUILayout.Width(18f))) { NGUISettings.partialSprite = ""; GUIUtility.keyboardControl = 0; } GUILayout.Space(84f); GUILayout.EndHorizontal(); BetterList <string> sprites = mAtlas.GetListOfSprites(NGUISettings.partialSprite); Texture2D tex = mAtlas.texture as Texture2D; 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); while (offset < sprites.size) { GUILayout.BeginHorizontal(); { int col = 0; rect.x = 10f; for (; offset < sprites.size; ++offset) { UIAtlas.Sprite sprite = mAtlas.GetSprite(sprites[offset]); if (sprite == null) { continue; } // Button comes first if (GUI.Button(rect, "") && spriteName != sprite.name) { if (mSprite != null) { NGUIEditorTools.RegisterUndo("Atlas Selection", mSprite); mSprite.spriteName = sprite.name; mSprite.MakePixelPerfect(); EditorUtility.SetDirty(mSprite.gameObject); } else if (mCallback != null) { mName = sprite.name; mCallback(sprite.name); } } // On top of the button we have a checkboard grid NGUIEditorTools.DrawTiledTexture(rect, NGUIEditorTools.backdropTexture); Rect uv = sprite.outer; if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels) { 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; if (aspect < 1f) { scaleX *= aspect; } else { scaleY /= aspect; } 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 (spriteName == sprite.name) { NGUIEditorTools.DrawOutline(rect, new Color(0.4f, 1f, 0f, 1f)); } if (++col >= columns) { ++offset; break; } rect.x += padded; } } GUILayout.EndHorizontal(); GUILayout.Space(padded); rect.y += padded; } GUILayout.EndScrollView(); } }
void showPackerView() { if (packTex != null) { //================= float h = 0; float w = position.width - 160; float rate = w / packTex.width; if (rate < 1) { h = packTex.height * rate; } else { h = packTex.height; } h = h > 512 ? h : 512; Rect r = new Rect(0, 0, NumEx.getIntPart(w), NumEx.getIntPart(h)); NGUIEditorTools.DrawTiledTexture(r, NGUIEditorTools.backdropTexture); if (isShowParckerTextureBg) { GUI.DrawTexture(r, _empty, ScaleMode.ScaleToFit, false); } GUI.DrawTexture(r, packTex, ScaleMode.ScaleToFit); GUILayout.Space(r.height + r.y); //这句主要目的是为了可以滑动 ECLEditorUtl.BeginContents(); { EditorGUILayout.BeginHorizontal(); { EditorGUILayout.IntField("width", packTex.width); EditorGUILayout.IntField("height", packTex.height); } EditorGUILayout.EndHorizontal(); if (GUILayout.Button("Show/Hide Detail")) { showDeltail = !showDeltail; } if (showDeltail) { if (packSprites != null) { Hashtable m = null; Hashtable d = null; Rect _rect; for (int i = 0; i < packSprites.Count; i++) { _rect = packRects [i]; m = packSprites [i] as Hashtable; d = MapEx.getMap(m, "data"); EditorGUILayout.BeginHorizontal(); { EditorGUILayout.LabelField(MapEx.getString(d, "name")); EditorGUILayout.LabelField(Mathf.RoundToInt(_rect.x) + "x" + Mathf.RoundToInt(_rect.y)); EditorGUILayout.LabelField(Mathf.RoundToInt(_rect.width) + "x" + Mathf.RoundToInt(_rect.height)); } EditorGUILayout.EndHorizontal(); } } } } ECLEditorUtl.EndContents(); } isShowParckerTextureBg = EditorGUILayout.ToggleLeft("Show Background", isShowParckerTextureBg); textureSize = (PackerTextureSize)EditorGUILayout.EnumPopup("", textureSize); isUseUnityPacking = EditorGUILayout.ToggleLeft("UnityPacking", isUseUnityPacking); sortSprite = (SortSprite)EditorGUILayout.EnumPopup("", sortSprite); GUILayout.Space(5); GUI.color = Color.yellow; if (GUILayout.Button("Review Pack Texture")) { if (!packTextures((int)textureSize, isUseUnityPacking)) { Debug.LogError("Some errors happened!"); } } GUI.color = Color.white; GUILayout.Space(10); ECLEditorUtl.BeginContents(); { packedName = EditorGUILayout.TextField("Packed Texture Name", string.IsNullOrEmpty(packedName) ? "Packed" + (int)packerSize : packedName); GUI.color = Color.red; removePublishRes = EditorGUILayout.ToggleLeft("Remove Publish AssetsBundle", removePublishRes); if (GUILayout.Button("Apply Pack Texture")) { applyPackTexture((int)textureSize, isUseUnityPacking); } GUI.color = Color.white; } ECLEditorUtl.EndContents(); }