using UnityEngine; public class RenderTextureColorPicker : MonoBehaviour { [SerializeField] private RenderTexture _target; private Texture2D _texture; private void Awake(){ _texture = new Texture2D(_target.width, _target.height); } private void OnGUI(){ GUI.DrawTexture(new Rect(1000, 500, 240, 240), _target, ScaleMode.StretchToFill); } public int GetWidth(){ return _target.width; } public int GetHeight(){ return _target.height; } public Color32[] GetPixels() { // アクティブなレンダーテクスチャをキャッシュしておく var currentRT = RenderTexture.active; // アクティブなレンダーテクスチャを一時的にTargetに変更する RenderTexture.active = _target; // Texture2D.ReadPixels()によりアクティブなレンダーテクスチャのピクセル情報をテクスチャに格納する //var texture = new Texture2D(_target.width, _target.height); _texture.ReadPixels(new Rect(0, 0, _target.width, _target.height), 0, 0); _texture.Apply(); // ピクセル情報を取得する var colors = _texture.GetPixels32(); // アクティブなレンダーテクスチャを元に戻す RenderTexture.active = currentRT; return colors; } }