Skip to content

The SaveToPNG(string) method

Narek Torosyan edited this page Nov 25, 2018 · 1 revision

About

This method extends Texture2D.

This method saves a texture as a PNG file.

This method requires a path string.

Example

// This script saves a sprite as PNG.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NUDev.PlusSharp;

public class SaveSpriteAsPNG : MonoBehaviour {

    public Sprite sprite;

    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(KeyCode.S))
        {
            StartCoroutine(Capture());
        }
    }

    IEnumerator Capture()
    {
        yield return new WaitForEndOfFrame();
        Texture2D spr = sprite.texture;
        spr.SaveToPNG(Application.persistentDataPath + "/mySprite.png");
    }
}

Clone this wiki locally