HonorLee 1 年之前
父节点
当前提交
8fa1591e63
共有 3 个文件被更改,包括 58 次插入27 次删除
  1. 11 5
      CGTool/Anime.cs
  2. 42 21
      CGTool/AnimePlayer.cs
  3. 5 1
      README.md

+ 11 - 5
CGTool/Anime.cs

@@ -49,6 +49,8 @@ namespace CGTool
         public int AudioIndex;
         //动效编号
         public Anime.EffectType Effect;
+        //GraphicInfo;
+        public GraphicInfoData GraphicInfo;
         //动画Sprite
         public Sprite AnimeSprite;
     }
@@ -215,11 +217,14 @@ namespace CGTool
             return null;
         }
 
-        //预处理动画图形合批,这个返回的是一个所有帧合并后的Texture2D
-        private static void prepareAnimeFrames(AnimeDetail animeDetail)
+        //预处理动画图形合批烘焙
+        public static void BakeAnimeFrames(AnimeDetail animeDetail)
         {
-            //动态合并Texture2D
+            if(animeDetail.AnimeTexture != null) return;
+            //所有帧的图形数据
             GraphicData[] graphicDatas = new GraphicData[animeDetail.FrameCount];
+            
+            //合并后的Texture2D尺寸
             uint textureWidth = 0;
             uint textureHeight = 0;
             
@@ -238,10 +243,10 @@ namespace CGTool
                 animeDetail.AnimeFrameInfos[i].Height = (int) graphicData.Height;
                 animeDetail.AnimeFrameInfos[i].OffsetX = (int) graphicInfoData.OffsetX;
                 animeDetail.AnimeFrameInfos[i].OffsetY = (int) graphicInfoData.OffsetY;
+                animeDetail.AnimeFrameInfos[i].GraphicInfo = graphicInfoData;
             }
             //合并图档
-            Texture2D texture2dMix = new Texture2D((int) textureWidth, (int) textureHeight, TextureFormat.RGBA32, false,
-                true);
+            Texture2D texture2dMix = new Texture2D((int) textureWidth, (int) textureHeight, TextureFormat.RGBA4444, false,false);
             Color32 transparentColor = new Color32(0, 0, 0, 0);
             Color32[] transparentColors = new Color32[texture2dMix.width * texture2dMix.height];
             for (var i = 0; i < transparentColors.Length; i++)
@@ -249,6 +254,7 @@ namespace CGTool
                 transparentColors[i] = transparentColor;
             }
             texture2dMix.SetPixels32(transparentColors,0);
