1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| public static int propertyScaleImage = 1;
public static Texture2D CopyTextureByBlit(Texture2D source, RenderTexture renderTex = null, Material sourceMaterial = null) {
Material material = new Material(sourceMaterial); material.SetVector("_ClipRect", new Vector4(0, 0, 0, 1));
RenderTexture currentCameraRenderTexture = null; if (renderTex == null) { if (Camera.main != null) { currentCameraRenderTexture = Camera.main.targetTexture; } if (currentCameraRenderTexture != null) { Camera.main.targetTexture = null; } }
Graphics.Blit(source, renderTex, material);
RenderTexture previous = RenderTexture.active; RenderTexture.active = renderTex;
Texture2D readableTexture = new Texture2D(source.width, source.height); readableTexture.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
RenderTexture.active = previous; if (Camera.main != null && currentCameraRenderTexture != null) { Camera.main.targetTexture = currentCameraRenderTexture; }
return readableTexture; }
|