Unity3D_Resources封装(ResourcesManager 类)

Unity3D_Resources封装(ResourcesManager 类)

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class ResourcesManager 
 6 {
 7     //如果说当前资源之前没有加载过的话,那么加载一遍,并且将该资源记录下来
 8     //如果当前资源之前 加载过的话,那么直接使用之前 加载过的资源
 9     static Dictionary<string, GameObject> resDic = new Dictionary<string, GameObject>();
10 
11     //非泛型用于加载预置体对象
12     public static GameObject Load(string path)
13     {
14         if (resDic.ContainsKey(path))
15         {
16             return resDic[path];
17         }
18 
19         // var goPrefab = Resources.Load("Prefab//BagUnit") as GameObject;
20         GameObject go = Resources.Load(path) as GameObject;
21         resDic[path] = go;
22 
23         return go;
24     }
25 
26     //------------------------------------------------
27     static Hashtable resTable = new Hashtable();
28 
29     //泛型约束,明确特化时,类型形参必须是继承于UnityEngine.Object
30     public static T Load<T>(string path) where T : UnityEngine.Object
31     {
32         if (resTable.ContainsKey(path))
33         {
34             return resTable[path] as T;
35         }
36 
37         //var goSprite = Resources.Load<Sprite>(iconPath);
38         T t = Resources.Load<T>(path);
39         resTable[path] = t;
40 
41         return t;
42     }
43 }
View Code

 

 

其作用最大的是性能优化 , 不用重复使用内存的读取。

ResourcesManager .Load和ResourcesManager .Load<T> ;

ResourcesManager中非泛型读取使用Dictionary 字典键值对 。 泛型使用Hashtable 字典键值对 做了明确特化的模式<T>: where T :UnityEngine.object ;

 

 

posted on 2019-07-03 09:56  嗜睡的熊大  阅读(360)  评论(0编辑  收藏  举报

导航