+            
             int offsetX = 0;
             for (var i = 0; i < animeDetail.FrameCount; i++)
             {

+ 42 - 21
CGTool/AnimePlayer.cs

@@ -81,6 +81,7 @@ namespace CGTool
         
         //绑定渲染对象
         [SerializeField,Header("Image渲染")] public bool isRenderByImage = false;
+        [SerializeField,Header("序列帧合批")] public bool isFrameBatch = false;
         private SpriteRenderer _spriteRenderer;
         private Image _imageRenderer;
         private int _paletIndex = 0;
@@ -312,33 +313,53 @@ namespace CGTool
             
             AnimeFrame[] frames = new AnimeFrame[animeOption.AnimeDetail.FrameCount];
 
-            //获取动画帧数据
-            for (int i = 0; i < animeOption.AnimeDetail.AnimeFrameInfos.Length; i++)
+            if (isFrameBatch)
             {
-                AnimeFrameInfo animeFrameInfo = animeOption.AnimeDetail.AnimeFrameInfos[i];
-                GraphicInfoData graphicInfoData = GraphicInfo.GetGraphicInfoDataByIndex(animeOption.AnimeDetail.Version, animeOption.AnimeDetail.AnimeFrameInfos[i].GraphicIndex);
-                if (graphicInfoData == null)
+                Debug.Log("AnimePlayer:Batch");
+                Anime.BakeAnimeFrames(animeOption.AnimeDetail);
+                //获取动画帧数据
+                for (int i = 0; i < animeOption.AnimeDetail.AnimeFrameInfos.Length; i++)
                 {
-                    Debug.Log("GraphicInfo Version:" + animeOption.AnimeDetail.Version + " Index:" +
-                              animeOption.AnimeDetail.AnimeFrameInfos[i] + " is null");
-                    continue;
+                    if(animeOption.AnimeDetail.AnimeFrameInfos[i].AnimeSprite == null) continue;
+                    //创建帧数据
+                    frames[i] = new AnimeFrame();
+                    frames[i].Index = i;
+                    frames[i].GraphicInfo = animeOption.AnimeDetail.AnimeFrameInfos[i].GraphicInfo;
+                    frames[i].Sprite = animeOption.AnimeDetail.AnimeFrameInfos[i].AnimeSprite;
+                    frames[i].AnimeFrameInfo = animeOption.AnimeDetail.AnimeFrameInfos[i];
                 }
-
-                GraphicData graphicData = Graphic.GetGraphicData(graphicInfoData, _paletIndex);
-                if (graphicData == null)
+            }
+            else
+            {
+                //获取动画帧数据
+                for (int i = 0; i < animeOption.AnimeDetail.AnimeFrameInfos.Length; i++)
                 {
-                    Debug.Log("GraphicData Version:" + animeOption.AnimeDetail.Version + " Index:" +
-                              animeOption.AnimeDetail.AnimeFrameInfos[i] + " is null");
-                    continue;
-                }
+                    AnimeFrameInfo animeFrameInfo = animeOption.AnimeDetail.AnimeFrameInfos[i];
+                    GraphicInfoData graphicInfoData = GraphicInfo.GetGraphicInfoDataByIndex(animeOption.AnimeDetail.Version, animeOption.AnimeDetail.AnimeFrameInfos[i].GraphicIndex);
+                    if (graphicInfoData == null)
+                    {
+                        Debug.Log("GraphicInfo Version:" + animeOption.AnimeDetail.Version + " Index:" +
+                                  animeOption.AnimeDetail.AnimeFrameInfos[i] + " is null");
+                        continue;
+                    }
+
+                    GraphicData graphicData = Graphic.GetGraphicData(graphicInfoData, _paletIndex);
+                    if (graphicData == null)
+                    {
+                        Debug.Log("GraphicData Version:" + animeOption.AnimeDetail.Version + " Index:" +
+                                  animeOption.AnimeDetail.AnimeFrameInfos[i] + " is null");
+                        continue;
+                    }
                 
-                //创建帧数据
-                frames[i] = new AnimeFrame();
-                frames[i].Index = i;
-                frames[i].GraphicInfo = graphicInfoData;
-                frames[i].Sprite = graphicData.Sprite;
-                frames[i].AnimeFrameInfo = animeFrameInfo;
+                    //创建帧数据
+                    frames[i] = new AnimeFrame();
+                    frames[i].Index = i;
+                    frames[i].GraphicInfo = graphicInfoData;
+                    frames[i].Sprite = graphicData.Sprite;
+                    frames[i].AnimeFrameInfo = animeFrameInfo;
+                }
             }
+            
 
             _currentAnime = animeOption;
             _frames = frames;

+ 5 - 1
README.md

@@ -109,7 +109,9 @@ Dictionary<int, GraphicData> MapGroundSerialDic =
 /**
 * 动画播放器,用于播放CG动画,支持多动画队列播放
 * 1.3更新后已无需手动挂载SpriteRenderer、Image组件,程序会自动处理
-* 只需将AnimePlayer挂载到任意GameObject上即可,可手动指定渲染方式是否以Image组件渲染
+* 只需将AnimePlayer挂载到任意GameObject上即可
+* 可手动指定渲染方式是否以Image组件渲染
+* 可选择是否对序列帧图像进行合批(建议开启)
 *
 * 动画解析Anime类中包含以下多个枚举预设:
 * DirectionType 方向类型,含8个不同方向
@@ -219,6 +221,8 @@ player.Stop();
 
 
 ## 4、更新日志
+### v 1.4
+> `ADD` 加入Anime序列帧合批,挂载AnimePlayer后可手动设置是否开启合批(建议开启)
 ### v 1.3
 > `UPD` 优化AnimePlayer组件的挂载和使用方式
 >