美术在制作特效的时候,可能会留下大量无效的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;
}