static void clearAllTextures() { foreach (var sheet in SKTextureUtil.getAllSpriteSheets()) { sheet.material.mainTexture = null; } }
public override void OnInspectorGUI() { //base.OnInspectorGUI(); _sprite = (SKSprite)target; // sprite properties if (_sprite.spriteSheet != null) { _sprite.desiredSize = EditorGUILayout.Vector2Field("Desired Size", _sprite.desiredSize); if (GUILayout.Button("Set Size to Image Size")) { Undo.RegisterUndo(_sprite, "Undo Make Pixel Perfect"); _sprite.desiredSize = _sprite.pixelPerfectHDSize; GUI.changed = true; } } _sprite.tintColor = EditorGUILayout.ColorField("Tint Color", _sprite.tintColor); _sprite.anchor = anchorSelector(_sprite.anchor, "Sprite Anchor"); // sprite sheet var spriteSheets = SKTextureUtil.getAllSpriteSheets().Select(x => x.name).ToList(); var spriteSheetSeletedIndex = 0; if (_sprite.spriteSheet == null) { spriteSheets.Insert(0, "Choose Sprite Sheet"); } else { // we have a sprite sheet. we want it to be selected spriteSheetSeletedIndex = Array.IndexOf(spriteSheets.ToArray(), _sprite.spriteSheet.name); } EditorGUILayout.BeginHorizontal(); GUILayout.Label("Sprite Sheet"); var newSpriteSheetIndex = EditorGUILayout.Popup(spriteSheetSeletedIndex, spriteSheets.ToArray()); EditorGUILayout.EndHorizontal(); if (newSpriteSheetIndex != spriteSheetSeletedIndex) { _sprite.spriteSheet = SKTextureUtil.getAllSpriteSheets().Where(x => x.name == spriteSheets[newSpriteSheetIndex]).Select(x => x).First(); _sprite.renderer.material = _sprite.spriteSheet.material; _sprite.sourceImageName = _sprite.spriteSheet.containedImages[0]; } if (_sprite.spriteSheet != null) { var currentIndex = -1; var currentImage = _sprite.spriteSheet.containedImages.Where(item => _sprite.sourceImageName != null && item.EndsWith(_sprite.sourceImageName)).FirstOrDefault(); if (currentImage != null) { currentIndex = Array.IndexOf(_sprite.spriteSheet.containedImages, currentImage); } EditorGUILayout.BeginHorizontal(); GUILayout.Label("Sprite Name"); var newIndex = EditorGUILayout.Popup(currentIndex, _sprite.spriteSheet.containedImages); EditorGUILayout.EndHorizontal(); if (currentIndex != newIndex) { var selectedImage = _sprite.spriteSheet.containedImages[newIndex]; var textureInfo = _sprite.spriteSheet.textureInfoForSourceImage(selectedImage); _sprite.pixelPerfectHDSize = textureInfo.size; _sprite.desiredSize = textureInfo.size; _sprite.sourceImageName = selectedImage; _sprite.Awake(); } } GUILayout.Space(10); if (_sprite.spriteSheet != null) { GUILayout.BeginHorizontal(); if (GUILayout.Button("Refresh Sprite Sheet")) { _sprite.spriteSheet.refreshSourceImages(); refreshAllSprites(); } // save colors so we can reset after making the destructive button var previousContentColor = GUI.contentColor; var previousBGColor = GUI.backgroundColor; GUI.contentColor = Color.red; GUI.backgroundColor = Color.red; if (GUILayout.Button("Delete Sprite Sheet")) { if (EditorUtility.DisplayDialog("SpriteKit Destruction Action Warning", "Are you sure you want to delete this sprite sheet?", "Yes, I'm Sure", "No!")) { // kill all assets associated with this sprite sheet var material = _sprite.spriteSheet.getMaterial(false); AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(material.mainTexture)); if (_sprite.spriteSheet.hasHdAtlas) { material = _sprite.spriteSheet.getMaterial(true); AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(material.mainTexture)); } // all done with the textures so now we kill the material AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(material)); // find any animations that reference this sprite sheet var allSpriteAnimations = SKTextureUtil.getAllSpriteAnimations().Where(anim => anim.spriteSheet == _sprite.spriteSheet).ToArray(); foreach (var animation in allSpriteAnimations) { AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(animation)); } AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(_sprite.spriteSheet)); _sprite.spriteSheet = null; _sprite.sourceImageName = null; } } GUILayout.EndHorizontal(); GUI.contentColor = previousContentColor; GUI.backgroundColor = previousBGColor; } if (GUI.changed) { EditorUtility.SetDirty(target); _sprite.Awake(); } }
static void createAnimation() { var textures = new List <Texture2D>(); // first validate that we have only Texture2Ds foreach (var o in Selection.objects) { if (o is Texture2D) { textures.Add(o as Texture2D); } } if (textures.Count == 0) { EditorUtility.DisplayDialog("Cannot Create Sprite Animation", "You did not select any images fool", "OK. I'm sorry."); return; } // figure out what sprite sheet the images are associated with and bail if it is more than one var allSpriteSheets = SKTextureUtil.getAllSpriteSheets(); Func <string, string, SKSpriteSheet> spriteSheetWithImage = (imageName, texturePath) => { foreach (var sheet in allSpriteSheets) { if (texturePath == sheet.imageSourceFolder && sheet.containedImages.Contains(imageName)) { return(sheet); } } return(null); }; SKSpriteSheet spriteSheetForAnimation = null; var spriteWidth = textures[0].width; var spriteHeight = textures[0].height; foreach (var tex in textures) { var texturePath = AssetDatabase.GetAssetPath(tex); var theSheet = spriteSheetWithImage(Path.GetFileName(texturePath), Path.GetDirectoryName(texturePath)); if (theSheet == null) { EditorUtility.DisplayDialog("Cannot Create Sprite Animation", "An image was selected that is not in a SKSpriteSheet", "Close"); return; } if (spriteSheetForAnimation == null) { spriteSheetForAnimation = theSheet; } else { // if we found a different sprite sheet we stop if (spriteSheetForAnimation != theSheet) { EditorUtility.DisplayDialog("Cannot Create Sprite Animation", "Images from multiple SKSpriteSheets were chosen", "Close"); return; } } if (tex.height != spriteHeight || tex.width != spriteWidth) { EditorUtility.DisplayDialog("Cannot Create Sprite Animation", "Images of different sizes were found. All images must be the same size for an animation.", "Close"); return; } } var wizard = SKSpriteAnimationWizard.createWizard(); wizard.spriteSheet = spriteSheetForAnimation; wizard.animationFrames = textures.Select(t => Path.GetFileName(AssetDatabase.GetAssetPath(t))).OrderBy(t => t, new NaturalSortComparer <string>()).ToArray(); }