浏览代码

Merge pull request #115 from Unity-Technologies/siyaoH/1.17/android

replace www with UnityWebRequest
/siyaoH-1.17-PlatformMessage
GitHub 4 年前
当前提交
4b4b260d
共有 4 个文件被更改,包括 78 次插入22 次删除
  1. 26
      com.unity.uiwidgets/Runtime/engine2/AndroidPlatformUtil.cs
  2. 7
      com.unity.uiwidgets/Runtime/engine2/UIWidgetsPanel.cs
  3. 23
      com.unity.uiwidgets/Runtime/painting/image_provider.cs
  4. 44
      com.unity.uiwidgets/Runtime/engine2/DisplayMetrics.cs

26
com.unity.uiwidgets/Runtime/engine2/AndroidPlatformUtil.cs


using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine.Networking;
using NativeBindings = Unity.UIWidgets.ui.NativeBindings;
namespace Unity.UIWidgets.engine2 {
public static class AndroidPlatformUtil {

internal static string unpackFile(string file) {
var dir = Application.temporaryCachePath + "/";
if (!File.Exists(dir + file)) {
WWW unpackerWWW = new WWW("jar:file://" + Application.dataPath + "!/assets/" + file);
while (!unpackerWWW.isDone) {
} // This will block in the webplayer.
if (!string.IsNullOrEmpty(unpackerWWW.error)) {
Debug.Log("Error unpacking 'jar:file://" + Application.dataPath + "!/assets/" + file +
"'");
dir = "";
return dir + file;
var path = "jar:file://" + Application.dataPath + "!/assets/" + file;
using(var unpackerWWW = UnityWebRequest.Get(path)) {
unpackerWWW.SendWebRequest();
while (!unpackerWWW.isDone) {
} // This will block in the webplayer.
if (unpackerWWW.isNetworkError || unpackerWWW.isHttpError) {
Debug.Log($"Failed to get file \"{path}\": {unpackerWWW.error}");
return "";
}
var data = unpackerWWW.downloadHandler.data;
FileInfo fileInfo = new System.IO.FileInfo(dir + file);
fileInfo.Directory.Create();
File.WriteAllBytes(fileInfo.FullName, data);
System.IO.FileInfo fileInfo = new System.IO.FileInfo(dir + file);
fileInfo.Directory.Create();
System.IO.File.WriteAllBytes(fileInfo.FullName, unpackerWWW.bytes);
}
return dir + file;

7
com.unity.uiwidgets/Runtime/engine2/UIWidgetsPanel.cs


static bool _ShowDebugLog;
public float devicePixelRatioOverride;
DisplayMetrics _displayMetrics = new DisplayMetrics();
float _devicePixelRatioOverride;
public bool hardwareAntiAliasing;

float _currentDevicePixelRatio {
get {
#if !UNITY_EDITOR
return _displayMetrics.DevicePixelRatioByDefault;
#endif
var currentDpi = Screen.dpi;
if (currentDpi == 0) {
currentDpi = canvas.GetComponent<CanvasScaler>().fallbackScreenDPI;

23
com.unity.uiwidgets/Runtime/painting/image_provider.cs


using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Unity.UIWidgets.async2;
using Unity.UIWidgets.engine2;

#else
var path = "file://" + Path.Combine(Application.streamingAssetsPath, key.file);
#endif
WWW unpackerWWW = new WWW(path);
while (!unpackerWWW.isDone) {
} // This will block in the webplayer.
if (unpackerWWW.bytes.Length > 0) {
return decode(unpackerWWW.bytes);
}
using(var unpackerWWW = UnityWebRequest.Get(path)) {
unpackerWWW.SendWebRequest();
while (!unpackerWWW.isDone) {
} // This will block in the webplayer.
if (unpackerWWW.isNetworkError || unpackerWWW.isHttpError) {
throw new Exception($"Failed to get file \"{path}\": {unpackerWWW.error}");
}
throw new Exception("not loaded");
var data = unpackerWWW.downloadHandler.data;
if (data.Length > 0) {
return decode(data);
}
throw new Exception("not loaded");
}
}
IEnumerator _loadBytes(FileImage key) {

44
com.unity.uiwidgets/Runtime/engine2/DisplayMetrics.cs


using UnityEngine;
namespace Unity.UIWidgets.engine2 {
public class DisplayMetrics {
float _devicePixelRatioByDefault;
public float DevicePixelRatioByDefault {
get {
if (_devicePixelRatioByDefault > 0) {
return _devicePixelRatioByDefault;
}
#if UNITY_ANDROID
_devicePixelRatioByDefault = AndroidDevicePixelRatio();
#endif
if (_devicePixelRatioByDefault <= 0) {
_devicePixelRatioByDefault = 1;
}
return _devicePixelRatioByDefault;
}
}
#if UNITY_ANDROID
static float AndroidDevicePixelRatio() {
using (
AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")
) {
using (
AndroidJavaObject metricsInstance = new AndroidJavaObject("android.util.DisplayMetrics"),
activityInstance = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity"),
windowManagerInstance = activityInstance.Call<AndroidJavaObject>("getWindowManager"),
displayInstance = windowManagerInstance.Call<AndroidJavaObject>("getDefaultDisplay")
) {
displayInstance.Call("getMetrics", metricsInstance);
return metricsInstance.Get<float>("density");
}
}
}
#endif
}
}
正在加载...
取消
保存