public static SKSprite createSprite(string sheetName, string imageName, SpriteAnchor anchor) { // find the sheet first var sheet = SKSpriteSheet.sheetWithName(sheetName); return(createSprite(sheet, imageName, anchor)); }
public static SKTextureInfo textureInfoForSourceImage(this SKSpriteSheet sheet, string sourceImageName) { foreach (var info in sheet.imageTextureInfo) { if (info.file == sourceImageName) { return(info); } } return(null); }
public static SKSprite createSprite( SKSpriteSheet sheet, string imageName, SpriteAnchor anchor ) { var targetGO = new GameObject( imageName ); var sprite = targetGO.AddComponent<SKSprite>(); sprite.spriteSheet = sheet; sprite.sourceImageName = imageName; sprite.anchor = anchor; var info = sheet.textureInfoForImage( imageName ); sprite.pixelPerfectHDSize = sprite.desiredSize = info.size; sprite.renderer.sharedMaterial = sheet.getMaterial( isDoubleDensityScreen ); sprite.generateMesh(); return sprite; }
public static SKSprite createSprite(SKSpriteSheet sheet, string imageName, SpriteAnchor anchor) { var targetGO = new GameObject(imageName); var sprite = targetGO.AddComponent <SKSprite>(); sprite.spriteSheet = sheet; sprite.sourceImageName = imageName; sprite.anchor = anchor; var info = sheet.textureInfoForImage(imageName); sprite.pixelPerfectHDSize = sprite.desiredSize = info.size; sprite.renderer.sharedMaterial = sheet.getMaterial(isDoubleDensityScreen); sprite.generateMesh(); return(sprite); }
private void OnPostprocessTexture(Texture2D texture) { // avoid processing multiple times if (_lastImport.HasValue && System.DateTime.Now.Subtract(_lastImport.Value).TotalSeconds < 20) { return; } // bail out if this change was because of the SKTextureUtil doing its job with a PSD if (SKTextureUtil.isCurrentlyRefreshingSourceImages) { return; } var filename = Path.GetFileName(assetPath); var sheet = SKSpriteSheet.sheetWithImageSourceFolder(Path.GetDirectoryName(assetPath)); if (sheet != null) { _lastImport = System.DateTime.Now; Debug.Log("SpriteKit detected that a texture updated (" + filename + ") from sprite sheet: " + sheet.name + ". Refreshing now."); sheet.refreshSourceImages(); } }
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(); }
public static void refreshSourceImages(this SKSpriteSheet sheet) { // set a flag while we import the PSD to avoid our AssetPostprocessor from getting an endless loop of death isCurrentlyRefreshingSourceImages = true; try { var textureInfoList = new List <SKTextureInfo>(); var files = Directory.GetFiles(sheet.imageSourceFolder).Where(f => !f.EndsWith(".meta")).ToArray(); var textures = new Dictionary <string, Texture2D>(files.Length); var texturesNotToDestroy = new List <string>(); var containedImages = new List <string>(); float progress = 1f; foreach (var f in files) { EditorUtility.DisplayProgressBar("Creating Sprite Atlases..", "processing image at path: " + f, progress++ / files.Length); var path = Path.Combine(sheet.imageSourceFolder, Path.GetFileName(f)); Texture2D tex = null; if (Path.GetExtension(path) == ".png") { // we load directly from disk so that the textures are guaranteed readable tex = new Texture2D(0, 0); tex.LoadImage(File.ReadAllBytes(f)); } else if (Path.GetExtension(path).ToLower() == ".psd" || Path.GetExtension(path) == ".gif") { var texImporter = TextureImporter.GetAtPath(path) as TextureImporter; texImporter.isReadable = true; AssetDatabase.ImportAsset(path); tex = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D; if (tex != null) { texturesNotToDestroy.Add(tex.name); } } if (tex != null) { textures.Add(Path.GetFileName(f), tex); containedImages.Add(Path.GetFileName(f)); } } sheet.containedImages = containedImages.ToArray(); // pack all the textures and make a lookup dictionary var textureArray = textures.Select(x => x.Value).ToArray(); var rects = SKTextureUtil.rebuildAtlas(textureArray, sheet.name.Replace("_sheet", string.Empty) + "_atlas", sheet.hasHdAtlas); var texToRect = new Dictionary <string, Rect>(textures.Count); for (var i = 0; i < textureArray.Length; i++) { var key = textures.Where(y => y.Value == textureArray[i]).Select(x => x.Key).First(); texToRect[key] = rects[i]; } // create our textureInfos foreach (var item in texToRect) { var tex = textures[item.Key]; var info = new SKTextureInfo(); info.file = item.Key; info.uvRect = item.Value; info.size = new Vector2(tex.width, tex.height); textureInfoList.Add(info); } // clean up textures foreach (var tex in textures) { // only destroy textures that we loaded from pngs if (!texturesNotToDestroy.Contains(tex.Value.name)) { GameObject.DestroyImmediate(tex.Value, true); } } textures.Clear(); // not sure why getting the asset path triggers a save but it does for some reason AssetDatabase.GetAssetPath(sheet); sheet.imageTextureInfo = textureInfoList.ToArray(); //AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); EditorUtility.SetDirty(sheet); } catch (System.Exception e) { Debug.LogError("Something went wrong creating the atlas: " + e); } finally { EditorUtility.ClearProgressBar(); isCurrentlyRefreshingSourceImages = false; } }