本文共 4011 字,大约阅读时间需要 13 分钟。
大佬的出品,这个界面见过很多次
自己实践了一遍,很多数据不知道原理,初步代码如下:
IEnumerator AnalyseSingleFx(GameObject fxObj,float fxLoadTime, string fxPath) { var report =new EffectPerformance(); reports.Add(report); report.Path = fxPath; System.GC.Collect(); AsyncOperation ao = Resources.UnloadUnusedAssets(); yield return ao; yield return new WaitForEndOfFrame(); //加载 float t1 = 0;// float t1 = Time.realtimeSinceStartup;// //GameObject fxObj = null;// //var fxRes = App loadobject// report.LoadTime = Time.realtimeSinceStartup - t1;// yield return new WaitForEndOfFrame(); report.LoadTime = fxLoadTime; if(fxObj==null) yield break; report.totalPsCount = fxObj.GetComponentsInChildren(true).Length; Renderer[] fxRenderers = fxObj.GetComponentsInChildren (); Dictionary fxMaterials = new Dictionary (); int active = 0; foreach (var r in fxRenderers) { bool has = false; if(r.sharedMaterial!=null && fxMaterials.TryGetValue(r.sharedMaterial,out has)==false){ fxMaterials.Add(r.sharedMaterial,true); } if (r.enabled) active++; } report.activeRendercount = active; report.materialCount = fxMaterials.Count; yield return new WaitForEndOfFrame(); GameObject someFxInst = null; t1 = Time.realtimeSinceStartup; //实例化 for (int i = 0; i < testCount; i++) { GameObject go = Instantiate(fxObj); go.transform.position = Vector3.zero; fxCopies.Add(go); if (i == 0) someFxInst = go; } report.InstTime = (Time.realtimeSinceStartup - t1) * 1000f / testCount; ParticleSystem[] psInsts = someFxInst.GetComponentsInChildren (); //渲染,只采样一个?? yield return new WaitForEndOfFrame(); yield return new WaitForEndOfFrame(); yield return new WaitForEndOfFrame(); float t4 = 0; int frame = 0; report.MinRenderTime = float.MaxValue; report.MaxRenderTime = float.MinValue; List timeList = new List (); while (t4 report.MaxRenderTime) { report.MaxRenderTime = dt; report.maxRenderOccurTime = t4; } timeList.Add(dt*1000); t4 += dt; int particleCount = 0; for (int i = 0; i < psInsts.Length; i++ ) { particleCount += psInsts[i].particleCount; } if (report.maxParticleCount < particleCount) report.maxParticleCount = particleCount; yield return new WaitForEndOfFrame(); } report.MinRenderTime *= 1000 / testCount; report.maxRenderOccurTime *= 1000 / testCount; timeList.Sort(); timeList.Reverse(); float avg = 0; int topN = Mathf.Min(topNFrame, timeList.Count); for(int i=0;i texNames = GetTextureMemAndCount(); foreach (var t in texNames) { if (localTexNames.ContainsKey(t.Key) == false) { report.TexNameList.Add(t.Key,t.Value); report.texMemBytes +=t.Value.bytes; report.texMemCount++; } } //清理// foreach (var fx in fxCopies)// {// UnityEngine.Object.DestroyImmediate(fx);// }// fxCopies.Clear(); reportDoneCount++; Debug.Log("完成测算过+1"); yield return new WaitForEndOfFrame(); }
public class EffectPerformance{ public string Path; ////// 加载时间 /// public float LoadTime; ////// 平均实例化时间 /// public float InstTime; public float MinRenderTime; public float MaxRenderTime; //最长渲染帧耗时 public float topNFramesAvgRenderTime; public float maxRenderOccurTime; //渲染的最长时间的一帧发生在第几秒 ////// 激活的渲染个数 /// public int activeRendercount; //激活的渲染个数 public int totalPsCount; //全部粒子个数,包括未激活 public int materialCount; //材质个数 public int maxParticleCount; //最大粒子个数 public int texMemBytes; //所用贴图内存 public int texMemCount; //所用贴图个数 public DictionaryTexNameList = new Dictionary ();}
转载地址:http://eoiiz.baihongyu.com/