Unity ReadPixels()正在减慢游戏速度

时间:2015-02-22 01:03:43

标签: c# unity3d

我使用以下内容拍摄相机内容并将其写入素材。这适用于1台相机,但当我将它连接到4台相机并写入4种材料时,它会将FPS降至12 - 13 FPS。我能做些什么来优化这个?

using UnityEngine;
using System.Collections;

public class RenderToTexture : MonoBehaviour {

    public Material mat;
    [HideInInspector]
    public Texture2D renderedTexture;

    void Awake () {
        renderedTexture = new Texture2D(Screen.width, Screen.height);

        mat.mainTexture = renderedTexture;
    }

    void OnPostRender(){
        renderedTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.width), 0, 0);

        renderedTexture.Apply();
    }
}

1 个答案:

答案 0 :(得分:-1)

我能够得到它,我只需要使纹理更小。这似乎有效:

using UnityEngine;
using System.Collections;

public class RenderToTexture : MonoBehaviour {

    public Material mat;
    [HideInInspector]
    public Texture2D renderedTexture;

    void Start () {
        renderedTexture = new Texture2D(300, 300);
        mat.mainTexture = renderedTexture;
    }

    void OnPostRender(){
        renderedTexture.ReadPixels(new Rect((camera.pixelWidth - 300) / 2, (camera.pixelHeight - 300) / 2, camera.pixelWidth, camera.pixelHeight), 0, 0, false);
        renderedTexture.Apply();
    }
}