public Sprite LoadAtlas(string atlasName, string spriteName) { Debug.Log("LoadAtlas"); string abPath = $"{Application.streamingAssetsPath}/atlas/{atlasName}{TCommon.ABSUFFIX_AFTER}"; AssetBundle ab = AssetBundle.LoadFromFile(abPath); GameObject atlasPrefab = ab.LoadAsset <GameObject>(atlasName); atlasPrefab.SetParent(GameObject.Find("Canvas").transform); TUIAtlas tuiAtlas = atlasPrefab.GetComponent <TUIAtlas>(); TUIAtlasSheet sheet = tuiAtlas.sheet; TUiSpriteData data = new TUiSpriteData(); for (int i = 0; i < sheet.uispriteList.Count; i++) { if (sheet.uispriteList[i].spriteName == spriteName) { data = sheet.uispriteList[i]; break; } } Texture2D tx2D = ab.LoadAsset(atlasName) as Texture2D; //使用图集和精灵信息,加载出精灵 Sprite sprite = Sprite.Create(tx2D, data.rect, data.pivot, 100f, 0, SpriteMeshType.Tight, Vector4.zero); return(sprite); }
/// <summary> /// 解析图集 /// </summary> void AnalySheet(string _atlasName) { string atlasPath = $"{atlasResPath}/{_atlasName}"; string outPngPath = $"{atlasResPath}/{_atlasName}/{_atlasName}.png"; string outTxtPath = $"{atlasResPath}/{_atlasName}/{_atlasName}.txt"; string outTextAssetPath = $"{atlasResPath}/{_atlasName}/{_atlasName}.asset"; string outPrefabPath = $"{atlasResPath}/{_atlasName}/{_atlasName}.prefab"; if (Directory.Exists(outPrefabPath)) { Directory.Delete(outPrefabPath, true); } AssetDatabase.Refresh(); GameObject prefab = new GameObject(); Object o = PrefabUtility.CreateEmptyPrefab(BuildTool.RemovePathPrefix(outPrefabPath)); TUIAtlas tAtlas = prefab.AddComponent <TUIAtlas>(); TUIAtlasSheet tSheetList = ScriptableObject.CreateInstance <TUIAtlasSheet>(); AssetDatabase.CreateAsset(tSheetList, BuildTool.RemovePathPrefix(outTextAssetPath)); TextAsset txtAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(BuildTool.RemovePathPrefix(outTxtPath)); Debug.Log("Txt:" + txtAsset); string txtString = txtAsset.ToString(); string[] split = txtString.Split('\n'); foreach (var _curSplit in split) { if (_curSplit.Contains(";")) { string [] _dataSplit = _curSplit.Split(';'); TUiSpriteData data = new TUiSpriteData(); data.border = Vector4.zero; data.spriteName = _dataSplit[0]; float x = 0; float.TryParse(_dataSplit[1], out x); float y = 0; float.TryParse(_dataSplit[2], out y); float width = 0; float.TryParse(_dataSplit[3], out width); float height = 0; float.TryParse(_dataSplit[4], out height); data.rect = new Rect(x, y, width, height); data.pivot = new Vector2(0.5f, 0.5f); tSheetList.uispriteList.Add(data); } } SpriteMetaData[] metaData = new SpriteMetaData[tSheetList.uispriteList.Count]; for (int i = 0; i < metaData.Length; i++) { metaData[i].pivot = tSheetList.uispriteList[i].pivot; metaData[i].border = tSheetList.uispriteList[i].border; metaData[i].rect = tSheetList.uispriteList[i].rect; metaData[i].name = tSheetList.uispriteList[i].spriteName; } TextureImporter textureImporter = TextureImporter.GetAtPath(BuildTool.RemovePathPrefix(outPngPath)) as TextureImporter; textureImporter.spritesheet = metaData; textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spriteImportMode = SpriteImportMode.Multiple; textureImporter.mipmapEnabled = false; textureImporter.SaveAndReimport(); tAtlas.sheet = tSheetList; Texture2D tx2d = AssetDatabase.LoadAssetAtPath <Texture2D>(BuildTool.RemovePathPrefix(outPngPath)); tAtlas.Texture = tx2d; PrefabUtility.ReplacePrefab(prefab, o); }