// return true in case of success, else false protected bool UpdateRuntimeSpritesImplementation(float newScale) { List <KeyValuePair <SVGSpriteRef, SVGSpriteData> > sprites = new List <KeyValuePair <SVGSpriteRef, SVGSpriteData> >(); this.m_RuntimeTextures.Clear(); this.m_RuntimeSprites.Clear(); if (SVGRuntimeGenerator.GenerateSprites(// input this.m_SvgList, this.m_MaxTexturesDimension, this.m_SpritesBorder, this.m_Pow2Textures, newScale, this.m_ClearColor, this.m_FastUpload, this.m_GeneratedSpritesFiles, // output this.m_RuntimeTextures, sprites, // here we are not interested in getting, for each SVG document, the list of its generated sprites null)) { // create the runtime sprites dictionary foreach (KeyValuePair <SVGSpriteRef, SVGSpriteData> data in sprites) { this.m_RuntimeSprites.Add(data.Key, new SVGSpriteAssetFile("", data.Key, data.Value)); } this.m_RuntimeGenerationScale = newScale; return(true); } else { return(false); } }
public static bool GenerateSprites(// input List <SVGAssetInput> svgList, int maxTexturesDimension, int border, bool pow2Textures, float scale, Color clearColor, bool fastUpload, SVGSpritesDictionary previousSprites, // output List <Texture2D> textures, List <KeyValuePair <SVGSpriteRef, SVGSpriteData> > sprites, SVGSpritesListDictionary spritesList) { // create dictionaries Dictionary <int, PackedSvgAssetDocLink> processedAssets = new Dictionary <int, PackedSvgAssetDocLink>(); Dictionary <uint, PackedSvgDocRef> loadedDocuments = new Dictionary <uint, PackedSvgDocRef>(); // generate bins SVGPackedBin[] bins = SVGRuntimeGenerator.GenerateBins(svgList, maxTexturesDimension, border, pow2Textures, scale, processedAssets, loadedDocuments); if (bins != null) { // generate textures and sprites if (SVGRuntimeGenerator.GenerateSpritesFromBins(bins, loadedDocuments, scale, clearColor, fastUpload, previousSprites, textures, sprites, spritesList)) { return(true); } } return(false); }
//---------------------------------------------------------------------------------------------------------------------------- // Generate a sprite set, according a given canvas scale factor public bool GenerateSprites(float scaleFactor, List <Texture2D> textures, List <KeyValuePair <SVGSpriteRef, SVGSpriteData> > sprites) { if ((textures != null) && (sprites != null) && (scaleFactor > 0)) { return(SVGRuntimeGenerator.GenerateSprites( // input this.m_SvgList, this.m_MaxTexturesDimension, this.m_SpritesBorder, this.m_Pow2Textures, scaleFactor, this.m_ClearColor, this.m_FastUpload, this.m_GeneratedSpritesFiles, // output) textures, sprites, // here we are not interested in getting, for each SVG document, the list of its generated sprites null)); } return(false); }
// return true if case of success, else false public bool UpdateRuntimeSprites(int currentScreenWidth, int currentScreenHeight, out float scale) { float newScale = SVGRuntimeGenerator.ScaleFactorCalc((float)this.m_ReferenceWidth, (float)this.m_ReferenceHeight, currentScreenWidth, currentScreenHeight, this.m_ScaleType, this.m_Match, this.m_OffsetScale); if (Math.Abs(this.m_RuntimeGenerationScale - newScale) > Single.Epsilon) { scale = newScale; return(this.UpdateRuntimeSprites(newScale)); } else { scale = this.m_RuntimeGenerationScale; return(true); } }
// used by SVGAtlasEditor, upon the "Update" button click (recalcScale = true); this function is used even // in the build post-processor, in order to restore sprites (recalcScale = false) public void UpdateEditorSprites(bool recalcScale) { float newScale; if (recalcScale) { Vector2 gameViewRes = SVGUtils.GetGameView(); float currentWidth = (this.m_DeviceTestWidth <= 0) ? gameViewRes.x : (float)this.m_DeviceTestWidth; float currentHeight = (this.m_DeviceTestHeight <= 0) ? gameViewRes.y : (float)this.m_DeviceTestHeight; newScale = SVGRuntimeGenerator.ScaleFactorCalc((float)this.m_ReferenceWidth, (float)this.m_ReferenceHeight, currentWidth, currentHeight, this.m_ScaleType, this.m_Match, this.m_OffsetScale); } else { newScale = this.m_EditorGenerationScale; } this.UpdateEditorSprites(newScale); }
// Calculate the scale factor that would be used to generate sprites if the screen would have the specified dimensions public float ScaleFactorCalc(int currentScreenWidth, int currentScreenHeight) { return(SVGRuntimeGenerator.ScaleFactorCalc((float)this.m_ReferenceWidth, (float)this.m_ReferenceHeight, currentScreenWidth, currentScreenHeight, this.m_ScaleType, this.m_Match, this.m_OffsetScale)); }
private void UpdateEditorSprites(float newScale) { // get the list of instantiated SVG sprites List <GameObject> spritesInstances = new List <GameObject>(); this.GetSpritesInstances(spritesInstances); // regenerate the list of sprite locations this.m_GeneratedSpritesLists = new SVGSpritesListDictionary(); if (this.m_SvgList.Count <= 0) { AssetDatabase.StartAssetEditing(); // delete previously generated textures (i.e. get all this.GeneratedTextures entries and delete the relative files) this.DeleteTextures(); // delete previously generated sprites (i.e. get all this.GeneratedSprites entries and delete the relative files) this.DeleteSprites(); if (spritesInstances.Count > 0) { bool remove = EditorUtility.DisplayDialog("Missing sprite!", string.Format("{0} gameobjects reference sprites that do not exist anymore. Would you like to remove them from the scene?", spritesInstances.Count), "Remove", "Keep"); if (remove) { this.DeleteGameObjects(spritesInstances); } } AssetDatabase.StopAssetEditing(); // input SVG list is empty, simply reset both hash this.m_SvgListHashOld = this.m_SvgListHashCurrent = ""; return; } // generate textures and sprites List <Texture2D> textures = new List <Texture2D>(); List <KeyValuePair <SVGSpriteRef, SVGSpriteData> > sprites = new List <KeyValuePair <SVGSpriteRef, SVGSpriteData> >(); if (SVGRuntimeGenerator.GenerateSprites(// input this.m_SvgList, this.m_MaxTexturesDimension, this.m_SpritesBorder, this.m_Pow2Textures, newScale, this.m_ClearColor, this.m_FastUpload, this.m_GeneratedSpritesFiles, // output textures, sprites, this.m_GeneratedSpritesLists)) { int i, j; if ((this.m_EditorGenerationScale > 0) && (newScale != this.m_EditorGenerationScale)) { // calculate how much we have to scale (relative) positions float deltaScale = newScale / this.m_EditorGenerationScale; // fix objects positions and animations foreach (GameObject gameObj in spritesInstances) { this.FixPositions(gameObj, deltaScale, deltaScale); } } // keep track of the new generation scale this.m_EditorGenerationScale = newScale; AssetDatabase.StartAssetEditing(); // delete previously generated textures (i.e. get all this.GeneratedTextures entries and delete the relative files) this.DeleteTextures(); // delete previously generated sprites (i.e. get all this.GeneratedSprites entries and delete the relative files) this.DeleteSprites(); // ensure the presence of needed subdirectories string atlasesPath = this.CreateOutputFolders(); string texturesDir = atlasesPath + "/Textures/"; string spritesDir = atlasesPath + "/Sprites/"; // save new texture assets i = 0; foreach (Texture2D texture in textures) { string textureFileName = texturesDir + "texture" + i + ".asset"; // save texture AssetDatabase.CreateAsset(texture, textureFileName); // DEBUG STUFF //byte[] pngData = texture.EncodeToPNG(); //if (pngData != null) // System.IO.File.WriteAllBytes(texturesDir + "texture" + i + ".png", pngData); // keep track of the saved texture this.m_GeneratedTexturesFiles.Add(new AssetFile(textureFileName, texture)); i++; } // save sprite assets j = sprites.Count; for (i = 0; i < j; ++i) { // get sprite reference and its pivot SVGSpriteRef spriteRef = sprites[i].Key; SVGSpriteData spriteData = sprites[i].Value; // build sprite file name string spriteFileName = spritesDir + spriteData.Sprite.name + ".asset"; // save sprite asset AssetDatabase.CreateAsset(spriteData.Sprite, spriteFileName); // keep track of the saved sprite and its pivot this.m_GeneratedSpritesFiles.Add(spriteRef, new SVGSpriteAssetFile(spriteFileName, spriteRef, spriteData)); } AssetDatabase.StopAssetEditing(); // for already instantiated (SVG) game object, set the new sprites // in the same loop we keep track of those game objects that reference missing sprites (i.e. sprites that do not exist anymore) List <GameObject> missingSpriteObjs = new List <GameObject>(); foreach (GameObject gameObj in spritesInstances) { SVGSpriteAssetFile spriteAsset; SVGSpriteLoaderBehaviour spriteLoader = (SVGSpriteLoaderBehaviour)gameObj.GetComponent <SVGSpriteLoaderBehaviour>(); if (spriteLoader.SpriteReference.TxtAsset != null) { if (this.m_GeneratedSpritesFiles.TryGetValue(spriteLoader.SpriteReference, out spriteAsset)) { // link the new sprite to the renderer SpriteRenderer renderer = (SpriteRenderer)gameObj.GetComponent <SpriteRenderer>(); if (renderer != null) { SVGSpriteData spriteData = spriteAsset.SpriteData; // assign the new sprite renderer.sprite = spriteData.Sprite; // NB: existing instances do not change sorting order! } } else { missingSpriteObjs.Add(gameObj); } } } if (missingSpriteObjs.Count > 0) { bool remove = EditorUtility.DisplayDialog("Missing sprite!", string.Format("{0} gameobjects reference sprites that do not exist anymore. Would you like to remove them from the scene?", missingSpriteObjs.Count), "Remove", "Keep"); if (remove) { this.DeleteGameObjects(missingSpriteObjs); } } // now SVG documents are instantiable foreach (SVGAssetInput svgAsset in this.m_SvgList) { svgAsset.Instantiable = true; } // keep track of the new hash this.m_SvgListHashOld = this.m_SvgListHashCurrent; } }