У меня возникли две проблемы:
1. При работе моего скрипта юнити начинает просто пожирать мою оперативу до 3,5 гигов!!!!! у меня всего то 4(((((((
2. Как сделать чтобы ReadPixels брал скрин именно с той камеры, которая мне нужна? ибо у меня изображение берется с маин камера, а камера которая нужна находится на самой оптике.
А вот и сам про скрипт от про скриптингера:
Синтаксис:
Используется csharp
void Start ()
{
camera.enabled = false;
camera.enabled = true;
tileSizeX = (int)camera.pixelWidth;
tileSizeY = (int)camera.pixelHeight;
screen = new Texture2D(tileSizeX, tileSizeY, TextureFormat.ARGB32, true);
renderer.material.mainTexture = screen;
}
void Update ()
{
camera.enabled = true;
screen = new Texture2D(tileSizeX, tileSizeY, TextureFormat.ARGB32, true);
StartCoroutine(Screenshot(screen, tileSizeX, tileSizeY, tileSizeX, tileSizeY));
camera.enabled = false;
//renderer.material.mainTexture = screen;
}
System.Collections.IEnumerator Screenshot(Texture2D tex, int tileWidth, int tileHeight, int destX, int destY)
{
// Read pixels to buffer location
yield return new WaitForEndOfFrame();
camera.enabled = true;
tex.ReadPixels(new Rect(0, 0, tileWidth, tileHeight), 0, 0);
tex.Apply();
renderer.material.mainTexture = tex;
//Copy pixels to dest in buffer
// Color background = camera.backgroundColor;
// Color alpha = background; alpha.a = 0.0f;
// for(int y = 0; y < tileHeight; y++)
// for(int x = 0; x < tileWidth; x++)
// {
// Color c = tex.GetPixel(x,y);
// if (c.r == background.r && c.g == background.g && c.b == background.b)
// tex.SetPixel(x + destX, y + destY, alpha);
// else
// tex.SetPixel(x + destX, y + destY, c);
// }
//Clear buffer data
// Color background = camera.backgroundColor;
// Color alpha = background; alpha.a = 0.0f;
// for(int y = 0; y < tileHeight; y++)
// for(int x = 0; x < tileWidth; x++)
// tex.SetPixel(x,y, alpha);
//camera.enabled = false;
//tex.Apply();
}
}
{
camera.enabled = false;
camera.enabled = true;
tileSizeX = (int)camera.pixelWidth;
tileSizeY = (int)camera.pixelHeight;
screen = new Texture2D(tileSizeX, tileSizeY, TextureFormat.ARGB32, true);
renderer.material.mainTexture = screen;
}
void Update ()
{
camera.enabled = true;
screen = new Texture2D(tileSizeX, tileSizeY, TextureFormat.ARGB32, true);
StartCoroutine(Screenshot(screen, tileSizeX, tileSizeY, tileSizeX, tileSizeY));
camera.enabled = false;
//renderer.material.mainTexture = screen;
}
System.Collections.IEnumerator Screenshot(Texture2D tex, int tileWidth, int tileHeight, int destX, int destY)
{
// Read pixels to buffer location
yield return new WaitForEndOfFrame();
camera.enabled = true;
tex.ReadPixels(new Rect(0, 0, tileWidth, tileHeight), 0, 0);
tex.Apply();
renderer.material.mainTexture = tex;
//Copy pixels to dest in buffer
// Color background = camera.backgroundColor;
// Color alpha = background; alpha.a = 0.0f;
// for(int y = 0; y < tileHeight; y++)
// for(int x = 0; x < tileWidth; x++)
// {
// Color c = tex.GetPixel(x,y);
// if (c.r == background.r && c.g == background.g && c.b == background.b)
// tex.SetPixel(x + destX, y + destY, alpha);
// else
// tex.SetPixel(x + destX, y + destY, c);
// }
//Clear buffer data
// Color background = camera.backgroundColor;
// Color alpha = background; alpha.a = 0.0f;
// for(int y = 0; y < tileHeight; y++)
// for(int x = 0; x < tileWidth; x++)
// tex.SetPixel(x,y, alpha);
//camera.enabled = false;
//tex.Apply();
}
}
вы пожалуста не пугайтесь от моего скила, но часть кода в коментах была с готового скрипта, но его работа мне не подходит. Но если это наведет вас, чтобы вы навели меня на путь истины , то пусть останется...
и скрипт не работает после попытки решения проблемы с оперативой, но оператива так и кушается(
ну а так скрипт по идее простяковый, но походу не для меня((((