/// <summary> /// Draw the font preview window. /// </summary> public override void OnPreviewGUI(Rect rect, GUIStyle background) { mFont = target as UIFont; if (mFont == null) { return; } Texture2D tex = mFont.texture; if (mView != View.Nothing && tex != null) { Material m = (mUseShader ? mFont.material : null); if (mView == View.Font) { Rect outer = new Rect(mFont.uvRect); Rect uv = outer; outer = NGUIMath.ConvertToPixels(outer, tex.width, tex.height, true); NGUIEditorTools.DrawSprite(tex, rect, outer, outer, uv, Color.white, m); } else { Rect outer = new Rect(0f, 0f, 1f, 1f); Rect inner = new Rect(mFont.uvRect); Rect uv = outer; outer = NGUIMath.ConvertToPixels(outer, tex.width, tex.height, true); inner = NGUIMath.ConvertToPixels(inner, tex.width, tex.height, true); NGUIEditorTools.DrawSprite(tex, rect, outer, inner, uv, Color.white, m); } } }
/// <summary> /// Draw the sprite preview. /// </summary> public override void OnPreviewGUI(Rect rect, GUIStyle background) { UISpriteData sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null; if (sprite == null) { return; } // Texture2D tex = mAtlas.texture as Texture2D; //modify by chenbin #region modify by chenbin Texture2D tex; if (mAtlas.isBorrowSpriteMode) { if (sprite.material != null) { tex = sprite.material.mainTexture as Texture2D; } else { tex = null; } } else { tex = mAtlas.texture as Texture2D; } #endregion if (tex != null) { NGUIEditorTools.DrawSprite(tex, rect, sprite, Color.white); } }
/// <summary> /// Draw the sprite preview. /// </summary> public override void OnPreviewGUI(Rect rect, GUIStyle background) { if (mSprite == null || !mSprite.isValid) { return; } Texture2D tex = mSprite.mainTexture as Texture2D; if (tex == null) { return; } Rect outer = new Rect(mSprite.GetAtlasSprite().outer); Rect inner = new Rect(mSprite.GetAtlasSprite().inner); Rect uv = outer; if (mSprite.atlas.coordinates == UIAtlas.Coordinates.Pixels) { uv = NGUIMath.ConvertToTexCoords(outer, tex.width, tex.height); } else { outer = NGUIMath.ConvertToPixels(outer, tex.width, tex.height, true); inner = NGUIMath.ConvertToPixels(inner, tex.width, tex.height, true); } NGUIEditorTools.DrawSprite(tex, rect, outer, inner, uv, mSprite.color); }
/// <summary> /// Draw the sprite preview. /// </summary> public override void OnPreviewGUI(Rect rect, GUIStyle background) { Texture2D tex = mTex.mainTexture as Texture2D; if (tex != null) { Rect tc = mTex.uvRect; tc.xMin *= tex.width; tc.xMax *= tex.width; tc.yMin *= tex.height; tc.yMax *= tex.height; Vector4 border = mTex.border; NGUIEditorTools.DrawSprite(tex, rect, mTex.color, mTex.material, Mathf.RoundToInt(tc.x), Mathf.RoundToInt(tex.height - tc.y - tc.height), Mathf.RoundToInt(tc.width), Mathf.RoundToInt(tc.height), Mathf.RoundToInt(border.x), Mathf.RoundToInt(border.y), Mathf.RoundToInt(border.z), Mathf.RoundToInt(border.w)); } }
/// <summary> /// Draw the sprite preview. /// </summary> public override void OnPreviewGUI(Rect rect, GUIStyle background) { UIAtlas.Sprite sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null; if (sprite == null) { return; } Texture2D tex = mAtlas.texture as Texture2D; if (tex == null) { return; } Rect outer = new Rect(sprite.outer); Rect inner = new Rect(sprite.inner); Rect uv = outer; if (mAtlas.coordinates == UIAtlas.Coordinates.Pixels) { uv = NGUIMath.ConvertToTexCoords(outer, tex.width, tex.height); } else { outer = NGUIMath.ConvertToPixels(outer, tex.width, tex.height, true); inner = NGUIMath.ConvertToPixels(inner, tex.width, tex.height, true); } NGUIEditorTools.DrawSprite(tex, rect, outer, inner, uv, Color.white); }
/// <summary> /// Draw the sprite preview. /// </summary> public override void OnPreviewGUI (Rect rect, GUIStyle background) { if (mSprite != null && mSprite.sprite2D != null) { Texture2D tex = mSprite.mainTexture as Texture2D; if (tex != null) NGUIEditorTools.DrawSprite(tex, rect, mSprite.color, mSprite.sprite2D.textureRect, mSprite.border); } }
void CellGUI(Rect cellRect, TreeViewItem <ReplaceUISpriteTreeElement> item, MyColumns column, ref RowGUIArgs args) { // Center cell rect vertically (makes it easier to place controls, icons etc in the cells) CenterRectUsingSingleLineHeight(ref cellRect); GUIStyle style = new GUIStyle(); style.alignment = TextAnchor.MiddleCenter; style.normal.textColor = Color.white; switch (column) { case MyColumns.GameObjectName: EditorGUI.LabelField(cellRect, item.data.GameObjectName, style); break; case MyColumns.UIAtlas: EditorGUI.LabelField(cellRect, item.data.AtlasStr, style); break; case MyColumns.UISpriteName: EditorGUI.LabelField(cellRect, item.data.SpriteName, style); break; case MyColumns.SpriteTexture: NGUIEditorTools.DrawSprite(item.data.SpriteTexture, cellRect, item.data.SpriteData, Color.white, false); break; case MyColumns.ChildPath: EditorGUI.LabelField(cellRect, item.data.Path, style); break; case MyColumns.Replace: Rect toggleRect = new Rect(cellRect.x, cellRect.y, 18, cellRect.height); Rect labelRect = new Rect(toggleRect.xMax, cellRect.y, cellRect.width / 2, cellRect.height); item.data.Replace = EditorGUI.Toggle(toggleRect, item.data.Replace); style.alignment = TextAnchor.MiddleLeft; if (item.data.Replace) { EditorGUI.LabelField(labelRect, "替换", style); } else { EditorGUI.LabelField(labelRect, "不替换", style); } break; case MyColumns.TargetSprite: if (item.data.TargetSpriteData != null && item.data.TargetSpriteTexture != null) { NGUIEditorTools.DrawSprite(item.data.TargetSpriteTexture, cellRect, item.data.TargetSpriteData, Color.white, false); } break; } }
/// <summary> /// Draw the sprite preview. /// </summary> public override void OnPreviewGUI(Rect rect, GUIStyle background) { Texture2D tex = mTex.mainTexture as Texture2D; if (tex != null) { Rect uv = mTex.uvRect; Rect outer = NGUIMath.ConvertToPixels(uv, tex.width, tex.height, true); NGUIEditorTools.DrawSprite(tex, rect, outer, outer, uv, mTex.color); } }
/// <summary> /// Draw the sprite preview. /// </summary> public override void OnPreviewGUI (Rect rect, GUIStyle background) { UISprite sprite = target as UISprite; if (sprite == null || !sprite.isValid) return; Texture2D tex = sprite.mainTexture as Texture2D; if (tex == null) return; UISpriteData sd = sprite.atlas.GetSprite(sprite.spriteName); NGUIEditorTools.DrawSprite(tex, rect, sd, sprite.color); }
/// <summary> /// Draw a sprite preview. /// </summary> static public void DrawSprite(Texture2D tex, Rect drawRect, Color color, Rect textureRect, Vector4 border) { NGUIEditorTools.DrawSprite(tex, drawRect, color, null, Mathf.RoundToInt(textureRect.x), Mathf.RoundToInt(tex.height - textureRect.y - textureRect.height), Mathf.RoundToInt(textureRect.width), Mathf.RoundToInt(textureRect.height), Mathf.RoundToInt(border.x), Mathf.RoundToInt(border.y), Mathf.RoundToInt(border.z), Mathf.RoundToInt(border.w)); }
/// <summary> /// Draw the sprite preview. /// </summary> public override void OnPreviewGUI(Rect rect, GUIStyle background) { AtlasSprite asp = target as AtlasSprite; if (asp == null || !asp.isValid) { return; } Sprite s = asp.Renderer.sprite; NGUIEditorTools.DrawSprite(s.texture, rect, asp.Renderer.color, s.textureRect, s.border); }
/// <summary> /// Draw the sprite preview. /// </summary> public override void OnPreviewGUI(Rect rect, GUIStyle background) { Texture2D tex = mTex.mainTexture as Texture2D; if (tex != null) { Rect tc = mTex.uvRect; tc.xMin *= tex.width; tc.xMax *= tex.width; tc.yMin *= tex.height; tc.yMax *= tex.height; NGUIEditorTools.DrawSprite(tex, rect, mTex.color, tc, mTex.border); } }
override protected void OnDrawTexture() { Texture2D tex = mLabel.mainTexture as Texture2D; if (tex != null) { // Draw the atlas EditorGUILayout.Separator(); NGUIEditorTools.DrawSprite(tex, mLabel.font.uvRect, mUseShader ? mLabel.font.material : null); // Sprite size label Rect rect = GUILayoutUtility.GetRect(Screen.width, 18f); EditorGUI.DropShadowLabel(rect, "Font Size: " + mLabel.font.size); } }
override protected void OnDrawTexture() { Texture2D tex = mWidget.mainTexture as Texture2D; if (tex != null) { // Draw the atlas EditorGUILayout.Separator(); NGUIEditorTools.DrawSprite(tex, new Rect(0f, 0f, 1f, 1f), null); // Sprite size label Rect rect = GUILayoutUtility.GetRect(Screen.width, 18f); EditorGUI.DropShadowLabel(rect, "Texture Size: " + tex.width + "x" + tex.height); } }
/// <summary> /// Draw the sprite preview. /// </summary> public override void OnPreviewGUI(Rect rect, GUIStyle background) { UISpriteData sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null; if (sprite == null) { return; } Texture2D tex = mAtlas.texture as Texture2D; if (tex != null) { NGUIEditorTools.DrawSprite(tex, rect, sprite, Color.white); } }
/// <summary> /// Draw the sprite preview. /// </summary> public override void OnPreviewGUI(Rect rect, GUIStyle background) { if (mButton == null || mButton.sprite == null || !mButton.sprite.isValid) { return; } Texture2D tex = mButton.sprite.mainTexture as Texture2D; if (tex == null) { return; } UISpriteData sd = mButton.sprite.atlas.GetSprite(mButton.sprite.spriteName); NGUIEditorTools.DrawSprite(tex, rect, sd, mButton.sprite.color); }
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 font preview window. /// </summary> public override void OnPreviewGUI(Rect rect, GUIStyle background) { mFont = target as UIFont; if (mFont == null) { return; } Texture2D tex = mFont.texture; if (mView != View.Nothing && tex != null) { Material m = (mUseShader ? mFont.material : null); if (mView == View.Font && mFont.atlas != null && mFont.sprite != null) { NGUIEditorTools.DrawSprite(tex, rect, mFont.sprite, Color.white, m); } else { NGUIEditorTools.DrawTexture(tex, rect, new Rect(0f, 0f, 1f, 1f), Color.white, m); } } }
/// <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 sprite preview. /// </summary> public override void OnPreviewGUI(Rect rect, GUIStyle background) { UISprite sprite = target as UISprite; if (sprite == null || !sprite.isValid) { return; } Texture2D tex = sprite.mainTexture as Texture2D; if (tex == null) { return; } float interval = 10f; float width = (rect.width - interval) / 2; Rect leftRect = new Rect(rect.x, rect.y, width, rect.height); Rect rightRect = new Rect(rect.x + width + interval, rect.y, width, rect.height); UISpriteData sd = sprite.atlas.GetSprite(sprite.spriteName); NGUIEditorTools.DrawAtlas(tex, leftRect, sd); NGUIEditorTools.DrawSprite(tex, rightRect, sd, sprite.color); //点选图片 Event e = Event.current; if (e.type == EventType.MouseDown) { //下面函数通过点击的坐标,和texture、texture draw rect等信息计算出被点选的sprite UISpriteData click = NGUIEditorTools.GetUISpriteData(tex, sprite.atlas, leftRect, e.mousePosition); if (click != null) { sprite.spriteName = click.name; EditorUtility.SetDirty(sprite); } } }
public override void OnPreviewGUI(Rect drawRect, GUIStyle background) { MeshBuilder mb = target as MeshBuilder; #if NGUI_USED if ((mb == null) || (mb.mAtlas == null)) { return; } Texture2D tex = mb.mAtlas.texture as Texture2D; if (tex == null) { return; } UISpriteData sprData = mb.mAtlas.GetSprite(mb.mSpriteName); if (sprData == null) { return; } NGUIEditorTools.DrawSprite(tex, drawRect, sprData, Color.white); #else Texture tex2D; if ((mb._Material == null) || ((tex2D = mb._Material.mainTexture) == null)) { return; } Rect outerRect = drawRect; Rect sprRect = mb._SpriteRect; outerRect.width = sprRect.width; outerRect.height = sprRect.height; if (sprRect.width > 0) { float f = drawRect.width / outerRect.width; outerRect.width *= f; outerRect.height *= f; } if (drawRect.height > outerRect.height) { outerRect.y += (drawRect.height - outerRect.height) * 0.5f; } else if (outerRect.height > drawRect.height) { float f = drawRect.height / outerRect.height; outerRect.width *= f; outerRect.height *= f; } if (drawRect.width > outerRect.width) { outerRect.x += (drawRect.width - outerRect.width) * 0.5f; } Rect uv = sprRect; int width = tex2D.width; int height = tex2D.height; if (width != 0f && height != 0f) { uv.xMin = sprRect.xMin / width; uv.xMax = sprRect.xMax / width; uv.yMin = 1f - sprRect.yMax / height; uv.yMax = 1f - sprRect.yMin / height; } GUI.DrawTextureWithTexCoords(outerRect, tex2D, uv, true); #endif }
/// <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(); } } } } }
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); } } } }
/// <summary> /// Draw the inspector widget. /// </summary> public override void OnInspectorGUI() { EditorGUIUtility.LookLikeControls(80f); InvDatabase db = target as InvDatabase; NGUIEditorTools.DrawSeparator(); InvBaseItem item = null; if (db.items == null || db.items.Count == 0) { mIndex = 0; } else { mIndex = Mathf.Clamp(mIndex, 0, db.items.Count - 1); item = db.items[mIndex]; } if (mConfirmDelete) { // Show the confirmation dialog GUILayout.Label("Are you sure you want to delete '" + item.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 Inventory Item", db); db.items.RemoveAt(mIndex); mConfirmDelete = false; } GUI.backgroundColor = Color.white; } GUILayout.EndHorizontal(); } else { // Database icon atlas UIAtlas atlas = EditorGUILayout.ObjectField("Icon Atlas", db.iconAtlas, typeof(UIAtlas), false) as UIAtlas; if (atlas != db.iconAtlas) { NGUIEditorTools.RegisterUndo("Databse Atlas change", db); db.iconAtlas = atlas; foreach (InvBaseItem i in db.items) { i.iconAtlas = atlas; } } // Database ID int dbID = EditorGUILayout.IntField("Database ID", db.databaseID); if (dbID != db.databaseID) { NGUIEditorTools.RegisterUndo("Database ID change", db); db.databaseID = dbID; } // "New" button GUI.backgroundColor = Color.green; if (GUILayout.Button("New Item")) { NGUIEditorTools.RegisterUndo("Add Inventory Item", db); InvBaseItem bi = new InvBaseItem(); bi.iconAtlas = db.iconAtlas; bi.id16 = (db.items.Count > 0) ? db.items[db.items.Count - 1].id16 + 1 : 0; db.items.Add(bi); mIndex = db.items.Count - 1; if (item != null) { bi.name = "Copy of " + item.name; bi.description = item.description; bi.slot = item.slot; bi.color = item.color; bi.iconName = item.iconName; bi.attachment = item.attachment; bi.minItemLevel = item.minItemLevel; bi.maxItemLevel = item.maxItemLevel; foreach (InvStat stat in item.stats) { InvStat copy = new InvStat(); copy.id = stat.id; copy.amount = stat.amount; copy.modifier = stat.modifier; bi.stats.Add(copy); } } else { bi.name = "New Item"; bi.description = "Item Description"; } item = bi; } GUI.backgroundColor = Color.white; if (item != null) { NGUIEditorTools.DrawSeparator(); // Navigation section GUILayout.BeginHorizontal(); { if (mIndex == 0) { GUI.color = Color.grey; } if (GUILayout.Button("<<")) { mConfirmDelete = false; --mIndex; } GUI.color = Color.white; mIndex = EditorGUILayout.IntField(mIndex + 1, GUILayout.Width(40f)) - 1; GUILayout.Label("/ " + db.items.Count, GUILayout.Width(40f)); if (mIndex + 1 == db.items.Count) { GUI.color = Color.grey; } if (GUILayout.Button(">>")) { mConfirmDelete = false; ++mIndex; } GUI.color = Color.white; } GUILayout.EndHorizontal(); NGUIEditorTools.DrawSeparator(); // Item name and delete item button GUILayout.BeginHorizontal(); { string itemName = EditorGUILayout.TextField("Item Name", item.name); GUI.backgroundColor = Color.red; if (GUILayout.Button("Delete", GUILayout.Width(55f))) { mConfirmDelete = true; } GUI.backgroundColor = Color.white; if (!itemName.Equals(item.name)) { NGUIEditorTools.RegisterUndo("Rename Item", db); item.name = itemName; } } GUILayout.EndHorizontal(); string itemDesc = GUILayout.TextArea(item.description, 200, GUILayout.Height(100f)); InvBaseItem.Slot slot = (InvBaseItem.Slot)EditorGUILayout.EnumPopup("Slot", item.slot); string iconName = ""; float iconSize = 64f; bool drawIcon = false; float extraSpace = 0f; if (item.iconAtlas != null) { List <string> sprites = item.iconAtlas.GetListOfSprites(); sprites.Insert(0, "<None>"); int index = 0; string spriteName = (item.iconName != null) ? item.iconName : sprites[0]; // We need to find the sprite in order to have it selected if (!string.IsNullOrEmpty(spriteName)) { for (int i = 1; i < sprites.Count; ++i) { if (spriteName.Equals(sprites[i], System.StringComparison.OrdinalIgnoreCase)) { index = i; break; } } } // Draw the sprite selection popup index = EditorGUILayout.Popup("Icon", index, sprites.ToArray()); UIAtlas.Sprite sprite = (index > 0) ? item.iconAtlas.GetSprite(sprites[index]) : null; if (sprite != null) { iconName = sprite.name; Material mat = item.iconAtlas.spriteMaterial; if (mat != null) { Texture2D tex = mat.mainTexture as Texture2D; if (tex != null) { drawIcon = true; Rect rect = sprite.outer; if (item.iconAtlas.coordinates == UIAtlas.Coordinates.Pixels) { rect = NGUIMath.ConvertToTexCoords(rect, tex.width, tex.height); } GUILayout.Space(4f); GUILayout.BeginHorizontal(); { GUILayout.Space(Screen.width - iconSize); NGUIEditorTools.DrawSprite(tex, rect, null, false); } GUILayout.EndHorizontal(); extraSpace = iconSize * (float)sprite.outer.height / sprite.outer.width; } } } } // Item level range GUILayout.BeginHorizontal(); GUILayout.Label("Level Range", GUILayout.Width(77f)); int min = EditorGUILayout.IntField(item.minItemLevel, GUILayout.MinWidth(40f)); int max = EditorGUILayout.IntField(item.maxItemLevel, GUILayout.MinWidth(40f)); if (drawIcon) { GUILayout.Space(iconSize); } GUILayout.EndHorizontal(); // Game Object attachment field, left of the icon GUILayout.BeginHorizontal(); GameObject go = (GameObject)EditorGUILayout.ObjectField("Attachment", item.attachment, typeof(GameObject), false); if (drawIcon) { GUILayout.Space(iconSize); } GUILayout.EndHorizontal(); // Color tint field, left of the icon GUILayout.BeginHorizontal(); Color color = EditorGUILayout.ColorField("Color", item.color); if (drawIcon) { GUILayout.Space(iconSize); } GUILayout.EndHorizontal(); // Calculate the extra spacing necessary for the icon to show up properly and not overlap anything if (drawIcon) { extraSpace = Mathf.Max(0f, extraSpace - 60f); GUILayout.Space(extraSpace); } // Item stats NGUIEditorTools.DrawSeparator(); if (item.stats != null) { for (int i = 0; i < item.stats.Count; ++i) { InvStat stat = item.stats[i]; GUILayout.BeginHorizontal(); { InvStat.Identifier iden = (InvStat.Identifier)EditorGUILayout.EnumPopup(stat.id, GUILayout.Width(80f)); // Color the field red if it's negative, green if it's positive if (stat.amount > 0) { GUI.backgroundColor = Color.green; } else if (stat.amount < 0) { GUI.backgroundColor = Color.red; } int amount = EditorGUILayout.IntField(stat.amount, GUILayout.Width(40f)); GUI.backgroundColor = Color.white; InvStat.Modifier mod = (InvStat.Modifier)EditorGUILayout.EnumPopup(stat.modifier); GUI.backgroundColor = Color.red; if (GUILayout.Button("X", GUILayout.Width(20f))) { NGUIEditorTools.RegisterUndo("Delete Item Stat", db); item.stats.RemoveAt(i); --i; } else if (iden != stat.id || amount != stat.amount || mod != stat.modifier) { NGUIEditorTools.RegisterUndo("Item Stats", db); stat.id = iden; stat.amount = amount; stat.modifier = mod; } GUI.backgroundColor = Color.white; } GUILayout.EndHorizontal(); } } if (GUILayout.Button("Add Stat", GUILayout.Width(80f))) { NGUIEditorTools.RegisterUndo("Add Item Stat", db); InvStat stat = new InvStat(); stat.id = InvStat.Identifier.Armor; item.stats.Add(stat); } // Save all values if (!itemDesc.Equals(item.description) || slot != item.slot || go != item.attachment || color != item.color || min != item.minItemLevel || max != item.maxItemLevel || !iconName.Equals(item.iconName)) { NGUIEditorTools.RegisterUndo("Item Properties", db); item.description = itemDesc; item.slot = slot; item.attachment = go; item.color = color; item.iconName = iconName; item.minItemLevel = min; item.maxItemLevel = max; } } } }