Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
ResourceCache.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using System.IO;
3using UnityEngine;
4
5public class ResourceCache
6{
8
9 public static T Load<T>(string path) where T : Object
10 {
11 return caches.Get<T>(path);
12 }
13
14 public static T LoadBundle<T>(string path) where T : Object
15 {
16 return caches.Get<T>(path, ResourceLoadType.AssetBundle);
17 }
18}
19public class ResourceCache<T> where T : Object
20{
21 public Dictionary<string, T> dict = new Dictionary<string, T>();
22
23 public string path;
24
25 public ResourceCache(string _path = "")
26 {
27 path = _path;
28 }
29
30 public void Clear()
31 {
32 foreach (T value in dict.Values)
33 {
34 Object.DestroyImmediate(value);
35 }
36 dict.Clear();
37 }
38
39 public T Get(string id, ResourceLoadType loadType = ResourceLoadType.Resource, object option = null)
40 {
41 T value = null;
42 string key = typeof(T).Name + id;
43 if (!dict.TryGetValue(key, out value))
44 {
45 value = ((loadType == ResourceLoadType.Streaming) ? IO.LoadObject<T>(path + id, option) : Resources.Load<T>(path + id));
46 dict.Add(key, value);
47 }
48 return value;
49 }
50
51 public T2 Get<T2>(string id, ResourceLoadType loadType = ResourceLoadType.Resource) where T2 : Object
52 {
53 T value = null;
54 string key = typeof(T2).Name + id;
55 if (!dict.TryGetValue(key, out value))
56 {
57 T2 val = null;
58 val = ((loadType != 0) ? AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/Bundle/" + id).LoadAsset<GameObject>(new FileInfo(id).Name).GetComponent<T2>() : Resources.Load<T2>(path + id));
59 dict.Add(key, val as T);
60 return val;
61 }
62 return value as T2;
63 }
64}
ResourceLoadType
static ResourceCache< Object > caches
Definition: ResourceCache.cs:7
static T LoadBundle< T >(string path)
static T Load< T >(string path)
Definition: ResourceCache.cs:9
ResourceCache(string _path="")
Dictionary< string, T > dict
T Get(string id, ResourceLoadType loadType=ResourceLoadType.Resource, object option=null)
T2 Get< T2 >(string id, ResourceLoadType loadType=ResourceLoadType.Resource)