Permalink
Browse files

Merge pull request #13 from endavid/fix/iOSLandscape

Fix iOS plugin SetMargins
  • Loading branch information...
splhack committed Jun 6, 2013
2 parents 4d7c6a4 + f1241c1 commit 3343201798085d60f43dfa960d25fd3b0931b6ae
Showing with 50 additions and 7 deletions.
  1. +17 −0 plugins/WebViewObject.cs
  2. +33 −7 plugins/iOS/WebView.mm
View
@@ -94,6 +94,9 @@ public class WebViewObject : MonoBehaviour
[DllImport("__Internal")]
private static extern void _WebViewPlugin_EvaluateJS(
IntPtr instance, string url);
+ [DllImport("__Internal")]
+ private static extern void _WebViewPlugin_SetFrame(
+ IntPtr instance, int x , int y , int width , int height);
#endif
#if UNITY_EDITOR || UNITY_STANDALONE_OSX
@@ -143,6 +146,20 @@ void OnDestroy()
#endif
}
+ /** Use this function instead of SetMargins to easily set up a centered window */
+ public void SetCenterPositionWithScale(Vector2 center , Vector2 scale)
+ {
+#if UNITY_EDITOR || UNITY_STANDALONE_OSX
+ rect.x = center.x + (Screen.width - scale.x)/2;
+ rect.y = center.y + (Screen.height - scale.y)/2;
+ rect.width = scale.x;
+ rect.height = scale.y;
+#elif UNITY_IPHONE
+ if(webView == IntPtr.Zero) return;
+ _WebViewPlugin_SetFrame(webView,(int)center.x,(int)center.y,(int)scale.x,(int)scale.y);
+#endif
+ }
+
public void SetMargins(int left, int top, int right, int bottom)
{
#if UNITY_EDITOR || UNITY_STANDALONE_OSX
View
@@ -67,16 +67,28 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
}
}
+- (void)setFrame:(NSInteger)x positionY:(NSInteger)y width:(NSInteger)width height:(NSInteger)height
+{
+ UIView* view = UnityGetGLViewController().view;
+ CGRect frame = webView.frame;
+ CGRect screen = view.bounds;
+ frame.origin.x = x + ((screen.size.width - width)/2);
+ frame.origin.y = -y + ((screen.size.height - height)/2);
+ frame.size.width = width;
+ frame.size.height = height;
+ webView.frame = frame;
+}
+
- (void)setMargins:(int)left top:(int)top right:(int)right bottom:(int)bottom
{
UIView *view = UnityGetGLViewController().view;
-
- CGRect frame = view.frame;
- CGFloat scale = view.contentScaleFactor;
- frame.size.width -= (left + right) / scale;
- frame.size.height -= (top + bottom) / scale;
- frame.origin.x += left / scale;
- frame.origin.y += top / scale;
+ CGRect frame = webView.frame;
+ CGRect screen = view.bounds;
+ CGFloat scale = 1.0f / view.contentScaleFactor;
+ frame.size.width = screen.size.width - scale * (left + right) ;
+ frame.size.height = screen.size.height - scale * (top + bottom) ;
+ frame.origin.x = scale * left ;
+ frame.origin.y = scale * top ;
webView.frame = frame;
}
@@ -104,6 +116,7 @@ - (void)evaluateJS:(const char *)js
extern "C" {
void *_WebViewPlugin_Init(const char *gameObjectName);
void _WebViewPlugin_Destroy(void *instance);
+ void _WebViewPlugin_SetFrame(void* instace,NSInteger x,NSInteger y,NSInteger width,NSInteger height);
void _WebViewPlugin_SetMargins(
void *instance, int left, int top, int right, int bottom);
void _WebViewPlugin_SetVisibility(void *instance, BOOL visibility);
@@ -123,6 +136,19 @@ void _WebViewPlugin_Destroy(void *instance)
[webViewPlugin release];
}
+void _WebViewPlugin_SetFrame(void* instance,NSInteger x,NSInteger y,NSInteger width,NSInteger height)
+{
+ float screenScale = [ UIScreen instancesRespondToSelector:@selector( scale ) ]?
+ [ UIScreen mainScreen ].scale:1.0f;
+
+ WebViewPlugin* webViewPlugin = (WebViewPlugin*)instance;
+ if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
+ if(screenScale == 2.0)
+ screenScale = 1.0f;
+ }
+ [webViewPlugin setFrame:x/screenScale positionY:y/screenScale width:width/screenScale height: height/screenScale];
+}
+
void _WebViewPlugin_SetMargins(
void *instance, int left, int top, int right, int bottom)
{

0 comments on commit 3343201

Please sign in to comment.