/// <summary> /// 从图集中获得sprite /// </summary> /// <param name="atlasName"></param> /// <param name="resName"></param> /// <returns></returns> public Sprite GetSprite(string atlasName, string resName) { ResourceData resourceData; if (!_resourceDataDic.TryGetValue(atlasName, out resourceData)) { resourceData = new ResourceData(); resourceData.Init(new string[] { atlasName }); _resourceDataDic.Add(atlasName, resourceData); Dictionary <string, Object> resMap = new Dictionary <string, Object>(); resourceData.datasDic = resMap; } Sprite sp = resourceData.GetObject(resName) as Sprite; // 若当前缓存中不存在,则从图集中创建一份 if (sp == null) { //TimeUtil.BeginSample("DEBUG_SPATLAS"); sp = CreateSpriteFromAtlas(atlasName, resName); if (sp != null) { Dictionary <string, Object> resMap = resourceData.datasDic as Dictionary <string, Object>; resMap.Add(resName, sp); } //TimeUtil.EndSample("DEBUG_SPATLAS"); //TimeUtil.LogSampleTick("DEBUG_SPATLAS", "Create Sp " + atlasName + "/" + resName + " cost {0}"); } return(sp); }
/// <summary> /// 根据resName拿到ResourceData /// <para>若没有,则创建一个新的</para> /// </summary> /// <param name="resName"></param> /// <returns></returns> private ResourceData GetOrCreateResourceData(string resName) { ResourceData data; if (!_resourceDataDic.TryGetValue(resName, out data)) { data = new ResourceData(); data.Init(new string[] { resName }); data.datasDic = new Dictionary <string, Object>();; } return(data); }
/// <summary> /// 根据配置解析资源,临时使用resource.load /// </summary> private void ParseResource(string packName, int count) { ResourceData resData = new ResourceData(); resData.Init(new string[] { packName }); Dictionary <string, Object> resMap = new Dictionary <string, Object>(); Texture2D texture; Sprite sp; for (int i = 0; i < count; i++) { texture = Resources.Load(packName + "/" + packName + "_" + i) as Texture2D; if (texture != null) { sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); resMap.Add(packName + "_" + i, sp); } } resData.datasDic = resMap; _resourceDataDic.Add(packName, resData); }
/// <summary> /// 解析一整个atlas /// </summary> /// <param name="atlasName"></param> public void ParseSpriteAtlas(string atlasName) { ResourceData resData = new ResourceData(); resData.Init(new string[] { atlasName }); Dictionary <string, Object> resMap = new Dictionary <string, Object>(); resData.datasDic = resMap; _resourceDataDic.Add(atlasName, resData); string path = "SpriteAtlas/" + atlasName; SpriteAtlas atlas = Resources.Load <SpriteAtlas>(path); if (atlas != null) { int len = atlas.spriteCount; Sprite[] sprites = new Sprite[len]; atlas.GetSprites(sprites); for (int i = 0; i < len; i++) { resMap.Add(sprites[i].name, sprites[i]); } } }