xingwei.zhu
6 年前
当前提交
9e9a2bcf
共有 13 个文件被更改,包括 423 次插入 和 13 次删除
-
4Runtime/engine/UIWidgetsPanel.cs
-
3Runtime/rendering/binding.cs
-
33Runtime/rendering/layer.cs
-
17Runtime/ui/compositing.cs
-
27Samples/UIWidgetSample/MaterialSample.cs
-
95Runtime/flow/performance_overlay_layer.cs
-
11Runtime/flow/performance_overlay_layer.cs.meta
-
81Runtime/rendering/performance_overlay.cs
-
11Runtime/rendering/performance_overlay.cs.meta
-
98Runtime/service/performance_utils.cs
-
11Runtime/service/performance_utils.cs.meta
-
34Runtime/widgets/performance_overlay.cs
-
11Runtime/widgets/performance_overlay.cs.meta
|
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.material; |
|||
using Unity.UIWidgets.rendering; |
|||
using Unity.UIWidgets.service; |
|||
using Unity.UIWidgets.ui; |
|||
using UnityEngine; |
|||
using Canvas = Unity.UIWidgets.ui.Canvas; |
|||
using Rect = Unity.UIWidgets.ui.Rect; |
|||
|
|||
namespace Unity.UIWidgets.flow { |
|||
public class PerformanceOverlayLayer : Layer { |
|||
public PerformanceOverlayLayer(int options) { |
|||
this._options = options; |
|||
} |
|||
|
|||
readonly int _options; |
|||
|
|||
public override void paint(PaintContext context) { |
|||
D.assert(this.needsPainting); |
|||
const int padding = 8; |
|||
const int fpsHeight = 20; |
|||
|
|||
Canvas canvas = context.canvas; |
|||
canvas.save(); |
|||
|
|||
float x = this.paintBounds.left + padding; |
|||
float y = this.paintBounds.top + padding; |
|||
float width = this.paintBounds.width - padding * 2; |
|||
float height = this.paintBounds.height; |
|||
|
|||
this._drawFPS(canvas, x, y); |
|||
|
|||
if ((this._options & (int) PerformanceOverlayOption.drawFrameCost) == 1) { |
|||
this._drawFrameCost(canvas, x, y + fpsHeight, width, height - padding - fpsHeight); |
|||
} |
|||
|
|||
canvas.restore(); |
|||
} |
|||
|
|||
|
|||
void _drawFPS(Canvas canvas, float x, float y) { |
|||
var pb = new ParagraphBuilder(new ParagraphStyle { }); |
|||
pb.addText("FPS = " + PerformanceUtils.instance.getFPS()); |
|||
var paragraph = pb.build(); |
|||
paragraph.layout(new ParagraphConstraints(width: 300)); |
|||
|
|||
canvas.drawParagraph(paragraph, new Offset(x, y)); |
|||
} |
|||
|
|||
void _drawFrameCost(Canvas canvas, float x, float y, float width, float height) { |
|||
Rect visualizationRect = Rect.fromLTWH(x, y, width, height); |
|||
|
|||
Paint paint = new Paint {color = Colors.blue}; |
|||
Paint paint2 = new Paint {color = Colors.red}; |
|||
Paint paint3 = new Paint {color = Colors.green}; |
|||
Paint paint4 = new Paint {color = Colors.white70}; |
|||
|
|||
float[] costFrames = PerformanceUtils.instance.getFrames(); |
|||
int curFrame = PerformanceUtils.instance.getCurFrame(); |
|||
|
|||
float barWidth = Mathf.Max(1, width / costFrames.Length); |
|||
float perHeight = height / 32.0f; |
|||
|
|||
canvas.drawRect(visualizationRect, paint4); |
|||
canvas.drawRect(Rect.fromLTWH(x, y + perHeight * 16.0f, width, 1), paint3); |
|||
|
|||
float cur_x = x; |
|||
Path barPath = new Path(); |
|||
|
|||
for (var i = 0; i < costFrames.Length; i++) { |
|||
if (costFrames[i] != 0) { |
|||
float curHeight = Mathf.Min(perHeight * costFrames[i], height); |
|||
Rect barRect = Rect.fromLTWH(cur_x, y + height - curHeight, barWidth, curHeight); |
|||
barPath.addRect(barRect); |
|||
} |
|||
|
|||
cur_x += barWidth; |
|||
} |
|||
|
|||
canvas.drawPath(barPath, paint); |
|||
if (curFrame >= 0 && curFrame < costFrames.Length && costFrames[curFrame] != 0) { |
|||
float curHeight = Mathf.Min(perHeight * costFrames[curFrame], height); |
|||
Rect barRect = Rect.fromLTWH(x + barWidth * curFrame, y + height - curHeight, barWidth, curHeight); |
|||
canvas.drawRect(barRect, paint2); |
|||
|
|||
var pb = new ParagraphBuilder(new ParagraphStyle { }); |
|||
pb.addText("Frame Cost: " + costFrames[curFrame] + "ms"); |
|||
var paragraph = pb.build(); |
|||
paragraph.layout(new ParagraphConstraints(width: 300)); |
|||
|
|||
canvas.drawParagraph(paragraph, new Offset(x, y + height - 12)); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 8abf5cb3d8e90423fb0d2476c8dea244 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.ui; |
|||
|
|||
namespace Unity.UIWidgets.rendering { |
|||
public enum PerformanceOverlayOption { |
|||
drawFPS, //default
|
|||
drawFrameCost |
|||
} |
|||
|
|||
|
|||
public class RenderPerformanceOverlay : RenderBox { |
|||
public RenderPerformanceOverlay( |
|||
int optionsMask = 0 |
|||
) { |
|||
this._optionMask = optionsMask; |
|||
} |
|||
|
|||
public int optionsMask { |
|||
get { return this._optionMask; } |
|||
set { |
|||
if (value == this._optionMask) { |
|||
return; |
|||
} |
|||
|
|||
this._optionMask = value; |
|||
this.markNeedsPaint(); |
|||
} |
|||
} |
|||
|
|||
int _optionMask; |
|||
|
|||
protected override bool sizedByParent { |
|||
get { return true; } |
|||
} |
|||
|
|||
protected override bool alwaysNeedsCompositing { |
|||
get { return true; } |
|||
} |
|||
|
|||
protected override float computeMinIntrinsicWidth(float height) { |
|||
return 0.0f; |
|||
} |
|||
|
|||
protected override float computeMaxIntrinsicWidth(float height) { |
|||
return 0.0f; |
|||
} |
|||
|
|||
float _intrinsicHeight { |
|||
get { |
|||
const float kDefaultGraphHeight = 80.0f; |
|||
float result = 20f; |
|||
|
|||
if ((this.optionsMask | (1 << (int) PerformanceOverlayOption.drawFrameCost)) > 0) { |
|||
result += kDefaultGraphHeight; |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
|
|||
protected override float computeMinIntrinsicHeight(float width) { |
|||
return this._intrinsicHeight; |
|||
} |
|||
|
|||
protected override float computeMaxIntrinsicHeight(float width) { |
|||
return this._intrinsicHeight; |
|||
} |
|||
|
|||
protected override void performResize() { |
|||
this.size = this.constraints.constrain(new Size(float.PositiveInfinity, this._intrinsicHeight)); |
|||
} |
|||
|
|||
public override void paint(PaintingContext context, Offset offset) { |
|||
D.assert(this.needsCompositing); |
|||
context.addLayer(new PerformanceOverlayLayer( |
|||
overlayRect: Rect.fromLTWH(offset.dx, offset.dy, this.size.width, this.size.height), |
|||
optionsMask: this.optionsMask |
|||
)); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 2e3164a1dd18d4b4fb226e57522bb7ca |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Diagnostics; |
|||
using Unity.UIWidgets.foundation; |
|||
|
|||
namespace Unity.UIWidgets.service { |
|||
public class PerformanceUtils { |
|||
public static PerformanceUtils instance { |
|||
get { |
|||
if (_instance != null) { |
|||
return _instance; |
|||
} |
|||
|
|||
_instance = new PerformanceUtils(); |
|||
_instance._setup(); |
|||
|
|||
return _instance; |
|||
} |
|||
} |
|||
|
|||
static PerformanceUtils _instance; |
|||
|
|||
const int FrameBufferSize = 200; |
|||
|
|||
float[] _frames; |
|||
int _curFrameId; |
|||
Stopwatch _stopwatch; |
|||
|
|||
float deltaTime = 0.0f; |
|||
|
|||
bool _enabled; |
|||
|
|||
|
|||
void _setup() { |
|||
this._frames = new float[FrameBufferSize]; |
|||
this._curFrameId = -1; |
|||
this._enabled = false; |
|||
} |
|||
|
|||
void _ensureStopWatch() { |
|||
if (this._stopwatch == null) { |
|||
this._stopwatch = new Stopwatch(); |
|||
} |
|||
} |
|||
|
|||
public void updateDeltaTime(float unscaledDeltaTime) { |
|||
this.deltaTime += (unscaledDeltaTime - this.deltaTime) * 0.1f; |
|||
} |
|||
|
|||
public float getFPS() { |
|||
return 1.0f / this.deltaTime; |
|||
} |
|||
|
|||
public void startProfile() { |
|||
if (!this._enabled) { |
|||
return; |
|||
} |
|||
|
|||
this._ensureStopWatch(); |
|||
if (this._stopwatch.IsRunning) { |
|||
D.assert(false, "Try to start the stopwatch when it is already running"); |
|||
return; |
|||
} |
|||
|
|||
this._stopwatch.Start(); |
|||
} |
|||
|
|||
public void endProfile() { |
|||
if (!this._enabled || this._stopwatch == null) { |
|||
return; |
|||
} |
|||
|
|||
if (!this._stopwatch.IsRunning) { |
|||
D.assert(false, "Try to record the stopwatch when it is already stopped"); |
|||
} |
|||
|
|||
this._stopwatch.Stop(); |
|||
float frameCost = this._stopwatch.ElapsedMilliseconds; |
|||
this._stopwatch.Reset(); |
|||
if (frameCost == 0) { |
|||
return; |
|||
} |
|||
|
|||
this._curFrameId = (this._curFrameId + 1) % FrameBufferSize; |
|||
this._frames[this._curFrameId] = frameCost; |
|||
} |
|||
|
|||
public float[] getFrames() { |
|||
if (!this._enabled) { |
|||
this._enabled = true; |
|||
} |
|||
|
|||
return this._frames; |
|||
} |
|||
|
|||
public int getCurFrame() { |
|||
return this._curFrameId; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 819a2117a730b431bb069817e3ed8cd7 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.rendering; |
|||
|
|||
namespace Unity.UIWidgets.widgets { |
|||
public class PerformanceOverlay : LeafRenderObjectWidget { |
|||
public PerformanceOverlay( |
|||
Key key = null, |
|||
int optionsMask = 0 |
|||
) : base(key: key) { |
|||
this.optionsMask = optionsMask; |
|||
} |
|||
|
|||
public readonly int optionsMask; |
|||
|
|||
public static PerformanceOverlay allEnabled( |
|||
Key key = null |
|||
) { |
|||
return new PerformanceOverlay( |
|||
optionsMask: (1 << (int) PerformanceOverlayOption.drawFPS) | |
|||
(1 << (int) PerformanceOverlayOption.drawFrameCost) |
|||
); |
|||
} |
|||
|
|||
public override RenderObject createRenderObject(BuildContext context) { |
|||
return new RenderPerformanceOverlay( |
|||
optionsMask: this.optionsMask); |
|||
} |
|||
|
|||
public override void updateRenderObject(BuildContext context, RenderObject renderObject) { |
|||
RenderPerformanceOverlay _renderObject = (RenderPerformanceOverlay) renderObject; |
|||
_renderObject.optionsMask = this.optionsMask; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 6f53af60c4e394d96880f3b94fe34dd3 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue