180 lines
6.1 KiB
C#
180 lines
6.1 KiB
C#
//**********************************************//
|
||
//作者:#AUTHOR#
|
||
//日期:#DATE#
|
||
//简述:#DESCRIPTION#
|
||
//*********************************************//
|
||
using UnityEngine;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Thousandto.Core.Asset;
|
||
using Thousandto.Core.Base;
|
||
|
||
namespace Thousandto.Plugins.Common
|
||
{
|
||
/// <summary>
|
||
/// 类ItemAndEquipAltasManager说明
|
||
/// </summary>
|
||
public class UIIconAltasManager
|
||
{
|
||
#region //常量
|
||
private const int Segmentation = 64;
|
||
private const string AltasName = "UINewIconAtlas";
|
||
#endregion
|
||
|
||
#region //私有变量
|
||
private Dictionary<int,UIIconAltas> _altasDic; //altas 字典,key为名字后面的后缀 比如public_0 这里的0;
|
||
#endregion
|
||
|
||
#region//公共接口
|
||
//初始化
|
||
public void Initialization()
|
||
{
|
||
_altasDic = new Dictionary<int , UIIconAltas>();
|
||
}
|
||
|
||
//反初始化
|
||
public void UnInitialization()
|
||
{
|
||
var enumber = _altasDic.GetEnumerator();
|
||
try
|
||
{
|
||
while ( enumber.MoveNext() )
|
||
{
|
||
if ( enumber.Current.Value.Altas != null )
|
||
{
|
||
UIPoolAssetsLoader.UnloadAtlas( enumber.Current.Value.AltasName , enumber.Current.Value.Altas.transform );
|
||
}
|
||
else
|
||
{
|
||
UIPoolAssetsLoader.UnloadAtlas( enumber.Current.Value.AltasName , null );
|
||
}
|
||
}
|
||
}
|
||
finally
|
||
{
|
||
enumber.Dispose();
|
||
}
|
||
_altasDic.Clear();
|
||
}
|
||
|
||
//获取altas
|
||
public UIIconAltas GetIconAltasWithIconId(int id, MyAction<UIAtlas> callBack)
|
||
{
|
||
UIIconAltas retAltas = null;
|
||
int index = GenerateIndexWithId(id);
|
||
_altasDic.TryGetValue(index, out retAltas);
|
||
if (null == retAltas)
|
||
{
|
||
//FLogger.LogError("UnUseAltasWithIconId:New", index);
|
||
retAltas = new UIIconAltas();
|
||
retAltas.Index = index;
|
||
retAltas.AltasName = UIPoolAssetsLoader.AtlasName(AltasName, index);
|
||
Debug.Log("yy GetIconAltasWithIconId id: " + id + " atlasName: " + retAltas.AltasName);
|
||
retAltas.RetainCount = 1;
|
||
retAltas.AddCallBack(callBack);
|
||
if (!_altasDic.ContainsKey(index))
|
||
{
|
||
_altasDic.Add(index, retAltas);
|
||
}
|
||
|
||
UIPoolAssetsLoader.LoadAtlasPrefabAsyn(AltasName, index, atlasTrans =>
|
||
{
|
||
//FLogger.LogError("GetIconAltasWithIconId ", AltasName, index, ";atlasTrans=", atlasTrans);
|
||
if (atlasTrans != null)
|
||
{
|
||
var itemAtlas = atlasTrans.GetComponent<UIAtlas>();
|
||
//FLogger.LogError("GetIconAltasWithIconId ", ";itemAtlas=", atlasTrans);
|
||
if (null != itemAtlas)
|
||
{
|
||
//FLogger.LogError("GetIconAltasWithIconId ", ";retAltas=", retAltas);
|
||
Debug.Log("yy GetIconAltasWithIconId retAltas is valid");
|
||
retAltas.Altas = itemAtlas;
|
||
retAltas.DoCallBack();
|
||
}
|
||
}
|
||
});
|
||
}
|
||
else
|
||
{
|
||
retAltas.AddCallBack(callBack);
|
||
retAltas.RetainCount++;
|
||
}
|
||
// Debug.Log(string.Format("GetIconAltasWithIconId:{0}:{1}", retAltas.AltasName, retAltas.RetainCount));
|
||
return retAltas;
|
||
}
|
||
|
||
//获取altas
|
||
public UIAtlas GetAltasWithIconId(int id, MyAction<UIAtlas> callBack)
|
||
{
|
||
UIIconAltas retAltas = GetIconAltasWithIconId(id, callBack);
|
||
return retAltas.Altas;
|
||
}
|
||
|
||
public void UnUseAltasWithIconId(int id, MyAction<UIAtlas> callBack)
|
||
{
|
||
//Debug.Log(string.Format("UnUseAltasWithIconId:{0}", id));
|
||
int index = GenerateIndexWithId( id );
|
||
UIIconAltas resAltas = null;
|
||
_altasDic.TryGetValue( index , out resAltas );
|
||
if ( null != resAltas )
|
||
{
|
||
resAltas.RemoveCallBack(callBack);
|
||
resAltas.RetainCount--;
|
||
//Debug.Log(string.Format("UnUseAltasWithIconId:{0}:{1}", resAltas.AltasName, resAltas.RetainCount));
|
||
}
|
||
|
||
}
|
||
//清除空引用的atlas
|
||
public void CleanupAltas()
|
||
{
|
||
List<UIIconAltas> removeList = new List<UIIconAltas>();
|
||
var e = _altasDic.GetEnumerator();
|
||
try
|
||
{
|
||
while (e.MoveNext())
|
||
{
|
||
if (e.Current.Value.RetainCount <= 0)
|
||
{
|
||
removeList.Add(e.Current.Value);
|
||
}
|
||
}
|
||
}
|
||
finally
|
||
{
|
||
e.Dispose();
|
||
}
|
||
|
||
for (int i = 0; i < removeList.Count; i++)
|
||
{
|
||
var resAltas = removeList[i];
|
||
if (resAltas.Altas != null)
|
||
{
|
||
UIPoolAssetsLoader.UnloadAtlas(resAltas.AltasName, resAltas.Altas.transform);
|
||
}
|
||
else
|
||
{
|
||
UIPoolAssetsLoader.UnloadAtlas(resAltas.AltasName, null);
|
||
}
|
||
_altasDic.Remove(resAltas.Index);
|
||
}
|
||
removeList.Clear();
|
||
}
|
||
#endregion
|
||
|
||
#region //私有函数
|
||
/// <summary>
|
||
/// 通过icon的索引值来计算atlas的索引
|
||
/// icon的索引值时从1开始
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
private int GenerateIndexWithId( int id )
|
||
{
|
||
int index = (id -1) / Segmentation + 1;
|
||
return index;
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
}
|