4

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[])

Vamshi Krishna
  • 61
  • 1
  • 1
  • 2

1 Answers1

15

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;
    }
}
zambari
  • 4,797
  • 1
  • 12
  • 22
  • 1
    can you send me an example? – Vamshi Krishna Jul 09 '19 at 10:11
  • Thank yo so much , it really worked i'm able to get the texture resize now i'm trying to add this to another texture specific co-ordinates the one which i used in getting the resize co-ordinates.how can i copy this image data to that one? – Vamshi Krishna Jul 09 '19 at 12:56
  • hey its working but its totally getting a blur image after resize is there any way i should get correct image with resize and same pixels – Vamshi Krishna Jul 10 '19 at 05:42
  • it shouldn't blur, but its using a GPU do do the resising. what do you mean about coordinates? coortinates are not texture dependant (expressed in normalized form usually) – zambari Jul 10 '19 at 08:56
  • 1
    @PUBGxxTHORxx There will always be some distortion when you resize, as the computer can't create data that doesn't exist, so it has to interpolate pixel values that don't align to exact coordinates in the original image. – Draco18s no longer trusts SE Jul 11 '19 at 15:12
  • Can't Texture2D.Resize be utilised here? https://docs.unity3d.com/2019.4/Documentation/ScriptReference/Texture2D.Resize.html – Rajat Gupta Jan 18 '23 at 08:51
  • for some reason on weaker mobile devices it takes forever to do `Graphics.Blit` (25 seconds for ~10Mpx photos) – dankal444 Jul 12 '23 at 16:00