Skip to content

Commit

Permalink
introduced transparent background option.
Browse files Browse the repository at this point in the history
  • Loading branch information
KojiNakamaru committed Dec 14, 2015
1 parent 93dffa0 commit 4e30bf2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
6 changes: 5 additions & 1 deletion plugins/Android/src/net/gree/unitywebview/WebViewPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public boolean IsInitialized() {
return mWebView != null;
}

public void Init(final String gameObject) {
public void Init(final String gameObject, final boolean transparent) {
final WebViewPlugin self = this;
final Activity a = UnityPlayer.currentActivity;
a.runOnUiThread(new Runnable() {public void run() {
Expand Down Expand Up @@ -126,6 +126,10 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
String databasePath = webView.getContext().getDir("databases", Context.MODE_PRIVATE).getPath();
webSettings.setDatabasePath(databasePath);

if (transparent) {
webView.setBackgroundColor(0x00000000);
}

if (layout == null) {
layout = new FrameLayout(a);
a.addContentView(
Expand Down
11 changes: 7 additions & 4 deletions plugins/Mac/Sources/WebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,15 @@ @interface WebViewPlugin : NSObject

@implementation WebViewPlugin

- (id)initWithGameObject:(const char *)gameObject_ width:(int)width height:(int)height ua:(const char *)ua_
- (id)initWithGameObject:(const char *)gameObject_ transparent:(BOOL)transparent width:(int)width height:(int)height ua:(const char *)ua_
{
self = [super init];
monoMethod = 0;
webView = [[WebView alloc] initWithFrame:NSMakeRect(0, 0, width, height)];
webView.hidden = YES;
if (transparent) {
[webView setDrawsBackground:NO];
}
[webView setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)];
[webView setPolicyDelegate:(id)self];
gameObject = [[NSString stringWithUTF8String:gameObject_] retain];
Expand Down Expand Up @@ -315,7 +318,7 @@ - (void)render
typedef void (*UnityRenderEventFunc)(int eventId);
extern "C" {
void *_WebViewPlugin_Init(
const char *gameObject, int width, int height, BOOL inEditor, const char *ua);
const char *gameObject, BOOL transparent, int width, int height, const char *ua, BOOL ineditor);
void _WebViewPlugin_Destroy(void *instance);
void _WebViewPlugin_SetRect(void *instance, int width, int height);
void _WebViewPlugin_SetVisibility(void *instance, BOOL visibility);
Expand All @@ -335,13 +338,13 @@ void _WebViewPlugin_Update(void *instance, int x, int y, float deltaY,
static NSMutableSet *pool;

void *_WebViewPlugin_Init(
const char *gameObject, int width, int height, BOOL ineditor, const char *ua)
const char *gameObject, BOOL transparent, int width, int height, const char *ua, BOOL ineditor)
{
if (pool == 0)
pool = [[NSMutableSet alloc] init];

inEditor = ineditor;
id instance = [[WebViewPlugin alloc] initWithGameObject:gameObject width:width height:height ua:ua];
id instance = [[WebViewPlugin alloc] initWithGameObject:gameObject transparent:transparent width:width height:height ua:ua];
[pool addObject:[NSValue valueWithPointer:instance]];
return (void *)instance;
}
Expand Down
17 changes: 9 additions & 8 deletions plugins/WebViewObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public bool IsKeyboardVisible {
#if UNITY_EDITOR || UNITY_STANDALONE_OSX
[DllImport("WebView")]
private static extern IntPtr _WebViewPlugin_Init(
string gameObject, int width, int height, bool ineditor, string ua);
string gameObject, bool transparent, int width, int height, string ua, bool ineditor);
[DllImport("WebView")]
private static extern int _WebViewPlugin_Destroy(IntPtr instance);
[DllImport("WebView")]
Expand Down Expand Up @@ -108,7 +108,7 @@ private static extern void _WebViewPlugin_Update(IntPtr instance,
private static extern IntPtr GetRenderEventFunc();
#elif UNITY_IPHONE
[DllImport("__Internal")]
private static extern IntPtr _WebViewPlugin_Init(string gameObject);
private static extern IntPtr _WebViewPlugin_Init(string gameObject, bool transparent);
[DllImport("__Internal")]
private static extern int _WebViewPlugin_Destroy(IntPtr instance);
[DllImport("__Internal")]
Expand Down Expand Up @@ -136,19 +136,20 @@ void OnApplicationFocus(bool focus)
#endif

#if UNITY_EDITOR || UNITY_STANDALONE_OSX
public void Init(Callback cb = null, string ua = @"Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53")
public void Init(Callback cb = null, bool transparent = false, string ua = @"Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53")
#else
public void Init(Callback cb = null)
public void Init(Callback cb = null, bool transparent = false)
#endif
{
callback = cb;
#if UNITY_EDITOR || UNITY_STANDALONE_OSX
webView = _WebViewPlugin_Init(
name,
transparent,
Screen.width,
Screen.height,
Application.platform == RuntimePlatform.OSXEditor,
ua);
ua,
Application.platform == RuntimePlatform.OSXEditor);
// define pseudo requestAnimationFrame.
EvaluateJS(@"(function() {
var vsync = 1000 / 60;
Expand All @@ -164,10 +165,10 @@ public void Init(Callback cb = null)
rect = new Rect(0, 0, Screen.width, Screen.height);
OnApplicationFocus(true);
#elif UNITY_IPHONE
webView = _WebViewPlugin_Init(name);
webView = _WebViewPlugin_Init(name, transparent);
#elif UNITY_ANDROID
webView = new AndroidJavaObject("net.gree.unitywebview.WebViewPlugin");
webView.Call("Init", name);
webView.Call("Init", name, transparent);
#elif UNITY_WEBPLAYER
Application.ExternalCall("unityWebView.init", name);
#endif
Expand Down
12 changes: 8 additions & 4 deletions plugins/iOS/WebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ @interface WebViewPlugin : NSObject<UIWebViewDelegate>

@implementation WebViewPlugin

- (id)initWithGameObjectName:(const char *)gameObjectName_
- (id)initWithGameObjectName:(const char *)gameObjectName_ transparent:(BOOL)transparent
{
self = [super init];

UIView *view = UnityGetGLViewController().view;
webView = [[UIWebView alloc] initWithFrame:view.frame];
if (transparent) {
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
}
webView.delegate = self;
webView.hidden = YES;
[view addSubview:webView];
Expand Down Expand Up @@ -133,7 +137,7 @@ - (void)evaluateJS:(const char *)js
@end

extern "C" {
void *_WebViewPlugin_Init(const char *gameObjectName);
void *_WebViewPlugin_Init(const char *gameObjectName, BOOL transparent);
void _WebViewPlugin_Destroy(void *instance);
void _WebViewPlugin_SetFrame(void* instace, int x, int y, int width, int height);
void _WebViewPlugin_SetMargins(
Expand All @@ -143,9 +147,9 @@ void _WebViewPlugin_SetMargins(
void _WebViewPlugin_EvaluateJS(void *instance, const char *url);
}

void *_WebViewPlugin_Init(const char *gameObjectName)
void *_WebViewPlugin_Init(const char *gameObjectName, BOOL transparent)
{
id instance = [[WebViewPlugin alloc] initWithGameObjectName:gameObjectName];
id instance = [[WebViewPlugin alloc] initWithGameObjectName:gameObjectName transparent:transparent];
return (__bridge_retained void *)instance;
}

Expand Down

0 comments on commit 4e30bf2

Please sign in to comment.