浏览代码

Merge pull request #176 from Unity-Technologies/siyaoH/1.17/gc

incremental gc
/main
GitHub 3 年前
当前提交
25db39f9
共有 7 个文件被更改,包括 87 次插入15 次删除
  1. 8
      README-ZH.md
  2. 8
      README.md
  3. 56
      com.unity.uiwidgets/Runtime/engine/UIWidgetsPanel.cs
  4. 4
      com.unity.uiwidgets/Runtime/foundation/debug.cs
  5. 11
      com.unity.uiwidgets/Runtime/scheduler/binding.cs
  6. 12
      com.unity.uiwidgets/Runtime/engine/UIWidgetsGlobalConfiguration.cs
  7. 3
      com.unity.uiwidgets/Runtime/engine/UIWidgetsGlobalConfiguration.cs.meta

8
README-ZH.md


#### 七、图片导入设置
请将图片放入StreamingAssets下,而后用```Image.file```读取
#### 八、自动降低帧率
目前在默认情况下,为了保证流畅度,项目在各个平台上均会以最高的刷新频率运行。不过您可以通过在代码中设置```SchedulerBinding.MEnableAutoAdjustFramerate = true```的方式来开启自动降帧的功能:该功能开启后,在UI内容不变的情况下我们将降低项目的刷新率来降低耗电。
#### 八、移动设备优化
目前在默认情况下,为了保证流畅度,项目在各个平台上均会以最高的刷新频率运行。不过您可以通过在代码中设置```UIWidgetsGlobalConfiguration.EnableAutoAdjustFramerate = true```的方式来开启自动降帧的功能:该功能开启后,在UI内容不变的情况下我们将降低项目的刷新率来降低耗电。
在移动设备上UI绘制的流畅度受到GC影响较大。项目默认会开启OnDemandGC来接管并优化整体GC效果。如果您不需要GC被UIWidgets控制,请在代码中设置```UIWidgetsGlobalConfiguration.EnableIncrementalGC = false```。
## 调试UIWidgets应用程序

如果想在runtime选择是否debug,请修改文件```com.unity.uiwidgets/com.unity.uiwidgets/Runtime/foundation/debug.cs```中的```static bool debugEnableAtRuntimeInternal```
如果想在runtime开启debug模式,请在项目代码中设置```UIWidgetsGlobalConfiguration.EnableDebugAtRuntime = true```。在默认情况下debug模式为关闭状态。
## 使用Window Scope保护外部调用
如果您在调试时遇到 `AssertionError: Window.instance is null` 或者在调用 `Window.instance` 时得到空指针, 那么您需要

8
README.md


#### Image Import Setting
Please put images under StreamingAssets folder, a and loading it using ```Image.file```.
#### Automatic Framerate Adjustment
You can enable this feature by setting ```SchedulerBinding.MEnableAutoAdjustFramerate = true``` in your project, which will automatically drop the frame rate of your App to 0 if the UI contents of UIWidgetsPanel is not changed for some time. This will help to prevent battery drain on mobile devices significantly.
#### Performance Optimization on Mobile devices
By setting ```UIWidgetsGlobalConfiguration.EnableAutoAdjustFramerate = true``` in your project, UIWidgets will drop the frame rate of your App to 0 if the UI contents of UIWidgetsPanel is not changed for some time. This will help to prevent battery drain on mobile devices significantly. Note that this feature is disabled by default.
Note that this feature is disabled by default though.
Long time garbage collection may cause App to stuck frequently. In UIWidgets we enable incremental garbage collection to avoid it. However, you can disable this feature by setting ```UIWidgetsGlobalConfiguration.EnableIncrementalGC = false```.
If you want to change different mode in runtime, please modify the file “com.unity.uiwidgets/com.unity.uiwidgets/Runtime/foundation/debug.cs” by making “static bool debugEnableAtRuntimeInternal” to true or false. Note that the value is set to false by default.
You can also enable debug mode in runtime by setting ```UIWidgetsGlobalConfiguration.EnableDebugAtRuntime = true``` in your project. Note that this value is set to false by default.
## Using Window Scope
If you see the error `AssertionError: Window.instance is null` or null pointer error of `Window.instance`,

56
com.unity.uiwidgets/Runtime/engine/UIWidgetsPanel.cs


