in VisualProfiler.cs [243:372]
private void LateUpdate()
{
if (window == null)
{
return;
}
// Update window transformation.
Transform cameraTransform = Camera.main ? Camera.main.transform : null;
if (window.activeSelf && cameraTransform != null)
{
float t = Time.deltaTime * windowFollowSpeed;
window.transform.position = Vector3.Lerp(window.transform.position, CalculateWindowPosition(cameraTransform), t);
window.transform.rotation = Quaternion.Slerp(window.transform.rotation, CalculateWindowRotation(cameraTransform), t);
window.transform.localScale = defaultWindowScale * windowScale;
}
// Capture frame timings every frame and read from it depending on the frameSampleRate.
FrameTimingManager.CaptureFrameTimings();
++frameCount;
float elapsedSeconds = stopwatch.ElapsedMilliseconds * 0.001f;
if (elapsedSeconds >= frameSampleRate)
{
int cpuFrameRate = (int)(1.0f / (elapsedSeconds / frameCount));
int gpuFrameRate = 0;
// Many platforms do not yet support the FrameTimingManager. When timing data is returned from the FrameTimingManager we will use
// its timing data, else we will depend on the stopwatch.
uint frameTimingsCount = FrameTimingManager.GetLatestTimings((uint)Mathf.Min(frameCount, maxFrameTimings), frameTimings);
if (frameTimingsCount != 0)
{
float cpuFrameTime, gpuFrameTime;
AverageFrameTiming(frameTimings, frameTimingsCount, out cpuFrameTime, out gpuFrameTime);
cpuFrameRate = (int)(1.0f / (cpuFrameTime / frameCount));
gpuFrameRate = (int)(1.0f / (gpuFrameTime / frameCount));
}
// Update frame rate text.
cpuFrameRateText.text = cpuFrameRateStrings[Mathf.Clamp(cpuFrameRate, 0, maxTargetFrameRate)];
if (gpuFrameRate != 0)
{
gpuFrameRateText.gameObject.SetActive(true);
gpuFrameRateText.text = gpuFrameRateStrings[Mathf.Clamp(gpuFrameRate, 0, maxTargetFrameRate)];
}
// Update frame colors.
for (int i = frameRange - 1; i > 0; --i)
{
frameInfoColors[i] = frameInfoColors[i - 1];
}
// Ideally we would query a device specific API (like the HolographicFramePresentationReport) to detect missed frames.
// But, many of these APIs are inaccessible in Unity. Currently missed frames are assumed when the average cpuFrameRate
// is under the target frame rate.
frameInfoColors[0] = (cpuFrameRate < ((int)(AppFrameRate) - 1)) ? missedFrameRateColor : targetFrameRateColor;
frameInfoPropertyBlock.SetVectorArray(colorID, frameInfoColors);
// Reset timers.
frameCount = 0;
stopwatch.Reset();
stopwatch.Start();
}
// Draw frame info.
if (window.activeSelf)
{
Matrix4x4 parentLocalToWorldMatrix = window.transform.localToWorldMatrix;
if (defaultInstancedMaterial != null)
{
frameInfoPropertyBlock.SetMatrix(parentMatrixID, parentLocalToWorldMatrix);
Graphics.DrawMeshInstanced(quadMesh, 0, defaultInstancedMaterial, frameInfoMatrices, frameInfoMatrices.Length, frameInfoPropertyBlock, UnityEngine.Rendering.ShadowCastingMode.Off, false);
}
else
{
// If a instanced material is not available, fall back to non-instanced rendering.
for (int i = 0; i < frameInfoMatrices.Length; ++i)
{
frameInfoPropertyBlock.SetColor(colorID, frameInfoColors[i]);
Graphics.DrawMesh(quadMesh, parentLocalToWorldMatrix * frameInfoMatrices[i], defaultMaterial, 0, null, 0, frameInfoPropertyBlock, false, false, false);
}
}
}
// Update memory statistics.
ulong limit = AppMemoryUsageLimit;
if (limit != limitMemoryUsage)
{
if (window.activeSelf && WillDisplayedMemoryUsageDiffer(limitMemoryUsage, limit, displayedDecimalDigits))
{
MemoryUsageToString(stringBuffer, displayedDecimalDigits, limitMemoryText, limitMemoryString, limit);
}
limitMemoryUsage = limit;
}
ulong usage = AppMemoryUsage;
if (usage != memoryUsage)
{
usedAnchor.localScale = new Vector3((float)usage / limitMemoryUsage, usedAnchor.localScale.y, usedAnchor.localScale.z);
if (window.activeSelf && WillDisplayedMemoryUsageDiffer(memoryUsage, usage, displayedDecimalDigits))
{
MemoryUsageToString(stringBuffer, displayedDecimalDigits, usedMemoryText, usedMemoryString, usage);
}
memoryUsage = usage;
}
if (memoryUsage > peakMemoryUsage)
{
peakAnchor.localScale = new Vector3((float)memoryUsage / limitMemoryUsage, peakAnchor.localScale.y, peakAnchor.localScale.z);
if (window.activeSelf && WillDisplayedMemoryUsageDiffer(peakMemoryUsage, memoryUsage, displayedDecimalDigits))
{
MemoryUsageToString(stringBuffer, displayedDecimalDigits, peakMemoryText, peakMemoryString, memoryUsage);
}
peakMemoryUsage = memoryUsage;
}
window.SetActive(isVisible);
}