/// <summary> /// 若有图集,有名字,没图片,就初始化一下图片 /// </summary> public void InitSprite() { if (_Name != "" && sprite == null && Atlas != null) { sprite = Atlas.GetSprite(_Name); } }
/// <summary> /// Draw the inspector widget. /// </summary> public override void OnInspectorGUI() { UGUIEditorTools.SetLabelWidth(80f); mAtlas = target as UGUIAtlas; Sprite sprite = (mAtlas != null) ? mAtlas.GetSprite(UGUISettings.selectedSprite) : null; GUILayout.Space(6f); if (mAtlas.replacement != null) { mType = AtlasType.Reference; mReplacement = mAtlas.replacement; } GUILayout.BeginHorizontal(); AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType); UGUIEditorTools.DrawPadding(); GUILayout.EndHorizontal(); if (mType != after) { if (after == AtlasType.Normal) { mType = AtlasType.Normal; OnSelectAtlas(null); } else { mType = AtlasType.Reference; } } if (mType == AtlasType.Reference) { ComponentSelector.Draw <UGUIAtlas>(mAtlas.replacement, OnSelectAtlas, true); GUILayout.Space(6f); 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) { UGUIEditorTools.RegisterUndo("Atlas Change", mAtlas); mAtlas.replacement = mReplacement; UGUITools.SetDirty(mAtlas); } return; } GUILayout.Space(6f); // Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material; // // if (mAtlas.spriteMaterial != mat) // { // UGUIEditorTools.RegisterUndo("Atlas Change", mAtlas); // mAtlas.spriteMaterial = mat; // // // Ensure that this atlas has valid import settings // if (mAtlas.texture != null) UGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha); // // mAtlas.MarkAsChanged(); // } // // 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) UGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha); // // UGUIEditorTools.RegisterUndo("Import Sprites", mAtlas); // NGUIJson.LoadSpriteData(mAtlas, ta); // if (sprite != null) sprite = mAtlas.GetSprite(sprite.name); // mAtlas.MarkAsChanged(); // } // // float pixelSize = EditorGUILayout.FloatField("Pixel Size", mAtlas.pixelSize, GUILayout.Width(120f)); // // if (pixelSize != mAtlas.pixelSize) // { // UGUIEditorTools.RegisterUndo("Atlas Change", mAtlas); // mAtlas.pixelSize = pixelSize; // } // } // if (mAtlas.mainTexture != null) { Color blueColor = new Color(0f, 0.7f, 1f, 1f); Color greenColor = new Color(0.4f, 1f, 0f, 1f); if (sprite == null && mAtlas.spriteList.Count > 0) { string spriteName = UGUISettings.selectedSprite; if (!string.IsNullOrEmpty(spriteName)) { sprite = mAtlas.GetSprite(spriteName); } if (sprite == null) { sprite = mAtlas.spriteList[0]; } } if (sprite != null) { if (sprite == null) { return; } // Texture2D tex = mAtlas.mainTexture as Texture2D; // if (tex != null) { if (!UGUIEditorTools.DrawHeader("Sprite Details")) { return; } UGUIEditorTools.BeginContents(); GUILayout.Space(3f); UGUIEditorTools.DrawAdvancedSpriteField(mAtlas, sprite.name, SelectSprite, true); GUILayout.Space(6f); GUI.changed = false; GUI.backgroundColor = greenColor; UGUIEditorTools.IntVector sizeA = UGUIEditorTools.IntPair("Dimensions", "X", "Y", (int)sprite.textureRect.x, (int)sprite.textureRect.y); UGUIEditorTools.IntVector sizeB = UGUIEditorTools.IntPair(null, "Width", "Height", (int)sprite.textureRect.width, (int)sprite.textureRect.height); EditorGUILayout.Separator(); GUI.backgroundColor = blueColor; UGUIEditorTools.IntVector borderA = UGUIEditorTools.IntPair("Border", "Left", "Right", (int)sprite.border.x, (int)sprite.border.y); UGUIEditorTools.IntVector borderB = UGUIEditorTools.IntPair(null, "Bottom", "Top", (int)sprite.border.y, (int)sprite.border.z); EditorGUILayout.Separator(); GUI.backgroundColor = Color.white; UGUIEditorTools.IntVector padA = UGUIEditorTools.IntPair("Padding", "Left", "Right", (int)sprite.rect.xMin, (int)sprite.rect.xMax); UGUIEditorTools.IntVector padB = UGUIEditorTools.IntPair(null, "Bottom", "Top", (int)sprite.rect.yMax, (int)sprite.rect.yMin); if (GUI.changed) { // UGUIEditorTools.RegisterUndo("Atlas Change", mAtlas); // // sprite.rect.x = sizeA.x; // sprite.rect.y = sizeA.y; // sprite.rect.width = sizeB.x; // sprite.rect.height = sizeB.y; // // sprite.paddingLeft = padA.x; // sprite.paddingRight = padA.y; // sprite.paddingBottom = padB.x; // sprite.paddingTop = padB.y; // // sprite.borderLeft = borderA.x; // sprite.borderRight = borderA.y; // sprite.borderBottom = borderB.x; // sprite.borderTop = borderB.y; // // MarkSpriteAsDirty(); } GUILayout.Space(3f); GUILayout.BeginHorizontal(); // if (GUILayout.Button("Duplicate")) // { // UGUIAtlasMaker.SpriteEntry se = UGUIAtlasMaker.DuplicateSprite(mAtlas, sprite.name); // if (se != null) UGUISettings.selectedSprite = se.name; // } // if (GUILayout.Button("Save As...")) // { // #if UNITY_3_5 // string path = EditorUtility.SaveFilePanel("Save As", // UGUISettings.currentPath, sprite.name + ".png", "png"); // #else // string path = EditorUtility.SaveFilePanelInProject("Save As", // sprite.name + ".png", "png", // "Extract sprite into which file?", UGUISettings.currentPath); // #endif // // if (!string.IsNullOrEmpty(path)) // { // UGUISettings.currentPath = System.IO.Path.GetDirectoryName(path); // UGUIAtlasMaker.SpriteEntry se = UGUIAtlasMaker.ExtractSprite(mAtlas, sprite.name); // // if (se != null) // { // byte[] bytes = se.tex.EncodeToPNG(); // File.WriteAllBytes(path, bytes); // AssetDatabase.ImportAsset(path); // if (se.temporaryTexture) DestroyImmediate(se.tex); // } // } // } GUILayout.EndHorizontal(); UGUIEditorTools.EndContents(); } if (UGUIEditorTools.DrawHeader("Modify")) { UGUIEditorTools.BeginContents(); EditorGUILayout.BeginHorizontal(); GUILayout.Space(20f); EditorGUILayout.BeginVertical(); UGUISettings.backgroundColor = EditorGUILayout.ColorField("Background", UGUISettings.backgroundColor); // if (GUILayout.Button("Add a Shadow")) AddShadow(sprite); // if (GUILayout.Button("Add a Soft Outline")) AddOutline(sprite); // if (GUILayout.Button("Add a Transparent Border")) AddTransparentBorder(sprite); // if (GUILayout.Button("Add a Clamped Border")) AddClampedBorder(sprite); // if (GUILayout.Button("Add a Tiled Border")) AddTiledBorder(sprite); // EditorGUI.BeginDisabledGroup(!sprite.hasBorder); // if (GUILayout.Button("Crop Border")) CropBorder(sprite); // EditorGUI.EndDisabledGroup(); EditorGUILayout.EndVertical(); GUILayout.Space(20f); EditorGUILayout.EndHorizontal(); UGUIEditorTools.EndContents(); } if (UGUIEditorTools.previousSelection != null) { GUILayout.Space(3f); GUI.backgroundColor = Color.green; if (GUILayout.Button("<< Return to " + UGUIEditorTools.previousSelection.name)) { UGUIEditorTools.SelectPrevious(); } GUI.backgroundColor = Color.white; } } } }
public Sprite GetSprite(string spriteName) { if (mReplacement != null) { return(mReplacement.GetSprite(spriteName)); } else if (!string.IsNullOrEmpty(spriteName)) { if (mSprites.Count == 0) { Upgrade(); } if (mSprites.Count == 0) { return(null); } // O(1) lookup via a dictionary #if UNITY_EDITOR if (Application.isPlaying) #endif { // The number of indices differs from the sprite list? Rebuild the indices. if (mSpriteIndices.Count != mSprites.Count) { MarkSpriteListAsChanged(); } int index; if (mSpriteIndices.TryGetValue(spriteName, out index)) { // If the sprite is present, return it as-is if (index > -1 && index < mSprites.Count) { return(mSprites[index]); } // The sprite index was out of range -- perhaps the sprite was removed? Rebuild the indices. MarkSpriteListAsChanged(); // Try to look up the index again return(mSpriteIndices.TryGetValue(spriteName, out index) ? mSprites[index] : null); } } // Sequential O(N) lookup. for (int i = 0, imax = mSprites.Count; i < imax; ++i) { Sprite s = mSprites[i]; // string.Equals doesn't seem to work with Flash export if (!string.IsNullOrEmpty(s.name) && spriteName == s.name) { #if UNITY_EDITOR if (!Application.isPlaying) { return(s); } #endif // If this point was reached then the sprite is present in the non-indexed list, // so the sprite indices should be updated. MarkSpriteListAsChanged(); return(s); } } } return(null); }
/// <summary> /// Draw the custom wizard. /// </summary> void OnGUI() { EditorGUIUtility.labelWidth = 80; if (UGUISettings.atlas == null) { GUILayout.Label("No Atlas selected.", "LODLevelNotifyText"); } else { UGUIAtlas atlas = UGUISettings.atlas; bool close = false; GUILayout.Label(atlas.name + " Sprites", "LODLevelNotifyText"); UGUIEditorTools.DrawSeparator(); GUILayout.BeginHorizontal(); GUILayout.Space(84f); string before = UGUISettings.partialSprite; string after = EditorGUILayout.TextField("", before, "SearchTextField"); if (before != after) { UGUISettings.partialSprite = after; } if (GUILayout.Button("", "SearchCancelButton", GUILayout.Width(18f))) { UGUISettings.partialSprite = ""; GUIUtility.keyboardControl = 0; } GUILayout.Space(84f); GUILayout.EndHorizontal(); // Texture2D tex = atlas.mainTexture as Texture2D; // // if (tex == null) // { // GUILayout.Label("The atlas doesn't have a texture to work with"); // return; // } BetterList <string> sprites = atlas.GetListOfSprites(UGUISettings.partialSprite); float size = 80f; float padded = size + 10f; int columns = Mathf.FloorToInt(Screen.width / padded) / 2; if (columns < 1) { columns = 1; } // Debug.LogWarning (columns); 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) { Sprite 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 (UGUISettings.selectedSprite != sprite.name) { if (mSprite != null) { UGUIEditorTools.RegisterUndo("Atlas Selection", mSprite); mSprite.SetNativeSize(); EditorUtility.SetDirty(mSprite.gameObject); } UGUISettings.selectedSprite = sprite.name; UGUIEditorTools.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 UGUIEditorTools.DrawTiledTexture(rect, UGUIEditorTools.backdropTexture); // Rect uv = new Rect(sprite.rect.x, sprite.rect.y, sprite.rect.width, sprite.rect.height); // uv = UGUIMath.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.DrawTexture(rect, sprite.texture); // Draw the selection if (UGUISettings.selectedSprite == sprite.name) { UGUIEditorTools.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(); } } }