Tanoda
GlobalTextureHolder.cs
Go to the documentation of this file.
1using NaughtyAttributes;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using UnityEngine;
6
7public class GlobalTextureHolder : MonoBehaviour
8{
10 private Dictionary<string, Texture2D> FullRes;
11 private Dictionary<string, Texture2D> LowRes;
12 [ReadOnly] [SerializeField] private int numOfPictures = 0;
13 void Start()
14 {
15 if (instance != null)
16 {
17 Destroy(gameObject);
18 return;
19 }
20 instance = this;
21 DontDestroyOnLoad(gameObject);
22 FullRes = new Dictionary<string, Texture2D>();
23 LowRes = new Dictionary<string, Texture2D>();
24 }
25
26 public void AddTexture(string filename, Texture2D texture)
27 {
28 filename = filename.Replace(" (Clone)", "");
29 if (FullRes.ContainsKey(filename))
30 {
31 Destroy(texture);
32 Debug.LogWarning($"{filename} already in dicionary! destroying!");
33 return;
34 }
35#if UNITY_WEBGL
36 FullRes.Add(filename, texture);
37 Debug.Log($"Texture '{filename}' with x: {texture.width} y: {texture.height}");
38#endif
39 Texture2D copyTexture = new Texture2D(texture.width, texture.height);
40 copyTexture.SetPixels(texture.GetPixels());
41 copyTexture.Apply();
42 Macro.Resize(copyTexture, 128, 128);
43 LowRes.Add(filename, copyTexture);
44 numOfPictures++;
45 }
46
47 public bool HasTexture(string filename)
48 {
49 filename = filename.Replace("(Clone)", "").Trim();
50 return LowRes.ContainsKey(filename);
51 }
52
53 public Texture2D GetFullRes(string filename)
54 {
55 filename = filename.Replace("(Clone)", "").Trim();
56 if (FullRes.ContainsKey(filename))
57 {
58 Debug.Log("[GlobalTextureHolder] GetFullRes called on: " + filename + " size X: " + FullRes[filename].width);
59 return FullRes[filename];
60 }
61 if (File.Exists(Path.Combine(Application.streamingAssetsPath, "pictures", filename)))
62 {
63 Debug.Log("[GlobalTextureHolder] GetFullRes called on: " + filename);
64
65 var fileData = File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, "pictures", filename));
66 var tex = new Texture2D(1, 1);
67 tex.LoadImage(fileData);
68 FullRes.Add(filename, tex);
69 return tex;
70 }
71 Debug.LogWarning("GetFullRes returns with NULL!");
72 return null;
73 }
74 public Texture2D GetLowRes(string filename)
75 {
76 filename = filename.Replace("(Clone)", "").Trim();
77 if (LowRes.ContainsKey(filename))
78 {
79 return LowRes[filename];
80 }
81 return null;
82 }
83}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
bool HasTexture(string filename)
void AddTexture(string filename, Texture2D texture)
Texture2D GetLowRes(string filename)
Texture2D GetFullRes(string filename)
static GlobalTextureHolder instance
Definition: Macro.cs:12
static void Resize(Texture2D texture2D, int targetX, int targetY, bool mipmap=true, FilterMode filter=FilterMode.Bilinear)
Definition: Macro.cs:359