I am looking for some advice. I am using SpriteManager to move sprites around that are mapped with different photos using a big UVAtlas on the SpriteManager material’s texture.
That is working well. Now I really need to swap out some of the photos in the UVAtlas with photos that I bring in using the camera or gallery functionality exposed in bliprob’s Unity Enhancement Pack add on.
I can swap out the whole texture using code similar to the example code that bliprob provided. However I would rather write using GetPixels/SetPixels the pixels from the Texture2D brought in from Obj-C into the current SpriteManager’s Material’s texture.
Does this sound possible?
I have been trying for a while and am no longer getting errors but it doesn’t seem to work or at least the texture is not updating on the device. Here’s some code I was trying based around the bliprob example but converted to c# and with attempt to get/set pixels:
void LoadImage(string path) {
System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] thebytes = new byte[fs.Length];
int temper = (int)fs.Length;
fs.Read(thebytes, 0, temper);
Texture2D texture = new Texture2D(1,1);
texture.LoadImage(thebytes);
Color[] pixels =texture.GetPixels (0,0,160,120,0);
GameObject sprManager = (GameObject) GameObject.Find("SpritesHolder");
Texture2D spriteTexture = (Texture2D) Instantiate(sprManager.renderer.material.mainTexture);
spriteTexture.SetPixels (0,0,160,120,pixels,0);
spriteTexture.Apply();
sprManager.renderer.material.mainTexture = spriteTexture;
}
This executes OK on device at least i am not getting any errors but the Texture does not seem to update ever. I can use a similar method to swap out the whole texture so i know it’s coming in but i can’t seem to set a region dynamically. I have set the GetPixels/SetPixels option in the import settings also. I’m prolly missing something though as I’m a noob to both Unity and C#,
Any one have any advice or clues?
Thanks
What about the compression on the image?
GetPixel:
This function works only on ARGB32, RGB24 and Alpha8 texture formats. For other formats it always returns opaque white.
SetPixel:
This function works only on ARGB32, RGB24 and Alpha8 texture formats. For other formats SetPixel is ignored.
There’s probably PVRTC compression going on - check your import settings for the texture.
Of course, having a big uncompressed texture on the sprite manager is going to eat a lot of memory… and before you ask, no, unfortunately there’s no way to compress to PVRTC on the iPhone. Unless of course, you write your own routine to do so, but it’d be dastardly slow.
the problem might be the texture size. as far as I remember, the minimum texture size on the iphone is 32x32 (and at max 1024x1024!), optimally you would naturally set it to the size of the texture you want to load.
Thanks for the replies.
I’m using RGBA uncompressed for the texture.
The original texture is 1024 x 1024 and I’m trying to write in a region of 160x120 using SetPixels back into that 1024x1024 texture… I will try to mess with a smaller original texture though and see if that helps.
I was able to get this working by offsetting the y position of the SetPixels command. Not sure why that go switched up, guess I don’t fully understand what is going.
In case it might help someone, here is the code that ended up working:
void LoadImage(string path) {
System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] thebytes = new byte[fs.Length];
int temper = (int)fs.Length;
fs.Read(thebytes, 0, temper);
Texture2D texture = new Texture2D(1,1);
texture.LoadImage(thebytes);
Color[] pixels =texture.GetPixels (0,0,128,128,0);
GameObject sprManager = (GameObject) GameObject.Find("SpritesHolder");
Texture2D spriteTexture = (Texture2D)Instantiate(sprManager.renderer.material.mainTexture);
sprManager.renderer.material.mainTexture = spriteTexture;
// offset the y position (original texture height minus height of SetPixels rect)
spriteTexture.SetPixels (0,512-128,128,128,pixels,0);
spriteTexture.Apply();
}
With that I can now update the SpriteManager’s Texture dynamically with photos from the iphone camera or images from the users library. Thanks to blitrob’s Enhancement Pack for making that sooo easy!