版本信息

Unity 5.4.2p3

Native Plugin UniWebView 2.7.1

特別說明

這篇文章意圖

發現最近很火游戲里面嵌入網頁,大家都在問這玩意怎么搞

一般如何接入的文章倒是巨多,但是很多缺陷問題,卻沒人解決,於是就總結下自己的使用

博主比較懶,因此Mac版的一直沒去弄,感覺也沒太大意義(其實主要是因為運行就error,懶得去fix),哪天心情比較好再考慮去搞吧

本文章重點

解決在Java中必須繼承UniWebViewActivity的缺陷

解決一些網頁瀏覽適配的問題

結合NGUI設置頁面大小

建議參考

代碼部分

目錄結構

0d9a271a174e49d75f45410fbbf70ac6.png

- UniWebView目錄結構就如圖了

Java不繼承UniWebView Activity如何使用

原作者說明

By default, Unity is using an activity called UnityPlayerActivity in Android as the main activity. UniWebView needs to run in its own activity as the main activity avoid some potential issues such as input handler or background switch. The activity which UniWebView is using by default is a subclass of UnityPlayerActivity, so it will not break anything of origin Unity. But sometimes you have to let other plugin’s activity to run as the main activity. Fortunately, UniWebView is shipped with original source code for Android, so you can modify the source code and recompile it as you need. This guide will show you how to do it.

AndroidManifest.xml

package="com.Web.UniWebView" >

不要詫異什么都沒有配置,因為這玩意真的不需要

關鍵點,修改UniWebView.cs

#if UNITY_ANDROID

private void OnApplicationPause(bool pauseStatus)

{

if (pauseStatus)

{

Hide();

}

else

{

Show();

}

}

#endif

如果不繼承的話,鎖屏或者切界面的時候,有可能會導致原來WebView對Unity界面的觸摸攔截失效

其實這樣子很好處理,既然是在鎖屏或者且界面的時候出現的問題,那就在這個時候做些特殊處理

其實就像一個堆棧,Unity彈出來前,WebView先彈出來,Unity入棧了,WebView就跟着入棧,保證WebView在Unity上面,就這么簡單幾句就可以了

如果有些項目有些特殊需求,具體問題具體分析吧,至少我們項目足夠處理了

華為虛擬按鍵

///

/// Get or set a value indicating whether this is in immersive mode.

///

/// true if immersive mode; otherwise, false.

///

///

///

public bool immersiveMode {

get {

return _immersiveMode;

}

set {

#if UNITY_ANDROID && !UNITY_EDITOR

_immersiveMode = value;

UniWebViewPlugin.SetImmersiveModeEnabled(gameObject.name, _immersiveMode);

#endif

}

}

Unity默認是顯示虛擬按鍵的

UniWebView默認是不顯示虛擬按鍵的,所以這里再初始化的時候需要設置為false

界面自適應縮放

AndroidPlugin.java

public static void _UniWebViewReload(String name)

{

runSafelyOnUiThread(new Runnable(name)

{

public void run() {

Log.d("UniWebView", "_UniWebViewReload");

UniWebViewDialog dialog = UniWebViewManager.Instance().getUniWebViewDialog(AndroidPlugin.this);

if (dialog != null)

dialog.reload();

}

});

}

UniWebViewDialog.java

public void loadWithOverviewMode(boolean use) {

this._uniWebView.getSettings().setLoadWithOverviewMode(use);

}

UniWeb ViewPluginAndroid.cs

public static void SetUseLoadWithOverviewMode(string name, bool use)

{

if (Application.platform == RuntimePlatform.Android)

{

webView.CallStatic("_UniWebViewLoadWithOverviewMode", name, use);

}

}

UniWebView.cs

public void SetUseLoadWithOverviewMode(bool use)

{

#if UNITY_ANDROID && !UNITY_EDITOR

UniWebViewPlugin.SetUseLoadWithOverviewMode(gameObject.name, use);

#endif

}

重點

在Java層增添了一個接口,在初始化的時候設置為true

這需要重新編譯UniWebView的java源碼,然后在導出jar

雖然原作者是在Mac下操作生成jar的,但是在Eclipse上生成完全沒有問題,問題基本是Min SDK或者是Target SDK問題吧,博主手殘刪了自己的Eclipse工程,所以下次要用到我再更新博客吧(我好懶,yeah)

結合NGUI適應頁面

邊距

///

/// Get the height of the screen.

///

///

/// The height of screen.

///

///

/// In iOS devices, it will always return the screen width in "point",

/// instead of "pixel". It would be useful to use this value to calculate webview size.

/// On other platforms, it will just return Unity's Screen.height.

/// For example, a portrait iPhone 5 will return 320 and a landscape one 568. You should

/// always use this value to do screen-size-based insets calculation.

///

public static int screenWidth {

get {

#if UNITY_IOS && !UNITY_EDITOR

return UniWebViewPlugin.ScreenWidth();

#else

return Screen.width;

#endif

}

}

看說明,在iOS上,使用的並不是像素,而是邊距,因此我們在所有的計算上,都應該進行一個轉換

簡單的”pixel”轉”point”

public static int ConvertPixelToPoint(float pixel, bool width)

{

#if UNITY_IOS && !UNITY_EDITOR

float scale = 0;

if (width)

{

scale = 1f * screenWidth / Screen.width;

}

else

{

scale = 1f * screenHeight / Screen.height;

}

return (int)(pixel * scale);

#endif

return (int)pixel;

}

}

很簡單的寫法,在iOS上轉換一下,根據比例

結合NGUI使用

using UnityEngine;

public static class UniWebViewExtHelper

{

public static UniWebView CreateUniWebView(UIWidget widget, string url)

{

if (widget == null || !widget.isActiveAndEnabled)

{

return null;

}

// 強制刷新一遍

widget.ResetAndUpdateAnchors();

var camera = widget.anchorCamera;

var bottomLeft = camera.WorldToScreenPoint(widget.worldCorners[0]);

var topRight = camera.WorldToScreenPoint(widget.worldCorners[2]);

return CreateUniWebView(widget.gameObject, url, Screen.height - topRight.y, bottomLeft.x, bottomLeft.y, Screen.width - topRight.x);

}

public static UniWebView CreateUniWebView(GameObject go, string url, float top, float left, float bottom, float right)

{

if (go == null || !go.activeSelf)

{

return null;

}

var view = go.GetComponent();

if (view == null)

{

view = go.AddComponent();

}

view.insets = new UniWebViewEdgeInsets(UniWebViewHelper.ConvertPixelToPoint(top, false), UniWebViewHelper.ConvertPixelToPoint(left, true), UniWebViewHelper.ConvertPixelToPoint(bottom, false), UniWebViewHelper.ConvertPixelToPoint(right, true));

view.SetShowSpinnerWhenLoading(false);

view.immersiveMode = false;

view.url = url;

return view;

}

}

為了防止Widget的大小還會變,強制刷新一遍

獲取Widget在屏幕中的位置,算出距離邊的像素距離

換成point距離,然后初始化

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