Unity之项目优化-精度优化


前言

随着工程项目的推进,项目资源性能优化提上日程,其中有一项是动画资源精度过高的问题,导致占用内存过大。我们需要将浮点数的精调小至2位小数,或者更少或更多。

一、优化前

在这里插入图片描述
打开anim的文本如下
在这里插入图片描述

接下来我们就要对此进行优化,将所有小数精确保留到两位小数。

二、优化

1.思路

怎么优化呢?我们要先获取anim文件,将anim文件转为文件流,然后对此进行操作,检索里面的内容,如果内容中含有小数的情况,就把该小数精确到2位,然后覆盖到原文件并保存。

2.代码

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

public class AniFloatConstraint : EditorWindow//继承于此,弹窗必要的属性
{

    void OnGUI()//在该窗口下作画
    {


        if (Selection.objects.Length <= 0)
        {
            GUILayout.Label("请先选择一个文件夹!!! ");//做一条标签
        }
        else
        {
            GUILayout.Label("当前选中的文件夹: " + AssetDatabase.GetAssetPath(Selection.objects[0]));//未经过筛选的对象组
        }

        if (GUILayout.Button("开始设置"))//做一个按钮
        {
            if (!CheckSelection()) return;

            //AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();//导入更改过的资源

            Debug.Log("完成");
        }
    }

    [MenuItem("ResTool/动画精度工具")]
    static void SetTextureFormat()
    {
        Rect _rect = new Rect(0, 0, 500, 500);
        var temp= GetWindowWithRect<AniFloatConstraint>(_rect,false, "设置", true);
    
    }

    //<格式, List<路径>>
    Dictionary<string, List<string>> allSettings;

    /// <summary>
    /// 没有选中内容返回false
    /// </summary>
    /// <returns></returns>
    public bool CheckSelection()
    {
        if (Selection.objects.Length <= 0)
        {
            Debug.LogError("请先选择一个文件!!! ");
            return false;
        }

        string selectPath = AssetDatabase.GetAssetPath(Selection.objects[0]);
        Debug.Log("选择了一个文件/夹!!!" + ":" + selectPath);
        SetAnimationClip(selectPath);
        return true;
    }


    public void SetAnimationClip(string path)
    {
        List<AnimationClip> animationClipList = new List<AnimationClip>();
        List<string> animationPathList = new List<string>();
        string[] dirs = Directory.GetFiles(path, "*", SearchOption.AllDirectories);//返回目录中的文件名(含路径),获取文件夹下的所有文件,深度查找
        if (dirs == null || dirs.Length <= 0)
        {
            Debug.LogError("当前地址没有获取到任何的动画文件");
            return;
        }

        for (int i = 0; i < dirs.Length; i++)
        {
            AnimationClip objs = AssetDatabase.LoadAssetAtPath<AnimationClip>(dirs[i]);//通过路径加载资源
            if (objs != null)
            {
                animationClipList.Add(objs);
                animationPathList.Add(AssetDatabase.GetAssetPath(objs));
            }
        }
       
        CompressAnim(animationPathList);

    }

   
    /// <summary>
    /// 修改动画精度
    /// </summary>
    /// <param name="animationClipList">动画列表路径</param>
    public void CompressAnim(List<string> list_anims)
    {
        int num = 2;
        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.WriteAllText(path, "");//覆盖path下的文件置为空文件
                //File.Delete(path);
                FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);//把path下的空文件获取为文件流形式,以便对它进行操作
                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].Contains("inWeight") || strs[j].Contains("outWeight"))
                    {
                        strs[j].Trim();//删除前后的空白字符

                        int frontindex = strs[j].IndexOf('{');
                        int behindindex = strs[j].IndexOf('}');

                        string beginstr = null;
                        string str = null;
                        if (frontindex < 0 || behindindex < 0)
                        {
                            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];
                                sw.WriteLine(strs[j]);
                                continue;
                            }

                        }
                        else
                        {
                            beginstr = strs[j].Substring(0, frontindex);
                            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]);
                }
                Debug.Log("修改动画精度:" + path);
                sw.Flush();//清除缓存
                sw.Close();//关闭文件流写入器
            }
            EditorUtility.ClearProgressBar();//都执行完了关闭进度条
            Resources.UnloadUnusedAssets();//卸载未使用的资源
            AssetDatabase.SaveAssets();//将所有未保存的资源写入磁盘
            list_anims.Clear();//清除文件路径
            GC.Collect();//GC回收
        }
    }

}

3.操作

在这里插入图片描述
在这里插入图片描述

三、优化后

在这里插入图片描述

在这里插入图片描述

总结

该方法使用了EditorWindow,方便操作,只需要点开窗口,选择要优化精度的anim文件夹,就能一键优化。其实这里除了优化anim文件外,想要优化其他文件浮点数精度,思路也是相同的。只需要在LoadAssetAtPath是放宽的类型,或者不做限制,在后面的优化方案中不采用Contains(“time”)等特key值检测的操作,而是通篇检索数字,凡是数字都对其进行精度优化,也能达到效果。

参考: unity中动画删减精度,优化动画文件.


Unity项目工程:
链接:https://pan.baidu.com/s/1kMNqlbWO-yyTGcse70hUbg
提取码:ssdj

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值