扩展:
//字典扩展类
public static class DicTool
{
//要扩展谁,第一个参数就写谁,前面加上this关键字
//这个函数并不是给DicTool类调用的,而是当作Dictionary的扩展函数
public static TValue TryGetValueByNN<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey type)
{
TValue v;
dic.TryGetValue(type, out v);
return v;
}
}
调用:
private Dictionary<UIPanelType,GameObject> PanelCach= new Dictionary<UIPanelType, GameObject>();
private NBasePanel GetPanel(UIPanelType panelType)
{
GameObject instPanel = PanelCach.TryGetValueByNN(panelType);
//判断缓存里面有没有,如果没有,创建新的,如果有拿缓存里的
if (instPanel == null){
//实例化面板预制物
instPanel = GameObject.Instantiate(Resources.Load(path)) as GameObject;
PanelCach.Add(panelType, instPanel);//将新面板添加到缓存里
Debug.Log("新创建");
}
else
{
Debug.Log("用的缓存");
}
}