4
\$\begingroup\$

I Have image with a sprite and i want to change this image sprite to Texture so that I can assign it to the material And i don't want to keep multiple copies for each image as a sprite and texture.

Is there any way to change sprite to texture at run time?

\$\endgroup\$
3
\$\begingroup\$
// assume "sprite" is your Sprite object
 var croppedTexture = new Texture2D( (int)sprite.rect.width, (int)sprite.rect.height );
 var pixels = sprite.texture.GetPixels(  (int)sprite.textureRect.x, 
                                         (int)sprite.textureRect.y, 
                                         (int)sprite.textureRect.width, 
                                         (int)sprite.textureRect.height );
 croppedTexture.SetPixels( pixels );
 croppedTexture.Apply();

this should work for you. you can follow this thread for more info: https://answers.unity.com/questions/651984/convert-sprite-image-to-texture.html

\$\endgroup\$
1
  • \$\begingroup\$ Thanks. I didn't need to crop or anything. but from thread you share i got my answer which was "Image.sprite.texture". \$\endgroup\$
    – Swati
    Sep 25 '19 at 6:36
1
\$\begingroup\$

For just converting Image's sprite To Texture use this:

Image.sprite.texture

And this will do the Job.

Thanks @virtouso for sharing the thread.

\$\endgroup\$
0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

#if UNITY_EDITOR
using UnityEditor;
#endif

public class SpriteToPNGFile : MonoBehaviour
{
    [SerializeField] private Sprite _sprite;
    public Sprite _Sprite => this._sprite;

    [SerializeField] private Texture2D _texturePreview;
    public Texture2D _Name => this._texturePreview;

    public void SaveTexture(Texture2D texture2D)
    {
        this._texturePreview = texture2D;

        byte[] bytes = texture2D.EncodeToPNG();

        string directoryPath = Application.dataPath + "/SpriteToPNGFileOutput";

        if (!Directory.Exists(directoryPath))
        {
            Directory.CreateDirectory(directoryPath);
        }

        File.WriteAllBytes(directoryPath + "/Tex_" + GUID.Generate() + ".png", bytes);

        Debug.Log(bytes.Length / 1024 + "Kb was saved as: " + directoryPath);

#if UNITY_EDITOR
        AssetDatabase.Refresh();
#endif
    }

    public void SaveSpriteAsTexture(Sprite sprite)
    {
        Texture2D texture = new Texture2D(
            (int)sprite.textureRect.width,
            (int)sprite.textureRect.height
        );

        Color[] pixels = sprite.texture.GetPixels(
            (int)sprite.textureRect.x,
            (int)sprite.textureRect.y,
            (int)sprite.textureRect.width,
            (int)sprite.textureRect.height
        );

        texture.SetPixels(pixels);
        texture.Apply();

        this.SaveTexture(texture2D: texture);
    }

#if UNITY_EDITOR
    [CustomEditor(inspectedType: typeof(SpriteToPNGFile))]
    public class SpriteToPNGFileEditor : Editor
    {
        private SpriteToPNGFile _target;

        private void OnEnable()
        {
            this._target = (SpriteToPNGFile)this.target;
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            if (GUILayout.Button("Generate PNG"))
            {
                this._target.SaveSpriteAsTexture(sprite: this._target._sprite);
            }
        }
    }
#endif
}
```
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.