Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize a Texture2D using height and width?

i tried to resize the texture with size and width and its showing index out of width/height

the texture need to be resized because i'm using it on other texture to apply on particular co-ordinates so i'm not able to resize the texture graphics is my texture2D

graphics.Resize((horizontalx - horizontaly), (verticalx - verticaly), TextureFormat.RGBA32, false);//creating texture with height and width

SetPixels32 can only be called on a RGBA32 or BGRA32 texture but is being called on TextureFormat(12) UnityEngine.Texture2D:SetPixels32(Color32[])

like image 218
Vamshi Krishna Avatar asked Dec 18 '22 16:12

Vamshi Krishna


1 Answers

Easiest way to resize would probably be to call Graphics.Blit targetting a new RenderTexture and using your Texture2D as source. If you need it to be a Texture2D afterwards, you can call ReadPixels on the RenderTexture

using UnityEngine;
using UnityEngine.UI;

public class Resizer : MonoBehaviour {
    public Texture2D inputtexture2D;
    public RawImage rawImage;
    [ExposeMethodInEditor]
    void Start()
    {
        rawImage.texture=Resize(inputtexture2D,200,100);
    }
    Texture2D Resize(Texture2D texture2D,int targetX,int targetY)
    {
        RenderTexture rt=new RenderTexture(targetX, targetY,24);
        RenderTexture.active = rt;
        Graphics.Blit(texture2D,rt);
        Texture2D result=new Texture2D(targetX,targetY);
        result.ReadPixels(new Rect(0,0,targetX,targetY),0,0);
        result.Apply();
        return result;
    }
}
like image 98
zambari Avatar answered Dec 24 '22 00:12

zambari