baobao
12/27/2018 - 2:49 AM

RenderTextureのメモリ確保と解放確認コード

using System.Collections.Generic;
using UnityEngine;

public class RenderTextureMemoryAlloc : MonoBehaviour
{
    public Texture tex;

    [SerializeField]
    private List<RenderTexture> _rtList = new List<RenderTexture>();

    void CreateRT(int cnt = 2)
    {
        int size = 2048;
        for (int i = 0; i < cnt; i++)
        {
            var rt = new RenderTexture(size, size, 24, RenderTextureFormat.ARGB32);
            _rtList.Add(rt);
        }
    }

    void StartBlit()
    {
        for (int i = 0; i < _rtList.Count; i++)
        {
            Graphics.Blit(tex, _rtList[i]);
        }
    }

    void ReleaseRT()
    {
        for (int i = 0; i < _rtList.Count; i++)
        {
            _rtList[i].Release();
        }
    }

    void DestroyRT()
    {
        for (int i = 0; i < _rtList.Count; i++)
        {
            Destroy(_rtList[i]);
        }

        _rtList.Clear();
    }

    void OnGUI()
    {
        var w = 200f;
        var h = 100f;
        if (GUILayout.Button("CreateRT", GUILayout.Width(w), GUILayout.Height(h)))
        {
            CreateRT();
        }

        if (GUILayout.Button("CreateRT 50", GUILayout.Width(w), GUILayout.Height(h)))
        {
            // 試しに50枚作ってみる
            CreateRT(50);
        }

        if (GUILayout.Button("StartBlit", GUILayout.Width(w), GUILayout.Height(h)))
        {
            StartBlit();
        }

        if (GUILayout.Button("ReleaseRT", GUILayout.Width(w), GUILayout.Height(h)))
        {
            ReleaseRT();
        }

        if (GUILayout.Button("DestroyRT", GUILayout.Width(w), GUILayout.Height(h)))
        {
            DestroyRT();
        }
    }
}