浏览代码
Merge branches 'kgdev' and 'master' of gitlab.cds.internal.unity3d.com:upm-packages/ui-widgets/com.unity.uiwidgets into kgdev
/main
Merge branches 'kgdev' and 'master' of gitlab.cds.internal.unity3d.com:upm-packages/ui-widgets/com.unity.uiwidgets into kgdev
/main
kg
6 年前
当前提交
e7c4db12
共有 6 个文件被更改,包括 164 次插入 和 30 次删除
-
110Editor/editor/EditorUtils.cs
-
12README.md
-
43Runtime/engine/DisplayMetrics.cs
-
20Runtime/engine/WidgetCanvas.cs
-
1Runtime/rendering/box.mixin.gen.cs
-
8Runtime/rendering/proxy_box.mixin.njk
|
|||
using System; |
|||
using System.Collections; |
|||
using System.Reflection; |
|||
using UnityEngine; |
|||
public class Startup { |
|||
static Startup() { |
|||
DisplayMetrics.SetDevicePixelRatioGetter(() => { return EditorGUIUtility.pixelsPerPoint; }); |
|||
public class EitorUtils { |
|||
static EitorUtils() { |
|||
DisplayMetricsProvider.provider = () => new EditorPlayerDisplayMetrics(); |
|||
} |
|||
} |
|||
|
|||
public class EditorPlayerDisplayMetrics : DisplayMetrics { |
|||
|
|||
float _lastDevicePixelRatio = 0; |
|||
|
|||
public void OnGUI() { |
|||
} |
|||
|
|||
public void Update() { |
|||
this._lastDevicePixelRatio = GameViewUtil.getGameViewDevicePixelRatio(); |
|||
} |
|||
|
|||
public float DevicePixelRatio { |
|||
get { return this._lastDevicePixelRatio; } |
|||
} |
|||
} |
|||
|
|||
internal static class GameViewUtil { |
|||
|
|||
static Type _gameViewType; |
|||
|
|||
static string _gameViewClassName = "UnityEditor.GameView"; |
|||
|
|||
public static float getGameViewDevicePixelRatio(float fallback = 1) { |
|||
loadTypeIfNeed(); |
|||
|
|||
EditorWindow gameview = getMainGameView(); |
|||
if (gameview == null) { |
|||
return fallback; |
|||
} |
|||
|
|||
bool lowResolutionForAspectRatios = false; |
|||
if (!getPropertyValue(gameview, "lowResolutionForAspectRatios", |
|||
ref lowResolutionForAspectRatios)) { |
|||
return fallback; |
|||
} |
|||
if (lowResolutionForAspectRatios) { |
|||
return 1; |
|||
} |
|||
|
|||
Vector2 sizeValue = new Vector2(); |
|||
if (!getFieldValue(gameview, "m_LastWindowPixelSize", ref sizeValue)) { |
|||
return fallback; |
|||
} |
|||
if (gameview.position.width > 0) { |
|||
return sizeValue.x / gameview.position.width; |
|||
} |
|||
if (gameview.position.height > 0) { |
|||
return sizeValue.y / gameview.position.height; |
|||
} |
|||
|
|||
return fallback; |
|||
} |
|||
|
|||
static EditorWindow getMainGameView() { |
|||
IEnumerable enumerable = null; |
|||
if (!getFieldValue(null, "s_GameViews", ref enumerable)) { |
|||
return null; |
|||
} |
|||
IEnumerator enumerator = enumerable != null ? enumerable.GetEnumerator() : null; |
|||
if (enumerator != null && enumerator.MoveNext()) { |
|||
return enumerator.Current as EditorWindow; |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
static bool getFieldValue<T>(object ins, string name, ref T result) { |
|||
var fieldInfo = _gameViewType.GetField(name, BindingFlags.Public |
|||
| BindingFlags.NonPublic |
|||
| BindingFlags.Static | BindingFlags.Instance); |
|||
if (fieldInfo == null) { |
|||
return false; |
|||
} |
|||
result = (T)fieldInfo.GetValue(ins); |
|||
return true; |
|||
} |
|||
|
|||
static void loadTypeIfNeed() { |
|||
if (_gameViewType == null) { |
|||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { |
|||
var type = assembly.GetType(_gameViewClassName); |
|||
if (type != null) { |
|||
_gameViewType = type; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
static bool getPropertyValue<T>(object ins, string name, ref T result) { |
|||
var property = _gameViewType.GetProperty(name, BindingFlags.Public |
|||
| BindingFlags.NonPublic | |
|||
BindingFlags.Static | BindingFlags.Instance); |
|||
if (property == null) { |
|||
return false; |
|||
} |
|||
|
|||
result = (T) property.GetValue(ins); |
|||
return true; |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue