I have a behaviour script attached to a plane and I’m trying to draw a gradient across it. This works intermittently when I run it within Unity, but on the iPhone or dekstop player I produces a very squashed gradient, in fact almost completely red.
What am I not understanding here?
Globals:
public int width = 480;
public int height = 320;
private Texture2D tex;
So in Start() I have:
renderer.material.color = new Color(0xFF,0xFF,0xFF,0xFF);
tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
renderer.material.mainTexture = tex;
In Update() I have:
Color[] val = new Color[width * height];
int row = 0;
for (int i = 0 ; i < width*height ; ++i)
{
row = (int)(i/(float)width);
float norm_row = row/(float)height;
val[i] = new Color(norm_row,0,0);
}
tex.SetPixels(val);
tex.Apply();
Hm, I’ve experienced SetPixels is a slow process, so having it in your Update loop would make your iPhone think really really hard. Just a possibility, I could be wrong. Can you put your code in code tags? Makes it easier to debug when everything doesn’t have the same indentation.
I’m not sure where else I could put this? I’m concerned about speed as well.
I’m actually using AVFoundation to capture video and display in Unity so I can make an AR app. This approach has been used elsewhere. But if anyone has any other ideas I’d love to hear them! I’d prefer just to render with a transparent background, but that doesn’t seem possible?
There’s no way that will run more than a couple frames per second or so. SetPixels() and Apply() every frame on such a large texture (relatively speaking) will choke an iPhone 4, never mind slower models. Unless this is only part of the code, the texture doesn’t change after it’s created, so just put it in Start so it’s only done once.
Unfortunately if will change as I’m grabbing camera frames and applying it to the texture. Is there another approach I could be using to make a camera background?
I’m still using GUITextures, but as was predicted it was really slow. About 24ms for the SetPixels/Apply on my MacBook and horrendously slow on the iPhone.