/// <summary> /// Duplicate the specified sprite. /// </summary> static public SpriteEntry DuplicateSprite(NGUIAtlas atlas, string spriteName) { if (atlas == null || atlas.texture == null) { return(null); } UISpriteData sd = atlas.GetSprite(spriteName); if (sd == null) { return(null); } Texture2D tex = NGUIEditorTools.ImportTexture(atlas.texture, true, true, false); SpriteEntry se = ExtractSprite(sd, tex); if (se != null) { se.name = se.name + " (Copy)"; List <UIAtlasMaker.SpriteEntry> sprites = new List <UIAtlasMaker.SpriteEntry>(); UIAtlasMaker.ExtractSprites(atlas, sprites); sprites.Add(se); UIAtlasMaker.UpdateAtlas(atlas, sprites); se.Release(); } else { NGUIEditorTools.ImportTexture(atlas.texture, false, false, !atlas.premultipliedAlpha); } return(se); }
/// <summary> /// Add a sprite appropriate for the specified atlas sprite. /// It will be sliced if the sprite has an inner rect, and a regular sprite otherwise. /// </summary> static public NGUISprite AddSprite(GameObject go, NGUIAtlas atlas, string spriteName) { NGUIAtlas.Sprite sp = (atlas != null) ? atlas.GetSprite(spriteName) : null; NGUISprite sprite = AddWidget <NGUISprite>(go); sprite.type = (sp == null || sp.inner == sp.outer) ? NGUISprite.Type.Simple : NGUISprite.Type.Sliced; sprite.atlas = atlas; sprite.spriteName = spriteName; return(sprite); }
/// <summary> /// Validate this symbol, given the specified atlas. /// </summary> public bool Validate(NGUIAtlas atlas) { if (atlas == null) { return(false); } #if UNITY_EDITOR if (!Application.isPlaying || !mIsValid) #else if (!mIsValid) #endif { if (string.IsNullOrEmpty(spriteName)) { return(false); } mSprite = (atlas != null) ? atlas.GetSprite(spriteName) : null; if (mSprite != null) { Texture tex = atlas.texture; if (tex == null) { mSprite = null; } else { Rect outer = mSprite.outer; mUV = outer; if (atlas.coordinates == NGUIAtlas.Coordinates.Pixels) { mUV = NGUIMath.ConvertToTexCoords(mUV, tex.width, tex.height); } else { outer = NGUIMath.ConvertToPixels(outer, tex.width, tex.height, true); } mOffsetX = Mathf.RoundToInt(mSprite.paddingLeft * outer.width); mOffsetY = Mathf.RoundToInt(mSprite.paddingTop * outer.width); mWidth = Mathf.RoundToInt(outer.width); mHeight = Mathf.RoundToInt(outer.height); mAdvance = Mathf.RoundToInt(outer.width + (mSprite.paddingRight + mSprite.paddingLeft) * outer.width); mIsValid = true; } } } return(mSprite != null); }
/// <summary> /// Convenience function that displays a list of sprites and returns the selected value. /// </summary> static public void AdvancedSpriteField(NGUIAtlas atlas, string spriteName, SpriteSelector.Callback callback, bool editable, params GUILayoutOption[] options) { // Give the user a warning if there are no sprites in the atlas if (atlas.spriteList.Count == 0) { EditorGUILayout.HelpBox("No sprites found", MessageType.Warning); return; } // Sprite selection drop-down list GUILayout.BeginHorizontal(); { if (GUILayout.Button("Sprite", "DropDownButton", GUILayout.Width(76f))) { SpriteSelector.Show(atlas, spriteName, callback); } if (editable) { string sn = GUILayout.TextField(spriteName); if (sn != spriteName) { NGUIAtlas.Sprite sp = atlas.GetSprite(spriteName); if (sp != null) { NGUIEditorTools.RegisterUndo("Edit Sprite Name", atlas); sp.name = sn; spriteName = sn; } } } else { GUILayout.BeginHorizontal(); GUILayout.Label(spriteName, "HelpBox", GUILayout.Height(18f)); GUILayout.Space(18f); GUILayout.EndHorizontal(); if (GUILayout.Button("Edit", GUILayout.Width(40f))) { EditorPrefs.SetString("NGUI Selected Sprite", spriteName); Select(atlas.gameObject); } } } GUILayout.EndHorizontal(); }
/// <summary> /// Draw a simple sprite selection button. /// </summary> static public bool SimpleSpriteField(NGUIAtlas atlas, string spriteName, SpriteSelector.Callback callback, params GUILayoutOption[] options) { if (atlas.GetSprite(spriteName) == null) { spriteName = ""; } if (GUILayout.Button(spriteName, "DropDown", options)) { SpriteSelector.Show(atlas, spriteName, callback); return(true); } return(false); }
/// <summary> /// Retrieve the atlas sprite referenced by the spriteName field. /// </summary> public NGUIAtlas.Sprite GetAtlasSprite() { if (!mSpriteSet) { mSprite = null; } if (mSprite == null && mAtlas != null) { if (!string.IsNullOrEmpty(mSpriteName)) { NGUIAtlas.Sprite sp = mAtlas.GetSprite(mSpriteName); if (sp == null) { return(null); } SetAtlasSprite(sp); } if (mSprite == null && mAtlas.spriteList.Count > 0) { NGUIAtlas.Sprite sp = mAtlas.spriteList[0]; if (sp == null) { return(null); } SetAtlasSprite(sp); if (mSprite == null) { Debug.LogError(mAtlas.name + " seems to have a null sprite!"); return(null); } mSpriteName = mSprite.name; } // If the sprite has been set, update the material if (mSprite != null) { material = mAtlas.spriteMaterial; UpdateUVs(true); } } return(mSprite); }
/// <summary> /// Convenience function that retrieves a sprite by name. /// </summary> public Sprite GetSprite(string name) { if (mReplacement != null) { return(mReplacement.GetSprite(name)); } else if (!string.IsNullOrEmpty(name)) { for (int i = 0, imax = sprites.Count; i < imax; ++i) { Sprite s = sprites[i]; // string.Equals doesn't seem to work with Flash export if (!string.IsNullOrEmpty(s.name) && name == s.name) { return(s); } } } return(null); }
/// <summary> /// Draw a sprite selection field. /// </summary> static public void SpriteField(string fieldName, string caption, NGUIAtlas atlas, string spriteName, SpriteSelector.Callback callback) { GUILayout.BeginHorizontal(); GUILayout.Label(fieldName, GUILayout.Width(76f)); if (atlas.GetSprite(spriteName) == null) { spriteName = ""; } if (GUILayout.Button(spriteName, "MiniPullDown", GUILayout.Width(120f))) { SpriteSelector.Show(atlas, spriteName, callback); } if (!string.IsNullOrEmpty(caption)) { GUILayout.Space(20f); GUILayout.Label(caption); } GUILayout.EndHorizontal(); }
/// <summary> /// Validate this symbol, given the specified atlas. /// </summary> public bool Validate(NGUIAtlas atlas) { if (atlas == null) return false; #if UNITY_EDITOR if (!Application.isPlaying || !mIsValid) #else if (!mIsValid) #endif { if (string.IsNullOrEmpty(spriteName)) return false; mSprite = (atlas != null) ? atlas.GetSprite(spriteName) : null; if (mSprite != null) { Texture tex = atlas.texture; if (tex == null) { mSprite = null; } else { Rect outer = mSprite.outer; mUV = outer; if (atlas.coordinates == NGUIAtlas.Coordinates.Pixels) { mUV = NGUIMath.ConvertToTexCoords(mUV, tex.width, tex.height); } else { outer = NGUIMath.ConvertToPixels(outer, tex.width, tex.height, true); } mOffsetX = Mathf.RoundToInt(mSprite.paddingLeft * outer.width); mOffsetY = Mathf.RoundToInt(mSprite.paddingTop * outer.width); mWidth = Mathf.RoundToInt(outer.width); mHeight = Mathf.RoundToInt(outer.height); mAdvance = Mathf.RoundToInt(outer.width + (mSprite.paddingRight + mSprite.paddingLeft) * outer.width); mIsValid = true; } } } return (mSprite != null); }
/// <summary> /// Draw the custom wizard. /// </summary> void OnGUI() { EditorGUIUtility.LookLikeControls(80f); if (mAtlas == null) { GUILayout.Label("No Atlas selected.", "LODLevelNotifyText"); } else { bool close = false; 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(); Texture2D tex = mAtlas.texture as Texture2D; if (tex == null) { GUILayout.Label("The atlas doesn't have a texture to work with"); return; } BetterList <string> sprites = mAtlas.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); while (offset < sprites.size) { GUILayout.BeginHorizontal(); { int col = 0; rect.x = 10f; for (; offset < sprites.size; ++offset) { NGUIAtlas.Sprite sprite = mAtlas.GetSprite(sprites[offset]); if (sprite == null) { continue; } // Button comes first if (GUI.Button(rect, "")) { float delta = Time.realtimeSinceStartup - mClickTime; mClickTime = Time.realtimeSinceStartup; if (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); } } else if (delta < 0.5f) { close = true; } } if (Event.current.type == EventType.Repaint) { // On top of the button we have a checkboard grid NGUIEditorTools.DrawTiledTexture(rect, NGUIEditorTools.backdropTexture); Rect uv = sprite.outer; if (mAtlas.coordinates == NGUIAtlas.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) / ((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 (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(); if (close) { Close(); } } }
/// <summary> /// Draw the inspector widget. /// </summary> public override void OnInspectorGUI() { EditorGUIUtility.LookLikeControls(80f); mAtlas = target as NGUIAtlas; NGUIEditorTools.DrawSeparator(); if (mAtlas.replacement != null) { mType = AtlasType.Reference; mReplacement = mAtlas.replacement; } 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<NGUIAtlas>(mAtlas.replacement, OnSelectAtlas); NGUIEditorTools.DrawSeparator(); EditorGUILayout.HelpBox("You can have one atlas simply point to " + "another one. This is useful if you want to be " + "able to quickly replace the contents of one " + "atlas with another one, for example for " + "swapping an SD atlas with an HD one, or " + "replacing an English atlas with a Chinese " + "one. All the sprites referencing this atlas " + "will update their references to the new one.", MessageType.Info); 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(); } NGUIAtlas.Coordinates coords = (NGUIAtlas.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, GUILayout.Width(120f)); 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]; } if (!mConfirmDelete && mSprite != null) { NGUIEditorTools.DrawSeparator(); NGUIEditorTools.AdvancedSpriteField(mAtlas, mSprite.name, SelectSprite, true); if (mSprite == null) return; Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D; if (tex != null) { Rect inner = mSprite.inner; Rect outer = mSprite.outer; if (mAtlas.coordinates == NGUIAtlas.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); bool changed = false; if (mSprite.inner != inner || mSprite.outer != outer) { NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas); mSprite.inner = inner; mSprite.outer = outer; MarkSpriteAsDirty(); changed = true; } EditorGUILayout.Separator(); if (mAtlas.coordinates == NGUIAtlas.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 (changed || 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.height; mSprite.paddingRight = b.x / mSprite.outer.width; 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 == NGUIAtlas.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(); } } // This functionality is no longer used. It became obsolete when the Atlas Maker was added. /*NGUIEditorTools.DrawSeparator(); GUILayout.BeginHorizontal(); { EditorGUILayout.PrefixLabel("Add/Delete"); if (GUILayout.Button("Clone Sprite")) { NGUIEditorTools.RegisterUndo("Add Sprite", mAtlas); NGUIAtlas.Sprite newSprite = new NGUIAtlas.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 (NGUIEditorTools.previousSelection != null) { NGUIEditorTools.DrawSeparator(); GUI.backgroundColor = Color.green; if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name)) { NGUIEditorTools.SelectPrevious(); } GUI.backgroundColor = Color.white; } } } } }
/// <summary> /// Draw the inspector widget. /// </summary> public override void OnInspectorGUI() { EditorGUIUtility.LookLikeControls(80f); mAtlas = target as NGUIAtlas; NGUIEditorTools.DrawSeparator(); if (mAtlas.replacement != null) { mType = AtlasType.Reference; mReplacement = mAtlas.replacement; } 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 <NGUIAtlas>(mAtlas.replacement, OnSelectAtlas); NGUIEditorTools.DrawSeparator(); EditorGUILayout.HelpBox("You can have one atlas simply point to " + "another one. This is useful if you want to be " + "able to quickly replace the contents of one " + "atlas with another one, for example for " + "swapping an SD atlas with an HD one, or " + "replacing an English atlas with a Chinese " + "one. All the sprites referencing this atlas " + "will update their references to the new one.", MessageType.Info); 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(); } NGUIAtlas.Coordinates coords = (NGUIAtlas.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, GUILayout.Width(120f)); 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]; } } if (!mConfirmDelete && mSprite != null) { NGUIEditorTools.DrawSeparator(); NGUIEditorTools.AdvancedSpriteField(mAtlas, mSprite.name, SelectSprite, true); if (mSprite == null) { return; } Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D; if (tex != null) { Rect inner = mSprite.inner; Rect outer = mSprite.outer; if (mAtlas.coordinates == NGUIAtlas.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); bool changed = false; if (mSprite.inner != inner || mSprite.outer != outer) { NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas); mSprite.inner = inner; mSprite.outer = outer; MarkSpriteAsDirty(); changed = true; } EditorGUILayout.Separator(); if (mAtlas.coordinates == NGUIAtlas.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 (changed || 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.height; mSprite.paddingRight = b.x / mSprite.outer.width; 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 == NGUIAtlas.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(); } } // This functionality is no longer used. It became obsolete when the Atlas Maker was added. /*NGUIEditorTools.DrawSeparator(); * * GUILayout.BeginHorizontal(); * { * EditorGUILayout.PrefixLabel("Add/Delete"); * * if (GUILayout.Button("Clone Sprite")) * { * NGUIEditorTools.RegisterUndo("Add Sprite", mAtlas); * NGUIAtlas.Sprite newSprite = new NGUIAtlas.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 (NGUIEditorTools.previousSelection != null) { NGUIEditorTools.DrawSeparator(); GUI.backgroundColor = Color.green; if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name)) { NGUIEditorTools.SelectPrevious(); } GUI.backgroundColor = Color.white; } } } } }
/// <summary> /// Sprite selection callback. /// </summary> void SelectSprite(string spriteName) { mSprite = mAtlas.GetSprite(spriteName); EditorPrefs.SetString("NGUI Selected Sprite", spriteName); Repaint(); }