工具:自动清除废弃的ParticleSystem

美术在制作特效的时候,可能会留下大量无效的ParticleSystem(我们的检查下来有几百个无效的),这样会给我们添加渲染压力。

这里提供一个简单的工具检查哪些是无效的ParticleSystem并且将其移除。判断的标准从Emit 和 Render是否勾选(根据自己情况定,一般来说是这两个选项)

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

public class CheckParticleSystem : MonoBehaviour
{
  
    const string DIR_ROOT = "/Test/";
    static List<ResultData> result = new List<ResultData>();

    [MenuItem("Optimize/检查无效ParticleSystem")]
    static void CheckNoAvaliablePS()
    {
        result.Clear();
        string[] files = EditorTool.GetFiles(DIR_ROOT, "*.prefab");
        foreach (var filepath in files)
        {
            GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(filepath);

            ParticleSystem[] psarray = go.GetComponentsInChildren<ParticleSystem>(true);
            foreach (var k in psarray)
            {
                bool emitEable = k.emission.enabled;
                ParticleSystemRenderer render = k.GetComponent<ParticleSystemRenderer>();
                bool rendEable = render.enabled;
                if (!emitEable || !rendEable)
                {
                    result.Add(new ResultData { path = filepath, emitEnable = emitEable, renderEable = rendEable, ps = k, render = render });
                }
            }
        }

        if (result.Count > 0)
        {
            Debug.LogError("Fail: 如下特效未通过检查, 一共:" + result.Count + " 个");
            foreach (var v in result)
            {
                Debug.LogError(v.ToString());
            }
        }
        else
        {
            Debug.Log("Success: 所有特效通过检查");
        }
    }

    [MenuItem("Optimize/检查 + 移除无效ParticleSystem")]
    static void RemoveNoAvaliablePS()
    {
        CheckNoAvaliablePS();
        foreach (var v in result)
        {
            v.render.material = null;
            v.render.trailMaterial = null;
            DestroyImmediate(v.ps, true);
        }
        Debug.LogError("Success: 移除成功, 一共:" + result.Count + " 个");
    }

    class ResultData
    {
        public string path;
        public bool emitEnable;
        public bool renderEable;
        public ParticleSystem ps;
        public ParticleSystemRenderer render;

        public override string ToString()
        {
            string msgemit = emitEnable ? "已勾选" : "未勾选";
            string msgrend = renderEable ? "已勾选" : "未勾选";
            return string.Format("路径:{0}; Emit:{1}; Render:{2}", path, msgemit, msgrend);
        }
    }
}



补一个函数片段

 public static string[] GetFiles(string rootpath, string parttern)
    {
        string[] files = Directory.GetFiles(Application.dataPath + rootpath, parttern, SearchOption.AllDirectories);
        string[] result = new string[files.Length];
        for (int i = 0, imax = files.Length; i < imax; i++)
        {
            string filepath = "Assets" + files[i].Substring(Application.dataPath.Length);
            filepath = filepath.Replace("\\", "/");
            result[i] = filepath;
        }
        return result;
    }

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值