Hi everyone
I have a sprite created from a png texture of a screenshot, and I want to show the sprite in a scene with the same size (width and height), that the texture. How can I do that?
Thanks in advanced!!
Hi everyone
I have a sprite created from a png texture of a screenshot, and I want to show the sprite in a scene with the same size (width and height), that the texture. How can I do that?
Thanks in advanced!!
I’m guessing the texture is a Texture2D in that case you can access the property .width of the texture2D variable. Then set the sprite width to the same. Repeat for height.
Yes, is a Texture2D, but how can I change the width and the height of de sprite?
Is your texture2d size dynamic , or like a standard 1920x1080 16:9 ratio
I declare Texture2D:
var imageTexture : Texture2D = new Texture2D(2,2);
And then the sprite:
Sprite.Create(imagenTexture, new Rect(0,0,imagenTexture.width, imagenTexture.height*0.9), new Vector2(-0.2,0.2f), 7.0f);
Hey sorry for not replying , was the weekend and not going to lie but I totally forgot. Any way here is what I came width. Keep in mind the last number is pixel to unity measurements. So the grey boxes you see in the scene view is 1 unity measurement. Generally sprites come in at about 100 instead of your 7 but play around with the number as you see fit. If your texture size changes size then you could just multiple it by a modifier to get the correct size.
using UnityEngine;
using System.Collections;
public class ChangeWidth : MonoBehaviour
{
public Texture2D imageTexture;
private Sprite spriteImage;
private GameObject imageGameObject;
void Start()
{
//TODO: Instead of it being public you could also grab it here
spriteImage = (Sprite)Sprite.Create(imageTexture, new Rect(0, 0, imageTexture.width, imageTexture.height * 0.9f), new Vector2(-0.2f, 0.2f), 7.0f);
imageGameObject = new GameObject();
imageGameObject.name = "Sprite_" + Random.Range(0, 1000).ToString();
imageGameObject.AddComponent<SpriteRenderer>().sprite = spriteImage;
}
}