浏览代码

Merge branch 'dpi' into 'master'

fix device pixel ratio in editor

See merge request upm-packages/ui-widgets/com.unity.uiwidgets!73
/main
Shenhua Gu 6 年前
当前提交
ac4d8748
共有 3 个文件被更改,包括 147 次插入25 次删除
  1. 110
      Editor/editor/EditorUtils.cs
  2. 42
      Runtime/engine/DisplayMetrics.cs
  3. 20
      Runtime/engine/WidgetCanvas.cs

110
Editor/editor/EditorUtils.cs


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;
}
}
}

42
Runtime/engine/DisplayMetrics.cs


using UnityEngine;
namespace Unity.UIWidgets.engine {
public class DisplayMetrics {
static float _devicePixelRatio = 0;
public static class DisplayMetricsProvider {
public static Func<DisplayMetrics> provider = () => new PlayerDisplayMetrics();
}
public interface DisplayMetrics {
void OnGUI();
void Update();
float DevicePixelRatio { get; }
}
static Func<float> _devicePixelRatioGetter;
public class PlayerDisplayMetrics: DisplayMetrics {
float _devicePixelRatio = 0;
public static void SetDevicePixelRatioGetter(Func<float> f) {
_devicePixelRatioGetter = f;
public void OnGUI() {
public static float devicePixelRatio {
public void Update() {
}
public float DevicePixelRatio {
if (_devicePixelRatioGetter != null) {
return _devicePixelRatioGetter();
}
if (_devicePixelRatio > 0) {
return _devicePixelRatio;
if (this._devicePixelRatio > 0) {
return this._devicePixelRatio;
}
#if UNITY_ANDROID

_devicePixelRatio = IOSDeviceSaleFactor();
#endif
if (_devicePixelRatio <= 0) {
_devicePixelRatio = 1;
if (this._devicePixelRatio <= 0) {
this._devicePixelRatio = 1;
return _devicePixelRatio;
return this._devicePixelRatio;
}
}

20
Runtime/engine/WidgetCanvas.cs


}
protected override Vector2 queryWindowSize() {
var size = this._widgetCanvas.rectTransform.rect.size;
size = size * this._widgetCanvas.canvas.scaleFactor / this._widgetCanvas.devicePixelRatio;
return new Vector2(Mathf.Round(size.x), Mathf.Round(size.y));
var rect = RectTransformUtility.PixelAdjustRect(this._widgetCanvas.rectTransform,
this._widgetCanvas.canvas);
var size = new Vector2(rect.width, rect.height) / this._widgetCanvas.devicePixelRatio;
size.x = Mathf.Round(size.x);
size.y = Mathf.Round(size.y);
return size;
}
}

static Event _repaintEvent;
[SerializeField] protected float devicePixelRatioOverride;
WindowAdapter _windowAdapter;
Texture _texture;
Vector2 _lastMouseMove;

const int mouseScrollId = mouseButtonNum + 1;
readonly ScrollInput _scrollInput = new ScrollInput();
DisplayMetrics _displayMetrics;
this._displayMetrics = DisplayMetricsProvider.provider();
if (_repaintEvent == null) {
_repaintEvent = new Event {type = EventType.Repaint};

get {
return this.devicePixelRatioOverride > 0
? this.devicePixelRatioOverride
: DisplayMetrics.devicePixelRatio;
: this._displayMetrics.DevicePixelRatio;
}
}

this.texture = texture;
this.material = mat;
}
this._displayMetrics.Update();
if (EventSystem.current != null && EventSystem.current.currentSelectedGameObject != this.gameObject) {
this.unfocusIfNeeded();
}

}
void OnGUI() {
this._displayMetrics.OnGUI();
if (Event.current.type == EventType.KeyDown || Event.current.type == EventType.KeyUp) {
this._windowAdapter.OnGUI(Event.current);
}

正在加载...
取消
保存