// 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);
        }
    }
//----------------------------------------------------------------------------------------------------------------------------

    // 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);
    }
Ejemplo n.º 3
0
    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;
        }
    }