siyao
4 年前
当前提交
88d5673e
共有 18 个文件被更改,包括 531 次插入 和 11 次删除
-
1Samples/UIWidgetsSamples_2019_4/Assets/UIWidgetsGallery/GalleryMain.cs
-
20com.unity.uiwidgets/Runtime/engine2/AndroidPlatformUtil.cs
-
86com.unity.uiwidgets/Runtime/engine2/DisplayMetrics.cs
-
6com.unity.uiwidgets/Runtime/engine2/UIWidgetsPanel.cs
-
2com.unity.uiwidgets/Runtime/engine2/UIWidgetsPanelWrapper.cs
-
1com.unity.uiwidgets/Runtime/rendering/binding.cs
-
5com.unity.uiwidgets/Runtime/ui2/window.cs
-
2com.unity.uiwidgets/Runtime/widgets/binding.cs
-
44com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsMessageManager.java
-
32com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsMessageManager.java.meta
-
216com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsViewController.java
-
32com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsViewController.java.meta
-
13com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsViewMetrics.java
-
32com.unity.uiwidgets/Runtime/Plugins/Android/UIWidgetsViewMetrics.java.meta
-
18com.unity.uiwidgets/Runtime/Plugins/Android/Utils.java
-
32com.unity.uiwidgets/Runtime/Plugins/Android/Utils.java.meta
|
|||
package com.unity.uiwidgets.plugin; |
|||
|
|||
import android.util.Log; |
|||
|
|||
import com.unity3d.player.UnityPlayer; |
|||
|
|||
import org.json.JSONArray; |
|||
import org.json.JSONException; |
|||
import org.json.JSONObject; |
|||
|
|||
import java.util.List; |
|||
|
|||
public class UIWidgetsMessageManager { |
|||
public static final String TAG = "UIWidgets"; |
|||
|
|||
private static UIWidgetsMessageManager _instance; |
|||
|
|||
private String gameObjectName; |
|||
|
|||
|
|||
public static UIWidgetsMessageManager getInstance() { |
|||
if (_instance == null) { |
|||
_instance = new UIWidgetsMessageManager(); |
|||
} |
|||
return _instance; |
|||
} |
|||
|
|||
public void SetObjectName(String name) { |
|||
gameObjectName = name; |
|||
} |
|||
|
|||
public void UIWidgetsMethodMessage(String channel, String method, List<Object> args) { |
|||
JSONObject object = new JSONObject(); |
|||
|
|||
try { |
|||
object.put("channel", channel); |
|||
object.put("method", method); |
|||
object.put("args", new JSONArray(args)); |
|||
UnityPlayer.UnitySendMessage(gameObjectName, "OnUIWidgetsMethodMessage", object.toString()); |
|||
} catch (JSONException e) { |
|||
Log.e(TAG, "error parse json", e); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: fbff29dbbe1ce46a5bd2b95fef473852 |
|||
PluginImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
iconMap: {} |
|||
executionOrder: {} |
|||
defineConstraints: [] |
|||
isPreloaded: 0 |
|||
isOverridable: 1 |
|||
isExplicitlyReferenced: 0 |
|||
validateReferences: 1 |
|||
platformData: |
|||
- first: |
|||
Android: Android |
|||
second: |
|||
enabled: 1 |
|||
settings: {} |
|||
- first: |
|||
Any: |
|||
second: |
|||
enabled: 0 |
|||
settings: {} |
|||
- first: |
|||
Editor: Editor |
|||
second: |
|||
enabled: 0 |
|||
settings: |
|||
DefaultValueInitialized: true |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
package com.unity.uiwidgets.plugin; |
|||
|
|||
import android.util.Log; |
|||
|
|||
import com.unity3d.player.UnityPlayer; |
|||
import android.graphics.Rect; |
|||
import android.os.Build; |
|||
import android.util.Log; |
|||
import android.util.TypedValue; |
|||
import android.view.View; |
|||
import android.view.ViewGroup; |
|||
import android.view.ViewTreeObserver; |
|||
import android.app.Activity; |
|||
import android.content.res.Configuration; |
|||
import android.content.res.Resources; |
|||
import android.view.Surface; |
|||
import java.util.Arrays; |
|||
import android.view.WindowManager; |
|||
import android.os.Handler; |
|||
import java.lang.reflect.Method; |
|||
|
|||
public class UIWidgetsViewController { |
|||
|
|||
private static UIWidgetsViewController _instance; |
|||
|
|||
public static UIWidgetsViewController getInstance() { |
|||
if (_instance == null) { |
|||
_instance = new UIWidgetsViewController(); |
|||
_instance.setup(); |
|||
} |
|||
return _instance; |
|||
} |
|||
|
|||
private UIWidgetsViewMetrics viewMetrics; |
|||
private boolean keyboardOpen; |
|||
|
|||
private float statusHeight; |
|||
private float navigationBarHeight; |
|||
|
|||
private void setup() { |
|||
//Log.i("tag", "On Setup 2"); |
|||
|
|||
keyboardOpen = false; |
|||
viewMetrics = new UIWidgetsViewMetrics(); |
|||
setupHeights(); |
|||
updateViewMetrics(); |
|||
|
|||
setupViewMetricsChangedListener(); |
|||
} |
|||
|
|||
private void setupHeights() { |
|||
final View unityView = ((ViewGroup)UnityPlayer.currentActivity.findViewById(android.R.id.content)).getChildAt(0); |
|||
Rect rect = new Rect(); |
|||
unityView.getWindowVisibleDisplayFrame(rect); |
|||
|
|||
statusHeight = rect.top; |
|||
navigationBarHeight = unityView.getRootView().getHeight() - rect.bottom; |
|||
} |
|||
|
|||
public static UIWidgetsViewMetrics getMetrics() { |
|||
UIWidgetsViewController controller = getInstance(); |
|||
return controller.viewMetrics; |
|||
} |
|||
|
|||
enum ZeroSides { NONE, LEFT, RIGHT, BOTH } |
|||
ZeroSides calculateShouldZeroSides(View unityView) { |
|||
Activity activity = UnityPlayer.currentActivity; |
|||
int orientation = activity.getResources().getConfiguration().orientation; |
|||
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); |
|||
|
|||
if (orientation == Configuration.ORIENTATION_LANDSCAPE) { |
|||
if (rotation == Surface.ROTATION_90) { |
|||
return ZeroSides.RIGHT; |
|||
} |
|||
else if (rotation == Surface.ROTATION_270) { |
|||
return Build.VERSION.SDK_INT >= 23 ? ZeroSides.LEFT : ZeroSides.RIGHT; |
|||
} else if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) { |
|||
return ZeroSides.BOTH; |
|||
} |
|||
} |
|||
|
|||
return ZeroSides.NONE; |
|||
} |
|||
|
|||
public boolean lastStatusBarHidden = true; |
|||
|
|||
private int calculateBottomKeyboardInset(Rect insets) { |
|||
if (keyboardOpen) { |
|||
return insets.bottom; |
|||
} else { |
|||
return 0; |
|||
} |
|||
} |
|||
|
|||
|
|||
private int getNavigationBarHeight() { |
|||
Resources resources = UnityPlayer.currentActivity.getResources(); |
|||
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); |
|||
if (resourceId > 0) |
|||
{ |
|||
return hasNavigationBar() ? resources.getDimensionPixelSize(resourceId) : 0; |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
private boolean hasNavigationBar() { |
|||
boolean hasBar = false; |
|||
Resources resources = UnityPlayer.currentActivity.getResources(); |
|||
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android"); |
|||
if (id > 0) { |
|||
hasBar = resources.getBoolean(id); |
|||
} |
|||
try { |
|||
Class systemPropertiesClass = Class.forName("android.os.SystemProperties"); |
|||
Method m = systemPropertiesClass.getMethod("get", String.class); |
|||
String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys"); |
|||
if ("1".equals(navBarOverride)) { |
|||
hasBar = false; |
|||
} else if ("0".equals(navBarOverride)) { |
|||
hasBar = true; |
|||
} |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
//Log.i("UIWidgetsDebug", " hasBar: " + hasBar); |
|||
return hasBar; |
|||
} |
|||
|
|||
public void updateViewMetrics() { |
|||
final View unityView = ((ViewGroup)UnityPlayer.currentActivity.findViewById(android.R.id.content)).getChildAt(0); |
|||
Rect rect = new Rect(); |
|||
unityView.getWindowVisibleDisplayFrame(rect); |
|||
|
|||
//Log.i("UIWidgetsDebug", "calculation: " + unityView.getRootView().getHeight() + " " + rect.bottom + " " + rect.top); |
|||
|
|||
rect.bottom = unityView.getRootView().getHeight() - (rect.bottom - rect.top) - rect.top; |
|||
rect.right = unityView.getRootView().getWidth() - (rect.right - rect.left) - rect.left; |
|||
|
|||
boolean statusBarHidden = (View.SYSTEM_UI_FLAG_FULLSCREEN & unityView.getWindowSystemUiVisibility()) != 0; |
|||
boolean navigationBarHidden = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION & unityView.getWindowSystemUiVisibility()) != 0; |
|||
|
|||
ZeroSides zeroSides = ZeroSides.NONE; |
|||
if (navigationBarHidden) { |
|||
zeroSides = calculateShouldZeroSides(unityView); |
|||
} |
|||
|
|||
viewMetrics.padding_top = rect.top; |
|||
viewMetrics.padding_right = zeroSides == ZeroSides.RIGHT || zeroSides == ZeroSides.BOTH ? 0 : rect.right; |
|||
viewMetrics.padding_bottom = 0; |
|||
viewMetrics.padding_left = zeroSides == ZeroSides.LEFT || zeroSides == ZeroSides.BOTH ? 0 : rect.left; |
|||
|
|||
viewMetrics.insets_top = 0; |
|||
viewMetrics.insets_right = 0; |
|||
viewMetrics.insets_bottom = navigationBarHidden? calculateBottomKeyboardInset(rect) : rect.bottom; |
|||
viewMetrics.insets_left = 0; |
|||
|
|||
//adjust |
|||
viewMetrics.insets_bottom -= navigationBarHeight; |
|||
viewMetrics.padding_top -= statusHeight; |
|||
|
|||
//Log.i("UIWidgetsDebug", "checks: " + navigationBarHidden + " " + rect.bottom); |
|||
//Log.i("UIWidgetsDebug", " padding: " + viewMetrics.padding_top + " " + viewMetrics.padding_right + " " + viewMetrics.padding_bottom + " " + viewMetrics.padding_left); |
|||
//Log.i("UIWidgetsDebug", " insets: " + viewMetrics.insets_top + " " + viewMetrics.insets_right + " " + viewMetrics.insets_bottom + " " + viewMetrics.insets_left); |
|||
} |
|||
|
|||
public void setupViewMetricsChangedListener() { |
|||
final View unityView = ((ViewGroup)UnityPlayer.currentActivity.findViewById(android.R.id.content)).getChildAt(0); |
|||
|
|||
UnityPlayer.currentActivity.runOnUiThread(new Runnable() { |
|||
@Override |
|||
public void run() { |
|||
unityView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() { |
|||
@Override |
|||
public void onSystemUiVisibilityChange(final int visibility){ |
|||
boolean statusBarHidden = (View.SYSTEM_UI_FLAG_FULLSCREEN & visibility) != 0; |
|||
unityView.setSystemUiVisibility(visibility); |
|||
UIWidgetsViewController controller = getInstance(); |
|||
if (statusBarHidden != controller.lastStatusBarHidden) { |
|||
controller.onViewMetricsChanged(); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
}); |
|||
|
|||
|
|||
unityView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { |
|||
private final int defaultKeyboardHeightDP = 100; |
|||
private final int estimatedKeyboardDP = defaultKeyboardHeightDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0); |
|||
private final Rect rect = new Rect(); |
|||
|
|||
@Override |
|||
public void onGlobalLayout() { |
|||
int estimatedKeyboardHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, estimatedKeyboardDP, unityView.getResources().getDisplayMetrics()); |
|||
unityView.getWindowVisibleDisplayFrame(rect); |
|||
int heightDiff = unityView.getRootView().getHeight() - (rect.bottom - rect.top); |
|||
boolean isShown = heightDiff >= estimatedKeyboardHeight; |
|||
|
|||
if (keyboardOpen == isShown) { |
|||
return; |
|||
} |
|||
|
|||
keyboardOpen = isShown; |
|||
onViewMetricsChanged(); |
|||
} |
|||
} |
|||
); |
|||
} |
|||
|
|||
public void onViewMetricsChanged() { |
|||
updateViewMetrics(); |
|||
UIWidgetsMessageManager.getInstance().UIWidgetsMethodMessage("ViewportMatricsChanged", "UIWidgetViewController.keyboardChanged", |
|||
Arrays.asList("")); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: dd01a28ebf279449dace0dc8580a9ece |
|||
PluginImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
iconMap: {} |
|||
executionOrder: {} |
|||
defineConstraints: [] |
|||
isPreloaded: 0 |
|||
isOverridable: 1 |
|||
isExplicitlyReferenced: 0 |
|||
validateReferences: 1 |
|||
platformData: |
|||
- first: |
|||
Android: Android |
|||
second: |
|||
enabled: 1 |
|||
settings: {} |
|||
- first: |
|||
Any: |
|||
second: |
|||
enabled: 0 |
|||
settings: {} |
|||
- first: |
|||
Editor: Editor |
|||
second: |
|||
enabled: 0 |
|||
settings: |
|||
DefaultValueInitialized: true |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
package com.unity.uiwidgets.plugin; |
|||
|
|||
public class UIWidgetsViewMetrics { |
|||
public float insets_top; |
|||
public float insets_bottom; |
|||
public float insets_left; |
|||
public float insets_right; |
|||
|
|||
public float padding_top; |
|||
public float padding_bottom; |
|||
public float padding_left; |
|||
public float padding_right; |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: efb8c112055a04b7dae8566ccf299bc7 |
|||
PluginImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
iconMap: {} |
|||
executionOrder: {} |
|||
defineConstraints: [] |
|||
isPreloaded: 0 |
|||
isOverridable: 1 |
|||
isExplicitlyReferenced: 0 |
|||
validateReferences: 1 |
|||
platformData: |
|||
- first: |
|||
Android: Android |
|||
second: |
|||
enabled: 1 |
|||
settings: {} |
|||
- first: |
|||
Any: |
|||
second: |
|||
enabled: 0 |
|||
settings: {} |
|||
- first: |
|||
Editor: Editor |
|||
second: |
|||
enabled: 0 |
|||
settings: |
|||
DefaultValueInitialized: true |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
package com.unity.uiwidgets.plugin; |
|||
import android.view.WindowManager; |
|||
|
|||
import com.unity3d.player.UnityPlayer; |
|||
|
|||
public class Utils { |
|||
public static void SetStatusBarState(final boolean show) { |
|||
UnityPlayer.currentActivity.runOnUiThread(new Runnable() { |
|||
public void run() { |
|||
if (show) { |
|||
UnityPlayer.currentActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); |
|||
} else { |
|||
UnityPlayer.currentActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 418c6c9d61a804a9e817e77aa7f83f67 |
|||
PluginImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
iconMap: {} |
|||
executionOrder: {} |
|||
defineConstraints: [] |
|||
isPreloaded: 0 |
|||
isOverridable: 1 |
|||
isExplicitlyReferenced: 0 |
|||
validateReferences: 1 |
|||
platformData: |
|||
- first: |
|||
Android: Android |
|||
second: |
|||
enabled: 1 |
|||
settings: {} |
|||
- first: |
|||
Any: |
|||
second: |
|||
enabled: 0 |
|||
settings: {} |
|||
- first: |
|||
Editor: Editor |
|||
second: |
|||
enabled: 0 |
|||
settings: |
|||
DefaultValueInitialized: true |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue