override protected void OnDrawTexture() { Texture2D tex = mWidget.mainTexture as Texture2D; if (tex != null) { // Draw the atlas EditorGUILayout.Separator(); // Draw the texture and its UV outline bool isSmall = (mTex.uvRect.width < 0.25f && mTex.uvRect.height < 0.25f); Rect spriteRect = NGUIEditorTools.DrawSprite(tex, isSmall ? mTex.uvRect : new Rect(0f, 0f, 1f, 1f), null, isSmall); NGUIEditorTools.DrawOutline(spriteRect, mTex.uvRect, Color.green); // Sprite size label Rect rect = GUILayoutUtility.GetRect(Screen.width, 18f); EditorGUI.DropShadowLabel(rect, "Texture Size: " + tex.width + "x" + tex.height); } }
/// <summary> /// Draw the sprite texture. /// </summary> override protected void OnDrawTexture() { Texture2D tex = mSprite.mainTexture as Texture2D; if (tex != null) { // Draw the atlas EditorGUILayout.Separator(); Rect rect = NGUIEditorTools.DrawSprite(tex, mSprite.outerUV, mUseShader ? mSprite.atlas.spriteMaterial : null); // Draw the selection NGUIEditorTools.DrawOutline(rect, mSprite.outerUV, new Color(0.4f, 1f, 0f, 1f)); // Sprite size label string text = "Sprite Size: "; text += Mathf.RoundToInt(Mathf.Abs(mSprite.outerUV.width * tex.width)); text += "x"; text += Mathf.RoundToInt(Mathf.Abs(mSprite.outerUV.height * tex.height)); rect = GUILayoutUtility.GetRect(Screen.width, 18f); EditorGUI.DropShadowLabel(rect, 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 inspector widget. /// </summary> public override void OnInspectorGUI() { EditorGUIUtility.LookLikeControls(80f); mAtlas = target as UIAtlas; NGUIEditorTools.DrawSeparator(); if (mAtlas.replacement != null) { mType = AtlasType.Reference; mReplacement = mAtlas.replacement as UIAtlas; } AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType); if (mType != after) { if (after == AtlasType.Normal) { OnSelectAtlas(null); } else { mType = AtlasType.Reference; } } if (mType == AtlasType.Reference) { ComponentSelector.Draw <UIAtlas>(mAtlas.replacement as UIAtlas, OnSelectAtlas); NGUIEditorTools.DrawSeparator(); GUILayout.Label("You can have one atlas simply point to\n" + "another one. This is useful if you want to be\n" + "able to quickly replace the contents of one\n" + "atlas with another one, for example for\n" + "swapping an SD atlas with an HD one, or\n" + "replacing an English atlas with a Chinese\n" + "one. All the sprites referencing this atlas\n" + "will update their references to the new one."); if (mReplacement != mAtlas && mAtlas.replacement != mReplacement) { NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas); mAtlas.replacement = mReplacement; UnityEditor.EditorUtility.SetDirty(mAtlas); } return; } if (!mConfirmDelete) { NGUIEditorTools.DrawSeparator(); Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material; if (mAtlas.spriteMaterial != mat) { NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas); mAtlas.spriteMaterial = mat; // Ensure that this atlas has valid import settings if (mAtlas.texture != null) { NGUIEditorTools.ImportTexture(mAtlas.texture, false, false); } mAtlas.MarkAsDirty(); mConfirmDelete = false; } if (mat != null) { TextAsset ta = EditorGUILayout.ObjectField("TP Import", null, typeof(TextAsset), false) as TextAsset; if (ta != null) { // Ensure that this atlas has valid import settings if (mAtlas.texture != null) { NGUIEditorTools.ImportTexture(mAtlas.texture, false, false); } NGUIEditorTools.RegisterUndo("Import Sprites", mAtlas); NGUIJson.LoadSpriteData(mAtlas, ta); if (mSprite != null) { mSprite = mAtlas.GetSprite(mSprite.name); } mAtlas.MarkAsDirty(); } UIAtlas.Coordinates coords = (UIAtlas.Coordinates)EditorGUILayout.EnumPopup("Coordinates", mAtlas.coordinates); if (coords != mAtlas.coordinates) { NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas); mAtlas.coordinates = coords; mConfirmDelete = false; } float pixelSize = EditorGUILayout.FloatField("Pixel Size", mAtlas.pixelSize); if (pixelSize != mAtlas.pixelSize) { NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas); mAtlas.pixelSize = pixelSize; mConfirmDelete = false; } } } if (mAtlas.spriteMaterial != null) { Color blue = new Color(0f, 0.7f, 1f, 1f); Color green = new Color(0.4f, 1f, 0f, 1f); if (mConfirmDelete) { if (mSprite != null) { // Show the confirmation dialog NGUIEditorTools.DrawSeparator(); GUILayout.Label("Are you sure you want to delete '" + mSprite.name + "'?"); NGUIEditorTools.DrawSeparator(); GUILayout.BeginHorizontal(); { GUI.backgroundColor = Color.green; if (GUILayout.Button("Cancel")) { mConfirmDelete = false; } GUI.backgroundColor = Color.red; if (GUILayout.Button("Delete")) { NGUIEditorTools.RegisterUndo("Delete Sprite", mAtlas); mAtlas.spriteList.Remove(mSprite); mConfirmDelete = false; } GUI.backgroundColor = Color.white; } GUILayout.EndHorizontal(); } else { mConfirmDelete = false; } } else { if (mSprite == null && mAtlas.spriteList.Count > 0) { string spriteName = EditorPrefs.GetString("NGUI Selected Sprite"); if (!string.IsNullOrEmpty(spriteName)) { mSprite = mAtlas.GetSprite(spriteName); } if (mSprite == null) { mSprite = mAtlas.spriteList[0]; } } GUI.backgroundColor = Color.green; GUILayout.BeginHorizontal(); { EditorGUILayout.PrefixLabel("Add/Delete"); if (GUILayout.Button("New Sprite")) { NGUIEditorTools.RegisterUndo("Add Sprite", mAtlas); UIAtlas.Sprite newSprite = new UIAtlas.Sprite(); if (mSprite != null) { newSprite.name = "Copy of " + mSprite.name; newSprite.outer = mSprite.outer; newSprite.inner = mSprite.inner; } else { newSprite.name = "New Sprite"; } mAtlas.spriteList.Add(newSprite); mSprite = newSprite; } // Show the delete button GUI.backgroundColor = Color.red; if (mSprite != null && GUILayout.Button("Delete", GUILayout.Width(55f))) { mConfirmDelete = true; } GUI.backgroundColor = Color.white; } GUILayout.EndHorizontal(); if (!mConfirmDelete && mSprite != null) { NGUIEditorTools.DrawSeparator(); string spriteName = UISpriteInspector.SpriteField(mAtlas, mSprite.name); if (spriteName != mSprite.name) { mSprite = mAtlas.GetSprite(spriteName); EditorPrefs.SetString("NGUI Selected Sprite", spriteName); } if (mSprite == null) { return; } Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D; if (tex != null) { Rect inner = mSprite.inner; Rect outer = mSprite.outer; string name = EditorGUILayout.TextField("Edit Name", mSprite.name); if (mSprite.name != name && !string.IsNullOrEmpty(name)) { bool found = false; foreach (UIAtlas.Sprite sp in mAtlas.spriteList) { if (sp.name == name) { found = true; break; } } if (!found) { NGUIEditorTools.RegisterUndo("Edit Sprite Name", mAtlas); mSprite.name = name; } } if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels) { GUI.backgroundColor = green; outer = NGUIEditorTools.IntRect("Dimensions", mSprite.outer); Vector4 border = new Vector4( mSprite.inner.xMin - mSprite.outer.xMin, mSprite.inner.yMin - mSprite.outer.yMin, mSprite.outer.xMax - mSprite.inner.xMax, mSprite.outer.yMax - mSprite.inner.yMax); GUI.backgroundColor = blue; border = NGUIEditorTools.IntPadding("Border", border); GUI.backgroundColor = Color.white; inner.xMin = mSprite.outer.xMin + border.x; inner.yMin = mSprite.outer.yMin + border.y; inner.xMax = mSprite.outer.xMax - border.z; inner.yMax = mSprite.outer.yMax - border.w; } else { // Draw the inner and outer rectangle dimensions GUI.backgroundColor = green; outer = EditorGUILayout.RectField("Outer Rect", mSprite.outer); GUI.backgroundColor = blue; inner = EditorGUILayout.RectField("Inner Rect", mSprite.inner); GUI.backgroundColor = Color.white; } if (outer.xMax < outer.xMin) { outer.xMax = outer.xMin; } if (outer.yMax < outer.yMin) { outer.yMax = outer.yMin; } if (outer != mSprite.outer) { float x = outer.xMin - mSprite.outer.xMin; float y = outer.yMin - mSprite.outer.yMin; inner.x += x; inner.y += y; } // Sanity checks to ensure that the inner rect is always inside the outer inner.xMin = Mathf.Clamp(inner.xMin, outer.xMin, outer.xMax); inner.xMax = Mathf.Clamp(inner.xMax, outer.xMin, outer.xMax); inner.yMin = Mathf.Clamp(inner.yMin, outer.yMin, outer.yMax); inner.yMax = Mathf.Clamp(inner.yMax, outer.yMin, outer.yMax); EditorGUILayout.Separator(); // Padding is mainly meant to be used by the 'trimmed' feature of TexturePacker if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels) { int left = Mathf.RoundToInt(mSprite.paddingLeft * mSprite.outer.width); int right = Mathf.RoundToInt(mSprite.paddingRight * mSprite.outer.width); int top = Mathf.RoundToInt(mSprite.paddingTop * mSprite.outer.height); int bottom = Mathf.RoundToInt(mSprite.paddingBottom * mSprite.outer.height); NGUIEditorTools.IntVector a = NGUIEditorTools.IntPair("Padding", "Left", "Top", left, top); NGUIEditorTools.IntVector b = NGUIEditorTools.IntPair(null, "Right", "Bottom", right, bottom); if (a.x != left || a.y != top || b.x != right || b.y != bottom) { NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas); mSprite.paddingLeft = a.x / mSprite.outer.width; mSprite.paddingTop = a.y / mSprite.outer.width; mSprite.paddingRight = b.x / mSprite.outer.height; mSprite.paddingBottom = b.y / mSprite.outer.height; MarkSpriteAsDirty(); } } else { // Create a button that can make the coordinates pixel-perfect on click GUILayout.BeginHorizontal(); { GUILayout.Label("Correction", GUILayout.Width(75f)); Rect corrected0 = outer; Rect corrected1 = inner; if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels) { corrected0 = NGUIMath.MakePixelPerfect(corrected0); corrected1 = NGUIMath.MakePixelPerfect(corrected1); } else { corrected0 = NGUIMath.MakePixelPerfect(corrected0, tex.width, tex.height); corrected1 = NGUIMath.MakePixelPerfect(corrected1, tex.width, tex.height); } if (corrected0 == mSprite.outer && corrected1 == mSprite.inner) { GUI.color = Color.grey; GUILayout.Button("Make Pixel-Perfect"); GUI.color = Color.white; } else if (GUILayout.Button("Make Pixel-Perfect")) { outer = corrected0; inner = corrected1; GUI.changed = true; } } GUILayout.EndHorizontal(); } GUILayout.BeginHorizontal(); { mView = (View)EditorGUILayout.EnumPopup("Show", mView); GUILayout.Label("Shader", GUILayout.Width(45f)); if (mUseShader != EditorGUILayout.Toggle(mUseShader, GUILayout.Width(20f))) { mUseShader = !mUseShader; if (mUseShader && mView == View.Sprite) { // TODO: Remove this when Unity fixes the bug with DrawPreviewTexture not being affected by BeginGroup Debug.LogWarning("There is a bug in Unity that prevents the texture from getting clipped properly.\n" + "Until it's fixed by Unity, your texture may spill onto the rest of the Unity's GUI while using this mode."); } } } GUILayout.EndHorizontal(); if (mSprite.outer != outer || mSprite.inner != inner) { NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas); mSprite.outer = outer; mSprite.inner = inner; mConfirmDelete = false; MarkSpriteAsDirty(); } Rect uv0 = outer; Rect uv1 = inner; if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels) { uv0 = NGUIMath.ConvertToTexCoords(uv0, tex.width, tex.height); uv1 = NGUIMath.ConvertToTexCoords(uv1, tex.width, tex.height); } // Draw the atlas EditorGUILayout.Separator(); Material m = mUseShader ? mAtlas.spriteMaterial : null; Rect rect = (mView == View.Atlas) ? NGUIEditorTools.DrawAtlas(tex, m) : NGUIEditorTools.DrawSprite(tex, uv0, m); // Draw the sprite outline NGUIEditorTools.DrawOutline(rect, uv0, uv1); EditorGUILayout.Separator(); } } } } }
/// <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(); } }
override public void OnInspectorGUI() { mFont = target as UIFont; EditorGUIUtility.LookLikeControls(80f); NGUIEditorTools.DrawSeparator(); if (mFont.replacement != null) { mType = FontType.Reference; mReplacement = mFont.replacement as UIFont; } FontType after = (FontType)EditorGUILayout.EnumPopup("Font Type", mType); if (mType != after) { if (after == FontType.Normal) { OnSelectFont(null); } else { mType = FontType.Reference; } } if (mType == FontType.Reference) { ComponentSelector.Draw <UIFont>(mFont.replacement as UIFont, OnSelectFont); NGUIEditorTools.DrawSeparator(); GUILayout.Label("You can have one font simply point to\n" + "another one. This is useful if you want to be\n" + "able to quickly replace the contents of one\n" + "font with another one, for example for\n" + "swapping an SD font with an HD one, or\n" + "replacing an English font with a Chinese\n" + "one. All the labels referencing this font\n" + "will update their references to the new one."); if (mReplacement != mFont && mFont.replacement != mReplacement) { NGUIEditorTools.RegisterUndo("Font Change", mFont); mFont.replacement = mReplacement; UnityEditor.EditorUtility.SetDirty(mFont); } return; } NGUIEditorTools.DrawSeparator(); ComponentSelector.Draw <UIAtlas>(mFont.atlas as UIAtlas, OnSelectAtlas); if (mFont.atlas != null) { if (mFont.bmFont.LegacyCheck()) { Debug.Log(mFont.name + " uses a legacy font data structure. Upgrading, please save."); EditorUtility.SetDirty(mFont); } if (mFont.bmFont.isValid) { string spriteName = UISlicedSpriteInspector.SpriteField(mFont.atlas as UIAtlas, mFont.spriteName); if (mFont.spriteName != spriteName) { NGUIEditorTools.RegisterUndo("Font Sprite", mFont); mFont.spriteName = spriteName; } } } else { // No atlas specified -- set the material and texture rectangle directly Material mat = EditorGUILayout.ObjectField("Material", mFont.material, typeof(Material), false) as Material; if (mFont.material != mat) { NGUIEditorTools.RegisterUndo("Font Material", mFont); mFont.material = mat; } } bool resetWidthHeight = false; if (mFont.atlas != null || mFont.material != null) { TextAsset data = EditorGUILayout.ObjectField("Import Font", null, typeof(TextAsset), false) as TextAsset; if (data != null) { NGUIEditorTools.RegisterUndo("Import Font Data", mFont); BMFontReader.Load(mFont.bmFont, NGUITools.GetHierarchy(mFont.gameObject), data.bytes); mFont.MarkAsDirty(); resetWidthHeight = true; Debug.Log("Imported " + mFont.bmFont.glyphCount + " characters"); } } if (mFont.bmFont.isValid) { Color green = new Color(0.4f, 1f, 0f, 1f); Texture2D tex = mFont.texture; if (tex != null) { if (mFont.atlas == null) { // Pixels are easier to work with than UVs Rect pixels = NGUIMath.ConvertToPixels(mFont.uvRect, tex.width, tex.height, false); // Automatically set the width and height of the rectangle to be the original font texture's dimensions if (resetWidthHeight) { pixels.width = mFont.texWidth; pixels.height = mFont.texHeight; } // Font sprite rectangle GUI.backgroundColor = green; pixels = EditorGUILayout.RectField("Pixel Rect", pixels); GUI.backgroundColor = Color.white; // Create a button that can make the coordinates pixel-perfect on click GUILayout.BeginHorizontal(); { GUILayout.Label("Correction", GUILayout.Width(75f)); Rect corrected = NGUIMath.MakePixelPerfect(pixels); if (corrected == pixels) { GUI.color = Color.grey; GUILayout.Button("Make Pixel-Perfect"); GUI.color = Color.white; } else if (GUILayout.Button("Make Pixel-Perfect")) { pixels = corrected; GUI.changed = true; } } GUILayout.EndHorizontal(); // Convert the pixel coordinates back to UV coordinates Rect uvRect = NGUIMath.ConvertToTexCoords(pixels, tex.width, tex.height); if (mFont.uvRect != uvRect) { NGUIEditorTools.RegisterUndo("Font Pixel Rect", mFont); mFont.uvRect = uvRect; } } // Font spacing GUILayout.BeginHorizontal(); { EditorGUIUtility.LookLikeControls(0f); GUILayout.Label("Spacing", GUILayout.Width(60f)); GUILayout.Label("X", GUILayout.Width(12f)); int x = EditorGUILayout.IntField(mFont.horizontalSpacing); GUILayout.Label("Y", GUILayout.Width(12f)); int y = EditorGUILayout.IntField(mFont.verticalSpacing); EditorGUIUtility.LookLikeControls(80f); if (mFont.horizontalSpacing != x || mFont.verticalSpacing != y) { NGUIEditorTools.RegisterUndo("Font Spacing", mFont); mFont.horizontalSpacing = x; mFont.verticalSpacing = y; } } GUILayout.EndHorizontal(); EditorGUILayout.Separator(); GUILayout.BeginHorizontal(); { mView = (View)EditorGUILayout.EnumPopup("Show", mView); GUILayout.Label("Shader", GUILayout.Width(45f)); if (mUseShader != EditorGUILayout.Toggle(mUseShader, GUILayout.Width(20f))) { mUseShader = !mUseShader; if (mUseShader && mView == View.Font) { // TODO: Remove this when Unity fixes the bug with DrawPreviewTexture not being affected by BeginGroup Debug.LogWarning("There is a bug in Unity that prevents the texture from getting clipped properly.\n" + "Until it's fixed by Unity, your texture may spill onto the rest of the Unity's GUI while using this mode."); } } } GUILayout.EndHorizontal(); if (mView != View.Nothing) { // Draw the atlas EditorGUILayout.Separator(); Material m = mUseShader ? mFont.material : null; Rect rect = (mView == View.Atlas) ? NGUIEditorTools.DrawAtlas(tex, m) : NGUIEditorTools.DrawSprite(tex, mFont.uvRect, m); NGUIEditorTools.DrawOutline(rect, mFont.uvRect, green); rect = GUILayoutUtility.GetRect(Screen.width, 18f); EditorGUI.DropShadowLabel(rect, "Font Size: " + mFont.size); } } } }
void showCellSprite(Hashtable m) { Hashtable d = MapEx.getMap(m, "data"); Texture tex = ECLEditorUtl.getObjectByPath(MapEx.getString(d, "path")) as Texture; if (tex == null) { return; } // NGUIEditorTools.DrawTiledTexture (rect, NGUIEditorTools.backdropTexture); Rect uv = new Rect(MapEx.getInt(d, "x"), MapEx.getInt(d, "y"), MapEx.getInt(d, "width"), MapEx.getInt(d, "height")); uv = NGUIMath.ConvertToTexCoords(uv, tex.width, tex.height); 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 = cellSize * (1f - aspect) * 0.5f; clipRect.xMin += padding; clipRect.xMax -= padding; } else { // The sprite is wider than it is taller float padding = cellSize * (1f - 1f / aspect) * 0.5f; clipRect.yMin += padding; clipRect.yMax -= padding; } } if (GUI.Button(rect, "")) { mSelectedSprite = MapEx.getString(d, "name"); isShowParckerView = false; currSelectSprite = m; } GUI.DrawTextureWithTexCoords(clipRect, tex, uv); // Draw the selection if (mSelectedSprite == MapEx.getString(d, "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), MapEx.getString(d, "name"), "ProgressBarBack"); GUI.contentColor = Color.white; GUI.backgroundColor = Color.white; GUILayout.Space(cellSize + 30); //这句主要目的是为了可以滑动 rect.y += (cellSize + 30); }
private void DrawSpriteInfo() { if (spriteInfoList == null) { return; } mListScrollPos = EditorGUILayout.BeginScrollView(mListScrollPos); //mIsListExpand = EditorGUILayout.Foldout(mIsListExpand, "图片列表"); //if (mIsListExpand) //{ // foreach (var info in spriteInfoList) // { // EditorGUILayout.BeginHorizontal(GUILayout.Width(380)); // GUILayout.Space(30); // GUILayout.Label(info.Atlas.name + " - " + info.Data.name); // //info.Selected = EditorGUILayout.ToggleLeft(info.SpriteName, info.Selected, GUILayout.Width(240)); // EditorGUILayout.EndHorizontal(); // } //} mIsDicExpand = EditorGUILayout.Foldout(mIsDicExpand, "比对结果"); if (mIsDicExpand) { int i = 0; int size = 80; int padded = 90; int columns = Mathf.FloorToInt(Screen.width / padded); if (columns < 1) { columns = 1; } GUILayout.Space(100); foreach (var info in spriteInfoDic) { #region 图标绘制 int y = i / columns; int x = i - columns * y; Texture2D tex = info.Value.Atlas.texture as Texture2D; Rect rect = new Rect(10f + x * (padded), 20 + y * (padded + 20), size, size); Rect uv = new Rect(info.Value.Data.x, info.Value.Data.y, info.Value.Data.width, info.Value.Data.height); uv = NGUIMath.ConvertToTexCoords(uv, tex.width, tex.height); float scaleX = rect.width / uv.width; float scaleY = rect.height / uv.height; //把图标拉伸到正常大小 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 = 80 * (1f - aspect) * 0.5f; clipRect.xMin += padding; clipRect.xMax -= padding; } else { // The sprite is wider than it is taller float padding = 80 * (1f - 1f / aspect) * 0.5f; clipRect.yMin += padding; clipRect.yMax -= padding; } } //画出外框 if (info.Value.Selected) { NGUIEditorTools.DrawOutline(new Rect(rect.x - 1, rect.y - 1, rect.width + 2, rect.height + 2), Color.green); } else { NGUIEditorTools.DrawOutline(new Rect(rect.x - 1, rect.y - 1, rect.width + 2, rect.height + 2), Color.grey); } //画出按钮 if (GUI.Button(rect, "")) { info.Value.Selected = !info.Value.Selected; } GUI.DrawTextureWithTexCoords(clipRect, tex, uv); #endregion #region 文字绘制和空白留出 GUI.Label(new Rect(rect.x, rect.y + size, rect.width, rect.height), info.Value.Data.name); GUI.Label(new Rect(rect.x, rect.y + size + 10, rect.width, rect.height), info.Value.Atlas.name); if (x == 0) { GUILayout.Space(100); } #endregion i++; } } EditorGUILayout.EndScrollView(); }