Skip to content

Instantly share code, notes, and snippets.

@mattak
Created March 18, 2016 02:56
Show Gist options
  • Save mattak/86507104c93357966995 to your computer and use it in GitHub Desktop.
Save mattak/86507104c93357966995 to your computer and use it in GitHub Desktop.
UnityView transparent & touchable on Unity 5.3.3f1
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class HelloPlugin : MonoBehaviour
{
[DllImport ("__Internal")]
private static extern void hello_ ();
[DllImport ("__Internal")]
private static extern void init_ ();
void Start ()
{
if (Application.platform == RuntimePlatform.IPhonePlayer) {
init_ ();
} else {
Debug.Log ("init");
}
}
public void hello ()
{
if (Application.platform == RuntimePlatform.IPhonePlayer) {
hello_ ();
} else {
Debug.Log ("hello");
}
}
}
#import <Foundation/Foundation.h>
extern "C" {
void hello_ ();
void init_ ();
}
#import "HelloPlugin.h"
#import <Mapbox/Mapbox.h>
#import "UnityView.h"
#import "UnityView+Touchable.h"
@implementation HelloPlugin
+ (UIWindow*) getWindow {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
if (!window) {
window = [UIApplication sharedApplication].windows[0];
}
return window;
}
+ (UnityView*) getUnityView {
return [HelloPlugin getWindow].subviews[0];
}
+ (UIView*) createCustomView:(CGRect)frame {
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width/2, frame.size.height/2, frame.size.width/2, frame.size.height/2)];
view.backgroundColor = [[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:1];
return view;
}
+ (UIView*) createMapView:(CGRect)frame {
MGLMapView *mapView = [[MGLMapView alloc] initWithFrame:frame];
mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[mapView setCenterCoordinate:CLLocationCoordinate2DMake(40.7326808, -73.9843407)
zoomLevel:12 animated:NO];
return mapView;
}
@end
void init_ () {
[MGLAccountManager setAccessToken:@"YOUR TOKEN"];
UIWindow* window = HelloPlugin.getWindow;
UnityView* unityView = HelloPlugin.getUnityView;
unityView.handleTouch = FALSE;
UIView* mapView = [HelloPlugin createMapView:window.frame];
[window addSubview:mapView];
[window sendSubviewToBack:mapView];
}
void hello_ () {
NSLog(@"Hello, Unity!");
}
--- /Users/maruyama/git/unity/UnityMapBox/Exports/ios/Classes/Unity/MetalHelper.mm 2016-02-20 05:20:34.000000000 +0900
+++ MetalHelper.mm 2016-03-16 13:50:33.000000000 +0900
@@ -35,7 +35,8 @@
surface->layer.presentsWithTransaction = NO;
surface->layer.drawsAsynchronously = YES;
- CGFloat backgroundColor[] = {0,0,0,1};
+ surface->layer.opaque = FALSE;
+ CGFloat backgroundColor[] = {0,0,0,0};
surface->layer.backgroundColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), backgroundColor);
surface->layer.device = surface->device;
surface->layer.pixelFormat = colorFormat;
#import "UnityView+Touchable.h"
#import <objc/runtime.h>
@implementation UnityView(Touchable)
static char const * const kKeyHandleTouch = "handleTouch";
@dynamic handleTouch;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
CGFloat thresholdX = [UIApplication sharedApplication].keyWindow.frame.size.width / 2;
return (thresholdX > point.x) ? YES : NO;
}
- (void)setHandleTouch:(BOOL)handleTouch {
NSNumber *number = [NSNumber numberWithBool:handleTouch];
objc_setAssociatedObject(self, kKeyHandleTouch, number, OBJC_ASSOCIATION_RETAIN);
}
- (BOOL)handleTouch {
NSNumber *number = objc_getAssociatedObject(self, kKeyHandleTouch);
return [number boolValue];
}
@end
#import <Foundation/Foundation.h>
#import "UnityView.h"
@interface UnityView (Touchable)
@property (readwrite) BOOL handleTouch;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment