static private void MakeAtlas() { string spriteDir = Application.dataPath + "/Atlas/"; if (!Directory.Exists(spriteDir)) { Directory.CreateDirectory(spriteDir); } Texture2D atlas = new Texture2D(2048, 2048); List <Texture2D> list = new List <Texture2D>(); DirectoryInfo rootDirInfo = new DirectoryInfo(Application.dataPath + "/UI/Image/Common/"); //foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories()) { foreach (FileInfo pngFile in rootDirInfo.GetFiles("*.png", SearchOption.AllDirectories)) { string allPath = pngFile.FullName; string assetPath = allPath.Substring(allPath.IndexOf("Assets")); Texture2D texture = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Texture2D)) as Texture2D; //Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(assetPath); //Debug.LogError(assetPath); //GameObject go = new GameObject(sprite.name); //go.AddComponent<SpriteRenderer>().sprite = sprite; //allPath = spriteDir + "/" + sprite.name + ".prefab"; //string prefabPath = allPath.Substring(allPath.IndexOf("Assets")); //PrefabUtility.CreatePrefab(prefabPath, go); //GameObject.DestroyImmediate(go); //Texture2D sprite = Resources.Load( // string.Format("IMAGE/{0}/{1}", PanelCreator.Instance.CurrentName, stateDict[state]), // typeof(Sprite)) as Sprite; list.Add(texture); } } Rect[] rects = atlas.PackTextures(list.ToArray(), 5); atlas = AtlasOptimizer.OptimizeAtlas(atlas, rects); SaveTexture(atlas); string pngPath = Application.dataPath + "/Resources/b.png"; string assetPath1 = pngPath.Substring(pngPath.IndexOf("Assets")); CreateMultipleModeSpriteImporter(assetPath1, rects, atlas.width, atlas.height); AssetDatabase.ImportAsset(assetPath1, ImportAssetOptions.ForceUncompressedImport); }
public static void Generate(string jsonPath) { string jsonFileName = FileUtility.GetFileName(jsonPath); Read9SliceParam(jsonPath); Texture2D atlas = new Texture2D(2048, 2048); List <Texture2D> list = new List <Texture2D>(); string imageDir = FileUtility.RemovePostfix(jsonPath.Replace("Data", "Image")); DirectoryInfo dirInfo = new DirectoryInfo(imageDir); List <string> nameList = new List <string>(); foreach (FileInfo pngFile in dirInfo.GetFiles("*.png", SearchOption.TopDirectoryOnly)) { string pngAssetPath = FileUtility.AllPath2AssetPath(pngFile.FullName); CreateTextureImporter(pngAssetPath, true); AssetDatabase.ImportAsset(pngAssetPath, ImportAssetOptions.ForceUncompressedImport); //TODO //FormatTexture(FileTool.RemovePostfix(pngFile.Name), pngAssetPath); Texture2D texture = AssetDatabase.LoadAssetAtPath(pngAssetPath, typeof(Texture2D)) as Texture2D; nameList.Add(texture.name); list.Add(texture); } //padding REYES //如果图集太大,会有缩放 需要确认下是否会产生影响 Rect[] rects = atlas.PackTextures(list.ToArray(), 5); atlas = AtlasOptimizer.OptimizeAtlas(atlas, rects); string outputPath = Application.dataPath + "/Atlas/" + jsonFileName + ".png"; string assetPath = FileUtility.AllPath2AssetPath(outputPath); AssetDatabase.DeleteAsset(assetPath); SaveTexture(atlas, outputPath); AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUncompressedImport); CreateTextureImporter(assetPath, true); AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUncompressedImport); CreateMultipleModeSpriteImporter(assetPath, rects, atlas.width, atlas.height, nameList); AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUncompressedImport); BuildAssetBundle(assetPath, jsonFileName); }