Can Sprite be used as texture?

In my game (searching an object in space) I want to use a texture on a Quad object and a sprite on a UI Image where these two originate from the same png. So far I have made always a duplicate and one of the marked as Sprite texture type and the other as Default texture type. By a mistake I have once used a Sprite on a material for the quad and it worked.

The question is can I use Sprites for both the 3D object as well as for the 2D UI image? This will reduce some of my work since I plan to have large variety of such objects.

Thanks.

Hello there,

You can use the same png file for both a sprite on a UI Image, and a texture on a 3D plane.

The following is code I use in my projects.

// This turns a PNG at FilePath into a texture, and then creates a sprite from it.
public static Sprite LoadNewSprite(string FilePath, float pixelsPerUnit = 100.0f)
        {
            Sprite NewSprite = new Sprite();
            Texture2D SpriteTexture = LoadTexture(FilePath);
            if (SpriteTexture != null)
                NewSprite = Sprite.Create(SpriteTexture, new Rect(0, 0, SpriteTexture.width, SpriteTexture.height), new Vector2(0, 0), pixelsPerUnit);

            return NewSprite;
        }

//This returns a Texture2D from a PNG file at FilePath
 public static Texture2D LoadTexture(string FilePath)
        {
            Texture2D Tex2D;
            byte[] FileData;

            if (File.Exists(FilePath))
            {
                FileData = File.ReadAllBytes(FilePath);
                Tex2D = new Texture2D(2, 2);
                if (Tex2D.LoadImage(FileData))
                    return Tex2D;
            }
            UnityEngine.Debug.LogError("Tried to load texture at path: " + FilePath + " but failed.");
            return null;
        }

Then, all you have to do is slap that sprite on an image, or that texture onto a plane.

I hope this helps!

Cheers,

~LegendBacon