using System.Collections.Generic;
using Unity.UIWidgets.external.simplejson;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.rendering;
using UnityEngine.Profiling;
using UnityEngine.Scripting;
using UnityEngine.UI;
using RawImage = UnityEngine.UI.RawImage;

Window.instance.onMetricsChanged?.Invoke();
}
}
protected virtual void Update() {
if (!_viewMetricsCallbackRegistered) {
_viewMetricsCallbackRegistered = true;

#if !UNITY_EDITOR
CollectGarbageOnDemand();
#endif
#region OnDemandGC
#if !UNITY_EDITOR
// 8 MB
const long kCollectAfterAllocating = 8 * 1024 * 1024;
long lastFrameMemory = 0;
long nextCollectAt = 0;
void TryEnableOnDemandGC()
{
if (UIWidgetsGlobalConfiguration.EnableIncrementalGC)
{
GarbageCollector.GCMode = GarbageCollector.Mode.Disabled;
Application.lowMemory += GC.Collect;
}
}
void CollectGarbageOnDemand()
{
if (!UIWidgetsGlobalConfiguration.EnableIncrementalGC)
{
return;
}
var mem = Profiler.GetMonoUsedSizeLong();
if (mem < lastFrameMemory)
{
// GC happened.
nextCollectAt = mem + kCollectAfterAllocating;
}
else if (mem >= nextCollectAt)
{
// Trigger incremental GC
GarbageCollector.CollectIncremental(1000);
lastFrameMemory = mem + kCollectAfterAllocating;
}
lastFrameMemory = mem;
}
#endif
#endregion
#if !UNITY_EDITOR && UNITY_ANDROID
bool AndroidInitialized = true;

//the hook API cannot be automatically called on IOS, so we need try hook it here
Hooks.tryHook();
#endif
#if !UNITY_EDITOR
TryEnableOnDemandGC();
#endif
base.OnEnable();
D.assert(_wrapper == null);

4
com.unity.uiwidgets/Runtime/foundation/debug.cs


public static bool debugCheckIntrinsicSizes = false;
public static bool debugPrintMouseHoverEvents = false;
static bool debugEnableAtRuntimeInternal = false;
public static HSVColor debugCurrentRepaintColor =
HSVColor.fromAHSV(0.4f, 60.0f, 1.0f, 1.0f);

}
}
#else
public static bool enableDebug => debugEnableAtRuntimeInternal;
public static bool enableDebug => UIWidgetsGlobalConfiguration.EnableDebugAtRuntime;
#endif
public static void _debugDrawDoubleRect(Canvas canvas, Rect outerRect, Rect innerRect, Color color) {

11
com.unity.uiwidgets/Runtime/scheduler/binding.cs


static readonly TimeSpan _coolDownDelay = new TimeSpan(0, 0, 0, 0, 200);
static Timer frameCoolDownTimer = null;
//disable auto adjust framerate by default
public static bool MEnableAutoAdjustFramerate = false;
public void scheduleFrame() {
if (hasScheduledFrame || !framesEnabled) {
return;

Window.instance.scheduleFrame();
hasScheduledFrame = true;
#if !UNITY_EDITOR
#endif
#if !UNITY_EDITOR
if (!MEnableAutoAdjustFramerate) {
if (!UIWidgetsGlobalConfiguration.EnableAutoAdjustFramerate) {
return;
}

void onFrameRateCoolDown() {
OnDemandRendering.renderFrameInterval = defaultMaxRenderFrameInterval;
}
#endif
public void scheduleForcedFrame() {
if (!framesEnabled) {

Window.instance.scheduleFrame();
hasScheduledFrame = true;
#if !UNITY_EDITOR
#endif
}
public void scheduleWarmUpFrame() {

12
com.unity.uiwidgets/Runtime/engine/UIWidgetsGlobalConfiguration.cs


namespace Unity.UIWidgets.engine {
public static class UIWidgetsGlobalConfiguration {
//disable auto adjust framerate by default
public static bool EnableAutoAdjustFramerate = false;
//enable incremental gc by default
public static bool EnableIncrementalGC = true;
//disable debug at runtime by default
public static bool EnableDebugAtRuntime = false;
}
}

3
com.unity.uiwidgets/Runtime/engine/UIWidgetsGlobalConfiguration.cs.meta


fileFormatVersion: 2
guid: 9e540b63fd904e628c98d48d1b99b5ef
timeCreated: 1623723651
正在加载...
取消
保存