/// <summary> /// 压缩UIAtlas /// </summary> /// <param name="uiatlasobj"></param> /// <returns></returns> public static ShrinkResult ShrinkAtlas(Object uiatlasobj) { AtlasProperty originalAtlasProp = null; ShrinkResult ret = prepareShrinkAtlas(uiatlasobj, ref originalAtlasProp); if (ret != ShrinkResult.Success) { return(ret); } UIAtlas uiatlas = ((GameObject)uiatlasobj).GetComponent <UIAtlas>(); BetterList <string> sl = uiatlas.GetListOfSprites(); //备份原始尺寸 originalAtlasProp = GetAtlasProperty(uiatlas, true); float times = 2f; uiatlas.scale /= times; foreach (string sn in sl) { UISpriteData sd = uiatlas.GetSprite(sn); sd.x /= (int)times; sd.y /= (int)times; sd.width /= (int)times; sd.height /= (int)times; sd.borderBottom /= (int)times; sd.borderLeft /= (int)times; sd.borderRight /= (int)times; sd.borderTop /= (int)times; sd.paddingTop /= (int)times; sd.paddingBottom /= (int)times; sd.paddingLeft /= (int)times; sd.paddingRight /= (int)times; } uiatlas.pixelSize *= times; uiatlas.MarkAsDirty(); AtlasProperty currentAtlasProp = GetAtlasProperty(uiatlas); SaveAtlasData(uiatlas, originalAtlasProp, currentAtlasProp); return(ShrinkResult.Success); }
/// <summary> /// 为压缩uiatlas做准备,包括压缩贴图及备份原始数据 /// </summary> /// <param name="uiatlasobj"></param> /// <param name="originalAtlasProp">用于存储uiatlas的原始数据</param> /// <returns></returns> private static ShrinkResult prepareShrinkAtlas(Object uiatlasobj, ref AtlasProperty originalAtlasProp) { UIAtlas uiatlas = ((GameObject)uiatlasobj).GetComponent <UIAtlas>(); string assetPath = AssetDatabase.GetAssetPath(uiatlas.gameObject); if (AtlasAlreadyShrinked(assetPath)) {//已经处理过 return(ShrinkResult.Success); } if (null == uiatlas.spriteMaterial || null == uiatlas.spriteList) { return(ShrinkResult.Failure); } BetterList <string> sl = uiatlas.GetListOfSprites(); if (sl == null) { return(ShrinkResult.Failure); } //先把纹理都缩了 Object[] dependObjs = new Object[] { uiatlas.spriteMaterial.mainTexture }; //EditorUtility.CollectDependencies(new Object[] { uiatlas.gameObject }); //获取依赖的资源 List <Texture2D> textureList = new List <Texture2D>(); for (int i = 0; i < dependObjs.Length; i++) {//搜集依赖的纹理资源 if (dependObjs[i] is Texture2D) { textureList.Add(dependObjs[i] as Texture2D); } } for (int index = 0; index < textureList.Count; ++index) { ShrinkResult sret = ShrinkTexture(textureList[index]); if (sret != ShrinkResult.Success) { RecoverUIAtlasFromMeta(uiatlasobj); return(ShrinkResult.Failure); } } return(ShrinkResult.Success); }