using System; using System.Collections.Generic; using UnityEngine; public class RenderTextureCache { public static bool CheckSupportsRenderTextureFormat(RenderTextureFormat tex_format) { if (!RenderTextureCache.supports_format_dic_.ContainsKey(tex_format)) { RenderTextureCache.supports_format_dic_.Add(tex_format, SystemInfo.SupportsRenderTextureFormat(tex_format)); } return RenderTextureCache.supports_format_dic_[tex_format]; } public RenderTexture GetTexture(int width, int height, RenderTextureFormat tex_format, KeyValuePair> recreate_func) { if (!this.render_tex_dic_.ContainsKey(width)) { this.render_tex_dic_.Add(width, new Dictionary>()); } Dictionary> dictionary = this.render_tex_dic_[width]; if (!dictionary.ContainsKey(height)) { dictionary.Add(height, new List()); } List list = dictionary[height]; RenderTextureCache.Data data = null; for (int i = 0; i < list.Count; i++) { if (list[i].status == RenderTextureCache.Data.Status.Free && list[i].tex.format == tex_format) { data = list[i]; break; } } if (data == null) { data = new RenderTextureCache.Data(); list.Add(data); data.size = new Vector2((float)width, (float)height); data.tex = this.CreateTex(width, height, tex_format); this.render_tex_list_.Add(data); } data.recreate_func = recreate_func; RenderTexture active = RenderTexture.active; data.tex.Create(); RenderTexture.active = active; data.status = RenderTextureCache.Data.Status.Use; return data.tex; } public void RemoveTexture(RenderTexture texture) { if (texture == null) { return; } foreach (KeyValuePair>> keyValuePair in this.render_tex_dic_) { Dictionary> value = keyValuePair.Value; foreach (KeyValuePair> keyValuePair2 in value) { List value2 = keyValuePair2.Value; for (int i = 0; i < value2.Count; i++) { if (value2[i].tex == texture) { value2[i].tex.Release(); value2[i].recreate_func = new KeyValuePair>(0, null); value2[i].status = RenderTextureCache.Data.Status.Free; return; } } } } } public void Update() { List list = null; for (int i = 0; i < this.render_tex_list_.Count; i++) { if (this.render_tex_list_[i].status == RenderTextureCache.Data.Status.Use && !this.render_tex_list_[i].tex.IsCreated()) { if (list == null) { list = new List(); } list.Add(this.render_tex_list_[i]); } } if (list != null) { SortedDictionary> sortedDictionary = new SortedDictionary>(); for (int j = 0; j < list.Count; j++) { int key = list[j].recreate_func.Key; if (!sortedDictionary.ContainsKey(key)) { sortedDictionary.Add(key, new List()); } sortedDictionary[key].Add(list[j]); } foreach (KeyValuePair> keyValuePair in sortedDictionary) { List value = keyValuePair.Value; for (int k = 0; k < value.Count; k++) { RenderTexture active = RenderTexture.active; value[k].tex.Create(); RenderTexture.active = active; value[k].recreate_func.Value(value[k].tex); } } } } private RenderTexture CreateTex(int width, int height, RenderTextureFormat tex_format) { RenderTexture active = RenderTexture.active; RenderTexture renderTexture = new RenderTexture(width, height, 0, tex_format); renderTexture.Create(); RenderTexture.active = active; return renderTexture; } private static Dictionary supports_format_dic_ = new Dictionary(); private Dictionary>> render_tex_dic_ = new Dictionary>>(); private List render_tex_list_ = new List(); private class Data { public RenderTexture tex; public Vector2 size; public RenderTextureCache.Data.Status status; public KeyValuePair> recreate_func; public enum Status { Free, Use } } }