Uniwebview2插件常见问题以及刘海屏屏幕适配,屏幕旋转的解决方案
uniwebview2
是什么
? uniwebview2是一個在Android和IOS移動端平臺下的Unity內置瀏覽器插件。主要是用來在游戲界面的最上面顯示運營開發(fā)的web活動或者論壇等網(wǎng)頁。
為什么
? Unity自帶了一個打開網(wǎng)址url的函數(shù)接口
Application.OpenURL(url);不過該接口會調用手機自帶的瀏覽器來打開該網(wǎng)頁,但是玩家想要返回游戲就比較困難,還需要重新去找對應的游戲App,因此需要一個插件能夠內置在游戲中,在游戲內打開瀏覽器,方便關閉網(wǎng)頁之后接著運行游戲。
怎么做
? 將下載好的uniwebview2的插件包導入unity之后,主要看UniWebView.cs這個文件,所有和網(wǎng)頁相關的前進,后退,刷新,返回,屏幕旋轉適配都是在這個文件中。我們需要將其封裝成一個外部可以直接調用的接口來使用。
打開網(wǎng)頁接口
SDK.UniWebViewManager.Instance.OpenUrl(Url, CallBack);一共需要兩個參數(shù),一個是string類型的url(需要打開的網(wǎng)址要有http或者https開頭的)和System.Action類型的回調函數(shù)(用于在網(wǎng)頁關閉的時候調用)
關閉網(wǎng)頁接口
SDK.UniWebViewManager.Instance.DoCloseAll();網(wǎng)頁前進接口
SDK.UniWebViewManager.Instance.GoForward();網(wǎng)頁后退接口
SDK.UniWebViewManager.Instance.GoBack();網(wǎng)頁刷新接口
SDK.UniWebViewManager.Instance.Reload();Q&A
Q1:在打開游戲內的uniwebview2情況下,將游戲App切到后臺導致失去焦點,重新進入游戲的時候游戲無法響應
A1: 在游戲暫停的時候將網(wǎng)頁給隱藏,當游戲切回來的時候重新調用顯示即可。打開UniWebView.cs文件添加如下代碼
/// <summary>/// 添加UniWebView插件退到后臺導致的失去焦點問題;/// </summary>/// <param name="pauseStatus"></param>private void OnApplicationPause(bool pauseStatus){ #if UNITY_IOS || UNITY_ANDROIDif (pauseStatus){Hide();}else{Show();} #endif}Q2:手指快速點擊按鈕的情況下會導致多次調用,導致打開了多個網(wǎng)頁
A2: 修改內置瀏覽器調用方式為在界面打開的時候才調用SDK接口,這樣就避免雙擊的時候多次調用導致的打開多個網(wǎng)頁。我們新建一個Panel界面,在點擊按鈕的時候先打開界面,然后再界面的Open和Close的時候調用網(wǎng)頁的打開和關閉接口。PanelUniWebViewController就是內置瀏覽器的通用打開界面。
//做一些打開界面時的處理.;protected override void OnPanelOpen(){if (data == null){LogHelper.LogError("內置瀏覽器插件的傳入數(shù)據(jù)為空;");return;}//界面打開的時候調用內置瀏覽器SDK的接口,避免多次調用會導致打開多個網(wǎng)頁的問題;SDK.UniWebViewManager.Instance.OpenUrl(data.Url, data.CallBack);}//關閉按鈕的回調函數(shù)private void OnClickCloseEvt(){// 關閉所有的網(wǎng)頁;DNA.SDK.UniWebViewManager.Instance.DoCloseAll();}Q3:uniwebview2瀏覽器打開的都是全屏的網(wǎng)頁,我們需要定制瀏覽器的顯示區(qū)域要怎么辦?比如如何適配劉海屏
A3:UniWebView2有一個屏幕旋轉變化的時候回調函數(shù),我們只需要監(jiān)聽并做相應的適配即可。
webView.InsetsForScreenOreitation += InsetsForScreenOreitation;我們在對應的InsetsForScreenOreitation監(jiān)聽函數(shù)里面添加代碼
// This method will be called when the screen orientation changed. Here we return UniWebViewEdgeInsets(5,5,5,5)// for both situation, which means the inset is 5 point for iOS and 5 pixels for Android from all edges.// Note: UniWebView is using point instead of pixel in iOS. However, the `Screen.width` and `Screen.height` will give you a// pixel-based value. // You could get a point-based screen size by using the helper methods: `UniWebViewHelper.screenHeight` and `UniWebViewHelper.screenWidth` for iOS.UniWebViewEdgeInsets InsetsForScreenOreitation(UniWebView webView, UniWebViewOrientation orientation){#if UNI_WEB_VIEW #if UNITY_IOS || UNITY_ANDROID || UNITY_WP8//SCREEN_WIDTH = 1136;//SCREEN_HEIGHT = 640;LogHelper.Log("InsetsForScreenOreitation orientation " + orientation);// int aTop, int aLeft, int aBottom, int aRight;// 橫屏;if (orientation == UniWebViewOrientation.LandScape){aTop = 0;aLeft = 88;aBottom = 88;aRight = 88;// 適配多分辨率的情況;// 1136 => 88;// Width => Width/1136*88;aLeft = (aLeft * UniWebViewHelper.screenWidth) / SCREEN_WIDTH;aRight = (aRight * UniWebViewHelper.screenWidth) / SCREEN_WIDTH;aBottom = (aBottom * UniWebViewHelper.screenHeight) / SCREEN_HEIGHT;//用于測試不同手機的適配問題LogHelper.Log(StringUtil.Format("UniWeb aTop = {0}, aLeft = {1}, aBottom = {2}, aRight = {3}", aTop, aLeft, aBottom, aRight)); EventMsgCenter.SendMsg(EEventName.Url_UpdateSize);return new UniWebViewEdgeInsets(aTop, aLeft, aBottom, aRight);}else{aTop = 0;aLeft = 0;aBottom = 88;aRight = 0;// 豎屏;aBottom = (aBottom * UniWebViewHelper.screenHeight) / SCREEN_HEIGHT;//用于測試不同手機的適配問題LogHelper.Log(StringUtil.Format("UniWeb aTop = {0}, aLeft = {1}, aBottom = {2}, aRight = {3}", aTop, aLeft, aBottom, aRight)); EventMsgCenter.SendMsg(EEventName.Url_UpdateSize);return new UniWebViewEdgeInsets(aTop, aLeft, aBottom, aRight);} #endif #endifreturn null;}UniWebViewEdgeInsets函數(shù)接口是用來返回瀏覽器的顯示區(qū)域,里面是4個參數(shù),分別是
既然是像素距離,那么我們就需要根據(jù)手機屏幕來重新計算,將對應的像素傳入進去,游戲中是按照1136*640來做的,所以當我們在1136下,距離左邊是88像素的時候,在其他分辨率下像素點計算如下:
aLeft = 88; // 適配多分辨率的情況; // 1136 => 88; // Width => Width/1136*88; aLeft = (aLeft * UniWebViewHelper.screenWidth) / SCREEN_WIDTH;同樣的在640下,距離底部88像素的時候,在其他分辨率下像素點計算如下:
aBottom = 88; // 適配多分辨率的情況; // 640 => 88; // Height => Height/640*88; aBottom = (aBottom * UniWebViewHelper.screenHeight) / SCREEN_HEIGHT;
由于劉海屏在Iphone下面的有個bar條的,所以這里的按鈕都設置在底部的背景框偏上的位置。返回按鈕在下面有個取巧的好處就是,不用去適配劉海的問題,因為劉海都是在手機橫屏時候的左右位置(手機豎屏時候的上下位置)。(PS:玩王者榮耀的時候看到他們的活動頁面的按鈕布局想到的啟示,所以沒事的時候多玩玩其他游戲還是很有好處的hhhh)
Q4:uniwebview2瀏覽器插件在IPhone下需要支持旋轉查看,就是在手機方向按鍵沒有鎖定的情況下,可以橫屏看也可以豎屏看,當退出瀏覽模式返回進入游戲的時候,還是按照之前的橫屏顯示,這個要怎么做?
A4:提這個問題是因為運營那邊設計網(wǎng)頁的時候內容顯示不下需要在豎屏的情況下面顯示。這個時候我們需要根據(jù)玩家操作,在打開網(wǎng)頁的時候讓屏幕可以支持旋轉,在關閉的時候只能橫向旋轉。 新建一個屏幕管理類ScreenManager,里面只需要兩個函數(shù),一個用來打開網(wǎng)頁的時候屏幕的控制,一個是關閉網(wǎng)頁的時候使用。
//Unity 屏幕管理類using DNA; using UnityEngine; #if UNITY_IOS || UNITY_IPHONE using System.Runtime.InteropServices; #endif public class ScreenManager : ReleaseSingleton<ScreenManager> {/// <summary>/// Url打開的時候設置橫屏/豎屏,允許自動旋轉屏幕/// </summary>public void OnUrlOpen(){ #if UNITY_IPHONE || UNITY_IOSDNA_U3D_SetPortrait(); #endiftemp = Screen.orientation;Screen.orientation = ScreenOrientation.AutoRotation;Screen.autorotateToLandscapeRight = true;Screen.autorotateToLandscapeLeft = true;Screen.autorotateToPortraitUpsideDown = false;Screen.autorotateToPortrait = true;}ScreenOrientation temp;/// <summary>/// Url關閉的時候設置回橫屏顯示/// </summary>public void OnUrlClose(){//IOS水平方向都可以 #if UNITY_IPHONE || UNITY_IOSDNA_U3D_SetLandscape();Screen.orientation = temp;Screen.autorotateToLandscapeRight = true;Screen.autorotateToLandscapeLeft = true;Screen.autorotateToPortraitUpsideDown = false;Screen.autorotateToPortrait = false;Screen.orientation = ScreenOrientation.AutoRotation;#elif UNITY_ANDROID//安卓鎖住方向Screen.orientation = ScreenOrientation.LandscapeLeft;Screen.autorotateToLandscapeLeft = true; #endif}#if UNITY_IPHONE || UNITY_IOS[DllImport("__Internal")]private static extern void DNA_U3D_SetLandscape();[DllImport("__Internal")]private static extern void DNA_U3D_SetPortrait(); #endif }IOS下需要增加兩個文件ScreenManager .h和ScreenManager .mm文件
#ifndef ScreenManager_h #define ScreenManager_h#import <Foundation/Foundation.h>#ifdef __cplusplus extern "C" { #endifvoid DNA_U3D_SetLandscape();void DNA_U3D_SetPortrait(); #ifdef __cplusplus } #endif@interface ScreenManager : NSObject @property (nonatomic, assign) BOOL isPortrait;- (ScreenManager *)sharedInstance;- (bool) DNA_U3D_GetPortrait;@end#endif /* ScreenManager_h */mm文件如下
#import "ScreenManager.h" #import <UIKit/UIKit.h>@interface ScreenManager () <NSObject>@endstatic ScreenManager* instance = nil; @implementation ScreenManager + (ScreenManager *)sharedInstance; {@synchronized (self) {if(instance==nil){instance = [[self alloc] init];}}return instance; }-(bool) DNA_U3D_GetPortrait {return self.isPortrait; }@end#if defined (__cplusplus) extern "C" { #endif void DNA_U3D_SetLandscape() {NSLog(@"設置水平方向");[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"]; [ScreenManager sharedInstance] .isPortrait = false; }void DNA_U3D_SetPortrait() {NSLog(@"設置垂直方向");[ScreenManager sharedInstance] .isPortrait = true; } #if defined (__cplusplus) } #endif在“UnityAppController.mm”文件中添加頭文件引用
#include "Screen/ScreenManager.h"同時在- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window函數(shù)中修改代碼如下:
- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window{ bool isPortrait = [[ScreenManager sharedInstance] DNA_U3D_GetPortrait];if(isPortrait)return UIInterfaceOrientationMaskAllButUpsideDown;return UIInterfaceOrientationMaskLandscape;}該函數(shù)在手機屏幕方向變化的時候會調用到。
注意:如果我們在手機旋轉到豎屏的時候退出uniwebview2,這個時候是豎屏,返回游戲的時候,這個時候屏幕并沒有變化,所以supportedInterfaceOrientationsForWindow這個函數(shù)是不會被調用到的,當返回游戲的時候,屏幕還是豎屏,必須需要手動旋轉一下才能恢復橫屏模式。所以我們需要在返回游戲的時候強制設置一下手機為橫屏,代碼如下
void DNA_U3D_SetLandscape() {NSLog(@"設置水平方向");[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"]; [ScreenManager sharedInstance] .isPortrait = false; }總結
以上是生活随笔為你收集整理的Uniwebview2插件常见问题以及刘海屏屏幕适配,屏幕旋转的解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: svg动画制作_制作第一个SVG动画
- 下一篇: [html] 写一个布局,当页面滚动一