unity中动画删减精度,优化动画文件

本文介绍了一个Unity编辑器脚本,用于批量优化项目的动画文件。通过减少浮点数精度来减小文件大小,同时尽量保持视觉效果不变。该脚本能够遍历选中的资源,读取并修改AnimationClip类型的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

using System;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
using UnityEditor;
using System.IO;

public class AnimationCut_new 
{
    static int num = 3;   //压缩精度
    static List<string> list_anims = new List<string>();  //存储anim文件的路径
    
    [MenuItem("Animation/cutFloat")]
    public static void Optimize()
    {
        FindAnims();
        CompressAnim();
    }

    private static void FindAnims()
    {
        UnityEngine.Object[] objs = Selection.GetFiltered(typeof(object), SelectionMode.DeepAssets);
        if (objs.Length > 0)
        {
            for (int i = 0; i < objs.Length; i++)
            {
                if (objs[i].GetType() != typeof(AnimationClip))
                    continue;
                string path = AssetDatabase.GetAssetPath(objs[i]);
                list_anims.Add(path);
            }
        }

    }
    public static void CompressAnim()
    {
       
        if (list_anims.Count > 0)
        {
            for (int i = 0; i < list_anims.Count; i++)
            {
                string path = list_anims[i];
              
                EditorUtility.DisplayProgressBar("CompressAnim", path + " Compressing...", ((float)i / list_anims.Count));
                string[] strs = File.ReadAllLines(path);
                if (strs == null)
                {
                    continue;
                }
                File.Delete(path);
                FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
               StreamWriter sw = new StreamWriter(fs);
               sw.Flush();
               sw.BaseStream.Seek(0, SeekOrigin.Begin);
                for(int j =0; j<strs.Length; j++)
                {
                   if(strs[j].Contains("time"))
                   {
                       string[] txts = strs[j].Split(':');
                       if(txts != null)
                       {
                          if(txts[1].Contains(".") && (txts[1].Length- txts[1].IndexOf('.')-1) >= num)
                          {
                              txts[1] = float.Parse(txts[1]).ToString("f"+num);
                              if(float.Parse(txts[1]) == 0)
                              {
                                  txts[1] = "0";
                              }
                               
                          }
                        
                           strs[j] = txts[0] + ": "+ txts[1];
                       }                    
                   }
                   if (strs[j].Contains("value") || strs[j].Contains("inSlope") || strs[j].Contains("outSlope"))
                   {
                       strs[j].Trim();
                       int frontindex = strs[j].IndexOf('{');
                       int behindindex = strs[j].IndexOf('}');
                       string beginstr = strs[j].Substring(0, frontindex);
                     
                       string str = strs[j].Substring(frontindex + 1, behindindex - frontindex - 1);
                       if (str != null)
                       {
                           string[] txts = str.Split(',');
                           if (txts != null)
                           {
                               string tt_new = null;
                               for (int k = 0; k < txts.Length; k++)
                               {
                                   string[] newstr = txts[k].Split(':');
                                   if(  newstr[1].Contains(".") && (newstr[1].Length - newstr[1].IndexOf('.')-1) >= num )
                                   {
                                       newstr[1] = float.Parse(newstr[1]).ToString("f" + num);
                                       if (float.Parse(newstr[1]) == 0)
                                       {
                                           newstr[1] = "0";
                                       }
                                   }
                                    tt_new += newstr[0] + ": " + newstr[1] + ( k == txts.Length-1 ? "" : ",");
                                                                      
                               }
                               strs[j] = beginstr + "{" + tt_new + "}";
                           }

                       }
                      
               
                   }
                   sw.WriteLine(strs[j]);
                }
                sw.Flush();
                sw.Close();                
            }
            EditorUtility.ClearProgressBar();
            Resources.UnloadUnusedAssets();
            AssetDatabase.SaveAssets();    
            list_anims.Clear();
            GC.Collect();

        }
    }

  
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值