hch
2025-06-19 35f08360f1e07c7a8301f7b2031c0779e2998fb5
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
51
52
53
54
55
56
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class RenderTextureCreator : MonoBehaviour
{
    [SerializeField] Vector2 m_Size;
    [SerializeField] Camera m_TargetCamera;
    [SerializeField] bool m_Reuse = false;
 
    static Dictionary<Vector2, RenderTexture> m_RenderTexDict = new Dictionary<Vector2, RenderTexture>();
 
    private void Awake()
    {
        if (m_TargetCamera != null && m_TargetCamera.targetTexture == null)
        {
            RenderTexture rt;
            if (m_Reuse && GetRenderTexture(m_Size, out rt))
            {
                m_TargetCamera.targetTexture = rt;
                return;
            }
 
            rt = new RenderTexture(Mathf.RoundToInt(m_Size.x), Mathf.RoundToInt(m_Size.y), 16);
            switch (Application.platform)
            {
                case RuntimePlatform.OSXEditor:
                case RuntimePlatform.WindowsEditor:
                case RuntimePlatform.WindowsPlayer:
                    rt.format = RenderTextureFormat.ARGB32;
                    break;
                default:
                    rt.format = RenderTextureFormat.ARGB4444;
                    break;
            }
 
            rt.useMipMap = false;
            rt.wrapMode = TextureWrapMode.Clamp;
            rt.filterMode = FilterMode.Bilinear;
 
            m_TargetCamera.targetTexture = rt;
 
            if (!m_RenderTexDict.ContainsKey(m_Size))
            {
                m_RenderTexDict.Add(m_Size, rt);
            }
        }
 
    }
 
    public static bool GetRenderTexture(Vector2 _Size, out RenderTexture _tex)
    {
        return m_RenderTexDict.TryGetValue(_Size, out _tex);
    }
 
}