Skip to content

Commit

Permalink
Merge pull request #300 from gree/feature/android_margin_adjustment_f…
Browse files Browse the repository at this point in the history
…or_keyboard

Feature/android margin adjustment for keyboard
  • Loading branch information
KojiNakamaru authored Mar 30, 2018
2 parents 7fbe291 + 31f2d12 commit a279a60
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Binary file modified dist/unity-webview.unitypackage
Binary file not shown.
Binary file modified dist/unity-webview.zip
Binary file not shown.
38 changes: 37 additions & 1 deletion plugins/WebViewObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public class WebViewObject : MonoBehaviour
Callback onError;
Callback onLoaded;
bool visibility;
int mMarginLeft;
int mMarginTop;
int mMarginRight;
int mMarginBottom;
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
IntPtr webView;
Rect rect;
Expand All @@ -60,12 +64,40 @@ public class WebViewObject : MonoBehaviour
#elif UNITY_ANDROID
AndroidJavaObject webView;

bool mIsKeyboardVisible0 = false;
bool mIsKeyboardVisible = false;

/// Called from Java native plugin to set when the keyboard is opened
public void SetKeyboardVisible(string pIsVisible)
{
mIsKeyboardVisible = (pIsVisible == "true");
if (mIsKeyboardVisible != mIsKeyboardVisible0)
{
mIsKeyboardVisible0 = mIsKeyboardVisible;
SetMargins(mMarginLeft, mMarginTop, mMarginRight, mMarginBottom);
}
}

public int AdjustBottomMargin(int bottom)
{
if (!mIsKeyboardVisible)
{
return bottom;
}
else
{
int keyboardHeight = 0;
using(AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
AndroidJavaObject View = UnityClass.GetStatic<AndroidJavaObject>("currentActivity").Get<AndroidJavaObject>("mUnityPlayer").Call<AndroidJavaObject>("getView");
using(AndroidJavaObject Rct = new AndroidJavaObject("android.graphics.Rect"))
{
View.Call("getWindowVisibleDisplayFrame", Rct);
keyboardHeight = View.Call<int>("getHeight") - Rct.Call<int>("height");
}
}
return (bottom > keyboardHeight) ? bottom : keyboardHeight;
}
}
#else
IntPtr webView;
Expand Down Expand Up @@ -376,8 +408,12 @@ public void SetMargins(int left, int top, int right, int bottom)
#elif UNITY_ANDROID
if (webView == null)
return;
webView.Call("SetMargins", left, top, right, bottom);
webView.Call("SetMargins", left, top, right, AdjustBottomMargin(bottom));
#endif
mMarginLeft = left;
mMarginTop = top;
mMarginRight = right;
mMarginBottom = bottom;
}

public void SetVisibility(bool v)
Expand Down

0 comments on commit a279a60

Please sign in to comment.