Texture2D.Resize problem

I might be missing something obvious, but whenever I used Texture2D.Resize on a new texture thats been copied from another, it breaks the original. This doesn’t seem logical.
Here’s is the code snippet:

public Texture2D TextureBase;
    void Start()
    {
        Texture2D scratchTexture = new Texture2D(TextureBase.width, TextureBase.height,TextureFormat.RGB24,false);
        scratchTexture = TextureBase;
        scratchTexture.Resize(8, 4);
        scratchTexture.Apply(false);
}

Your code snippet will make TextureBase be 8x4 pixels in size, and it’s contents will be undefined. (Resize only changes the size of the texture, and the new contents are undefined).

You’ll probably leak one texture as well (you allocate new one, and then change the texture reference to point to the same object as TextureBase). Assignment operator on texture references just makes them point to the same object, it does not do a “copy textures”.

1 Like

ah - that was the key, I guessed it must’ve been a ref not copy.
Can we get around this using GetPixels and SetPixels, or is there a better way?

Thx
Shaun

I seam to have the same problem.

How can I copy a texture?

Thanks
~Carl Emil

Create a new texture of the same size. Use GetPixels on the old texture and pass them as SetPixels on the new texture.

I works! =)

// copy texture and use mipmaps //
var useMipMaps : boolean = true;
var slideTexture : Texture2D = new Texture2D( [url]www.texture.width[/url], [url]www.texture.height[/url], TextureFormat.RGB24, useMipMaps );
slideTexture.SetPixels( www.texture.GetPixels() );
slideTexture.Apply( useMipMaps );

This was the only way I could create mipmaps of a downloaded texture ( using the WWW class ). Please let me know if there is an easier way to do this.

~ce

If you simply want to generate mip maps you can use:

var tex = [url]www.texture;[/url]
tex.Apply( true );

Ha!! Beautiful =)

Ups! I was a bit too fast to open the champagne there. It doesn’t work for me. The mip maps are not generated unless I use Apply(true) on a copied texture.

I test it like this:

print( "before: " + [url]www.texture.mipmapCount[/url] );
var tex = [url]www.texture;[/url]
tex.Apply( true );
print( "after: " + tex.mipmapCount );
// returns "before: 1", "after: 1".
// should have returned "after: 11"

Oops you are right. Okay so the right way of doing it is this:

// Create empty texture set up to use mip maps
var tex = new Texture2D (4, 4, TextureFormat.ARGB32, true);
// Load image into mip mapped texture
www.LoadImageIntoTexture(tex);

Nice and simple. Thanks Joachim.
~ce