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();
}
}
}
————————————————
版權聲明:本文爲CSDN博主「weixin_42524778」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_42524778/java/article/details/88907909